2015-12-14 21:54:33 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2017-06-07 19:01:29 +02:00
|
|
|
from twisted.internet import defer
|
2015-12-14 21:54:33 +01:00
|
|
|
from twisted.internet.task import LoopingCall
|
2016-03-17 15:19:36 +01:00
|
|
|
|
2018-10-17 17:21:21 +02:00
|
|
|
from lbrynet.daemon.Components import f2d
|
2017-05-29 22:38:08 +02:00
|
|
|
from lbryschema.fee import Fee
|
|
|
|
|
2018-02-22 01:09:10 +01:00
|
|
|
from lbrynet.core.Error import InsufficientFundsError, KeyFeeAboveMaxAllowed, InvalidStreamDescriptorError
|
2017-09-28 23:36:55 +02:00
|
|
|
from lbrynet.core.Error import DownloadDataTimeout, DownloadCanceledError, DownloadSDTimeout
|
2017-06-21 18:33:12 +02:00
|
|
|
from lbrynet.core.utils import safe_start_looping_call, safe_stop_looping_call
|
2015-12-14 21:54:33 +01:00
|
|
|
from lbrynet.core.StreamDescriptor import download_sd_blob
|
2017-06-26 03:04:04 +02:00
|
|
|
from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloaderFactory
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2018-09-14 07:59:04 +02:00
|
|
|
from torba.constants import COIN
|
2018-10-12 15:49:13 +02:00
|
|
|
from lbrynet.wallet.dewies import dewies_to_lbc
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2016-05-04 11:20:38 +02:00
|
|
|
INITIALIZING_CODE = 'initializing'
|
|
|
|
DOWNLOAD_METADATA_CODE = 'downloading_metadata'
|
|
|
|
DOWNLOAD_TIMEOUT_CODE = 'timeout'
|
|
|
|
DOWNLOAD_RUNNING_CODE = 'running'
|
|
|
|
DOWNLOAD_STOPPED_CODE = 'stopped'
|
|
|
|
STREAM_STAGES = [
|
2017-01-04 23:10:36 +01:00
|
|
|
(INITIALIZING_CODE, 'Initializing'),
|
2016-10-23 07:17:24 +02:00
|
|
|
(DOWNLOAD_METADATA_CODE, 'Downloading metadata'),
|
|
|
|
(DOWNLOAD_RUNNING_CODE, 'Started stream'),
|
|
|
|
(DOWNLOAD_STOPPED_CODE, 'Paused stream'),
|
|
|
|
(DOWNLOAD_TIMEOUT_CODE, 'Stream timed out')
|
|
|
|
]
|
2016-05-04 11:20:38 +02:00
|
|
|
|
2016-06-07 10:19:51 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2016-07-25 20:04:30 +02:00
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class GetStream:
|
2018-07-25 21:33:43 +02:00
|
|
|
def __init__(self, sd_identifier, wallet, exchange_rate_manager, blob_manager, peer_finder, rate_limiter,
|
|
|
|
payment_rate_manager, storage, max_key_fee, disable_max_key_fee, data_rate=None, timeout=None):
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
self.timeout = timeout or conf.settings['download_timeout']
|
|
|
|
self.data_rate = data_rate or conf.settings['data_rate']
|
|
|
|
self.max_key_fee = max_key_fee or conf.settings['max_key_fee'][1]
|
2017-07-06 20:53:16 +02:00
|
|
|
self.disable_max_key_fee = disable_max_key_fee or conf.settings['disable_max_key_fee']
|
2017-06-13 21:28:49 +02:00
|
|
|
self.download_directory = conf.settings['download_directory']
|
2017-02-13 02:57:23 +01:00
|
|
|
self.timeout_counter = 0
|
|
|
|
self.code = None
|
|
|
|
self.sd_hash = None
|
2018-07-25 21:33:43 +02:00
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.peer_finder = peer_finder
|
|
|
|
self.rate_limiter = rate_limiter
|
|
|
|
self.wallet = wallet
|
2016-07-28 11:30:13 +02:00
|
|
|
self.exchange_rate_manager = exchange_rate_manager
|
2018-07-25 21:33:43 +02:00
|
|
|
self.payment_rate_manager = payment_rate_manager
|
2015-12-14 21:54:33 +01:00
|
|
|
self.sd_identifier = sd_identifier
|
2018-07-25 21:33:43 +02:00
|
|
|
self.storage = storage
|
2016-04-24 10:42:42 +02:00
|
|
|
self.downloader = None
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker = LoopingCall(self.check_status)
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
# fired when the download is complete
|
2017-06-07 19:01:29 +02:00
|
|
|
self.finished_deferred = None
|
2017-02-13 02:57:23 +01:00
|
|
|
# fired after the metadata and the first data blob have been downloaded
|
|
|
|
self.data_downloading_deferred = defer.Deferred(None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def download_path(self):
|
|
|
|
return os.path.join(self.download_directory, self.downloader.file_name)
|
2016-03-17 05:44:04 +01:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
def _check_status(self, status):
|
2017-09-27 23:47:36 +02:00
|
|
|
if status.num_completed > 0 and not self.data_downloading_deferred.called:
|
2017-02-13 02:57:23 +01:00
|
|
|
self.data_downloading_deferred.callback(True)
|
|
|
|
if self.data_downloading_deferred.called:
|
2017-06-21 18:33:12 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
2017-02-13 02:57:23 +01:00
|
|
|
else:
|
2017-09-28 23:36:55 +02:00
|
|
|
log.debug("Waiting for stream data (%i seconds)", self.timeout_counter)
|
2016-03-17 05:44:04 +01:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
def check_status(self):
|
|
|
|
"""
|
|
|
|
Check if we've got the first data blob in the stream yet
|
|
|
|
"""
|
|
|
|
self.timeout_counter += 1
|
2017-09-27 23:25:21 +02:00
|
|
|
if self.timeout_counter > self.timeout:
|
2017-02-13 02:57:23 +01:00
|
|
|
if not self.data_downloading_deferred.called:
|
2017-09-28 23:36:55 +02:00
|
|
|
if self.downloader:
|
|
|
|
err = DownloadDataTimeout(self.sd_hash)
|
|
|
|
else:
|
|
|
|
err = DownloadSDTimeout(self.sd_hash)
|
|
|
|
self.data_downloading_deferred.errback(err)
|
2017-06-21 18:33:12 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
2017-09-28 23:36:55 +02:00
|
|
|
elif self.downloader:
|
2017-02-13 02:57:23 +01:00
|
|
|
d = self.downloader.status()
|
|
|
|
d.addCallback(self._check_status)
|
2017-09-28 23:36:55 +02:00
|
|
|
else:
|
|
|
|
log.debug("Waiting for stream descriptor (%i seconds)", self.timeout_counter)
|
2017-02-13 02:57:23 +01:00
|
|
|
|
|
|
|
def convert_max_fee(self):
|
2017-05-30 23:07:23 +02:00
|
|
|
currency, amount = self.max_key_fee['currency'], self.max_key_fee['amount']
|
|
|
|
return self.exchange_rate_manager.convert_currency(currency, "LBC", amount)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
def set_status(self, status, name):
|
|
|
|
log.info("Download lbry://%s status changed to %s" % (name, status))
|
|
|
|
self.code = next(s for s in STREAM_STAGES if s[0] == status)
|
|
|
|
|
2018-09-14 07:59:04 +02:00
|
|
|
@defer.inlineCallbacks
|
2017-05-30 23:07:23 +02:00
|
|
|
def check_fee_and_convert(self, fee):
|
|
|
|
max_key_fee_amount = self.convert_max_fee()
|
|
|
|
converted_fee_amount = self.exchange_rate_manager.convert_currency(fee.currency, "LBC",
|
|
|
|
fee.amount)
|
2018-10-17 17:21:21 +02:00
|
|
|
if converted_fee_amount > (yield f2d(self.wallet.default_account.get_balance())):
|
2017-05-30 23:07:23 +02:00
|
|
|
raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee_amount)
|
2017-07-06 20:53:16 +02:00
|
|
|
if converted_fee_amount > max_key_fee_amount and not self.disable_max_key_fee:
|
2018-10-18 12:42:45 +02:00
|
|
|
raise KeyFeeAboveMaxAllowed('Key fee {} above max allowed {}'.format(converted_fee_amount,
|
|
|
|
max_key_fee_amount))
|
2017-05-30 23:07:23 +02:00
|
|
|
converted_fee = {
|
|
|
|
'currency': 'LBC',
|
|
|
|
'amount': converted_fee_amount,
|
|
|
|
'address': fee.address
|
|
|
|
}
|
|
|
|
return Fee(converted_fee)
|
2017-02-13 02:57:23 +01:00
|
|
|
|
|
|
|
def get_downloader_factory(self, factories):
|
|
|
|
for factory in factories:
|
|
|
|
if isinstance(factory, ManagedEncryptedFileDownloaderFactory):
|
|
|
|
return factory
|
2018-10-18 12:42:45 +02:00
|
|
|
raise Exception(f'No suitable factory was found in {factories}')
|
2017-02-13 02:57:23 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:13:30 +01:00
|
|
|
def get_downloader(self, factory, stream_metadata, file_name=None):
|
2017-02-14 20:18:42 +01:00
|
|
|
# TODO: we should use stream_metadata.options.get_downloader_options
|
|
|
|
# instead of hard-coding the options to be [self.data_rate]
|
2017-02-14 18:32:04 +01:00
|
|
|
downloader = yield factory.make_downloader(
|
|
|
|
stream_metadata,
|
2018-02-12 20:13:30 +01:00
|
|
|
self.data_rate,
|
2017-02-14 18:32:04 +01:00
|
|
|
self.payment_rate_manager,
|
2018-02-12 20:13:30 +01:00
|
|
|
self.download_directory,
|
|
|
|
file_name=file_name
|
2017-02-14 18:32:04 +01:00
|
|
|
)
|
2017-02-13 02:57:23 +01:00
|
|
|
defer.returnValue(downloader)
|
|
|
|
|
|
|
|
def _pay_key_fee(self, address, fee_lbc, name):
|
2018-10-12 15:49:13 +02:00
|
|
|
log.info("Pay key fee %s --> %s", dewies_to_lbc(fee_lbc), address)
|
2017-02-13 02:57:23 +01:00
|
|
|
reserved_points = self.wallet.reserve_points(address, fee_lbc)
|
|
|
|
if reserved_points is None:
|
2018-10-12 15:49:13 +02:00
|
|
|
raise InsufficientFundsError(
|
2018-10-18 12:42:45 +02:00
|
|
|
'Unable to pay the key fee of {} for {}'.format(dewies_to_lbc(fee_lbc), name)
|
2018-10-12 15:49:13 +02:00
|
|
|
)
|
2018-10-17 17:21:21 +02:00
|
|
|
return f2d(self.wallet.send_points_to_address(reserved_points, fee_lbc))
|
2017-02-13 02:57:23 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def pay_key_fee(self, fee, name):
|
|
|
|
if fee is not None:
|
2018-09-14 07:59:04 +02:00
|
|
|
yield self._pay_key_fee(fee.address.decode(), int(fee.amount * COIN), name)
|
2017-02-13 02:57:23 +01:00
|
|
|
else:
|
|
|
|
defer.returnValue(None)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def finish(self, results, name):
|
|
|
|
self.set_status(DOWNLOAD_STOPPED_CODE, name)
|
|
|
|
log.info("Finished downloading lbry://%s (%s) --> %s", name, self.sd_hash[:6],
|
|
|
|
self.download_path)
|
2017-06-21 18:33:12 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
2017-02-13 02:57:23 +01:00
|
|
|
status = yield self.downloader.status()
|
|
|
|
self._check_status(status)
|
|
|
|
defer.returnValue(self.download_path)
|
|
|
|
|
2017-09-27 23:23:41 +02:00
|
|
|
def fail(self, err):
|
|
|
|
safe_stop_looping_call(self.checker)
|
2017-09-28 19:51:20 +02:00
|
|
|
raise err
|
2017-09-27 23:23:41 +02:00
|
|
|
|
2017-02-13 02:57:23 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-06-21 17:48:35 +02:00
|
|
|
def _initialize(self, stream_info):
|
2017-06-07 19:01:29 +02:00
|
|
|
# Set sd_hash and return key_fee from stream_info
|
2018-08-10 23:23:50 +02:00
|
|
|
self.sd_hash = stream_info.source_hash.decode()
|
2017-06-07 19:01:29 +02:00
|
|
|
key_fee = None
|
2017-05-29 22:38:08 +02:00
|
|
|
if stream_info.has_fee:
|
2017-06-07 19:01:29 +02:00
|
|
|
key_fee = yield self.check_fee_and_convert(stream_info.source_fee)
|
|
|
|
defer.returnValue(key_fee)
|
2017-02-13 02:57:23 +01:00
|
|
|
|
2017-06-07 19:01:29 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:13:30 +01:00
|
|
|
def _create_downloader(self, sd_blob, file_name=None):
|
2017-02-13 02:57:23 +01:00
|
|
|
stream_metadata = yield self.sd_identifier.get_metadata_for_sd_blob(sd_blob)
|
|
|
|
factory = self.get_downloader_factory(stream_metadata.factories)
|
2018-02-12 20:13:30 +01:00
|
|
|
downloader = yield self.get_downloader(factory, stream_metadata, file_name)
|
2017-06-07 19:01:29 +02:00
|
|
|
defer.returnValue(downloader)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-06-21 17:48:35 +02:00
|
|
|
def _download_sd_blob(self):
|
2018-08-03 15:36:03 +02:00
|
|
|
sd_blob = yield download_sd_blob(
|
|
|
|
self.sd_hash, self.blob_manager, self.peer_finder, self.rate_limiter, self.payment_rate_manager,
|
|
|
|
self.wallet, self.timeout, conf.settings['download_mirrors']
|
|
|
|
)
|
2017-06-21 17:48:35 +02:00
|
|
|
defer.returnValue(sd_blob)
|
2017-02-13 02:57:23 +01:00
|
|
|
|
2017-06-21 17:48:35 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:13:30 +01:00
|
|
|
def _download(self, sd_blob, name, key_fee, txid, nout, file_name=None):
|
|
|
|
self.downloader = yield self._create_downloader(sd_blob, file_name=file_name)
|
2017-06-21 17:48:35 +02:00
|
|
|
yield self.pay_key_fee(key_fee, name)
|
2018-07-25 21:33:43 +02:00
|
|
|
yield self.storage.save_content_claim(self.downloader.stream_hash, "%s:%i" % (txid, nout))
|
2017-02-13 02:57:23 +01:00
|
|
|
log.info("Downloading lbry://%s (%s) --> %s", name, self.sd_hash[:6], self.download_path)
|
|
|
|
self.finished_deferred = self.downloader.start()
|
2017-09-28 19:51:20 +02:00
|
|
|
self.finished_deferred.addCallbacks(lambda result: self.finish(result, name), self.fail)
|
2017-02-13 02:57:23 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-02-12 20:13:30 +01:00
|
|
|
def start(self, stream_info, name, txid, nout, file_name=None):
|
2017-06-07 19:01:29 +02:00
|
|
|
"""
|
|
|
|
Start download
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
(tuple) Tuple containing (downloader, finished_deferred)
|
|
|
|
|
|
|
|
downloader - instance of ManagedEncryptedFileDownloader
|
|
|
|
finished_deferred - deferred callbacked when download is finished
|
|
|
|
"""
|
2017-06-21 17:48:35 +02:00
|
|
|
self.set_status(INITIALIZING_CODE, name)
|
|
|
|
key_fee = yield self._initialize(stream_info)
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2017-09-28 23:36:55 +02:00
|
|
|
safe_start_looping_call(self.checker, 1)
|
2017-06-21 17:48:35 +02:00
|
|
|
self.set_status(DOWNLOAD_METADATA_CODE, name)
|
2017-02-13 02:57:23 +01:00
|
|
|
try:
|
2018-02-22 01:09:10 +01:00
|
|
|
sd_blob = yield self._download_sd_blob()
|
|
|
|
yield self._download(sd_blob, name, key_fee, txid, nout, file_name)
|
|
|
|
self.set_status(DOWNLOAD_RUNNING_CODE, name)
|
2017-03-03 22:43:13 +01:00
|
|
|
yield self.data_downloading_deferred
|
2018-02-22 01:09:10 +01:00
|
|
|
except (DownloadDataTimeout, InvalidStreamDescriptorError) as err:
|
2017-06-21 18:33:12 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
2017-09-27 23:25:21 +02:00
|
|
|
raise err
|
2017-06-07 19:01:29 +02:00
|
|
|
|
|
|
|
defer.returnValue((self.downloader, self.finished_deferred))
|
2017-09-28 19:51:20 +02:00
|
|
|
|
|
|
|
def cancel(self, reason=None):
|
|
|
|
if reason:
|
|
|
|
msg = "download stream cancelled: %s" % reason
|
|
|
|
else:
|
|
|
|
msg = "download stream cancelled"
|
|
|
|
if self.data_downloading_deferred and not self.data_downloading_deferred.called:
|
|
|
|
self.data_downloading_deferred.errback(DownloadCanceledError(msg))
|