2015-12-14 21:54:33 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
2016-04-24 23:51:24 +02:00
|
|
|
import sys
|
2016-03-17 15:19:36 +01:00
|
|
|
|
2016-04-24 23:51:24 +02:00
|
|
|
from appdirs import user_data_dir
|
2015-12-14 21:54:33 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.task import LoopingCall
|
2016-03-17 15:19:36 +01:00
|
|
|
|
2015-12-14 21:54:33 +01:00
|
|
|
from lbrynet.core.Error import InvalidStreamInfoError, InsufficientFundsError
|
|
|
|
from lbrynet.core.PaymentRateManager import PaymentRateManager
|
|
|
|
from lbrynet.core.StreamDescriptor import download_sd_blob
|
2016-03-08 18:15:49 +01:00
|
|
|
from lbrynet.lbryfilemanager.LBRYFileDownloader import ManagedLBRYFileDownloaderFactory
|
2016-06-07 10:19:51 +02:00
|
|
|
from lbrynet.conf import DEFAULT_TIMEOUT, LOG_FILE_NAME
|
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 = [
|
|
|
|
(INITIALIZING_CODE, 'Initializing...'),
|
|
|
|
(DOWNLOAD_METADATA_CODE, 'Downloading metadata'),
|
|
|
|
(DOWNLOAD_RUNNING_CODE, 'Started stream'),
|
|
|
|
(DOWNLOAD_STOPPED_CODE, 'Paused stream'),
|
|
|
|
(DOWNLOAD_TIMEOUT_CODE, 'Stream timed out')
|
|
|
|
]
|
|
|
|
|
2016-06-07 10:19:51 +02:00
|
|
|
if sys.platform != "darwin":
|
|
|
|
log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet")
|
|
|
|
else:
|
|
|
|
log_dir = user_data_dir("LBRY")
|
|
|
|
|
|
|
|
if not os.path.isdir(log_dir):
|
|
|
|
os.mkdir(log_dir)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2016-06-07 10:30:22 +02:00
|
|
|
lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME)
|
2016-06-07 10:19:51 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2016-06-07 10:30:22 +02:00
|
|
|
handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5)
|
2016-06-07 10:19:51 +02:00
|
|
|
log.addHandler(handler)
|
|
|
|
log.setLevel(logging.INFO)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
|
|
|
class GetStream(object):
|
2016-06-27 23:07:59 +02:00
|
|
|
def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, data_rate=0.5,
|
|
|
|
timeout=DEFAULT_TIMEOUT, download_directory=None, file_name=None):
|
2015-12-14 21:54:33 +01:00
|
|
|
self.wallet = wallet
|
|
|
|
self.resolved_name = None
|
|
|
|
self.description = None
|
|
|
|
self.key_fee = None
|
|
|
|
self.key_fee_address = None
|
2016-01-20 03:07:16 +01:00
|
|
|
self.data_rate = data_rate
|
2015-12-14 21:54:33 +01:00
|
|
|
self.name = None
|
2016-05-05 03:25:46 +02:00
|
|
|
self.file_name = file_name
|
2015-12-14 21:54:33 +01:00
|
|
|
self.session = session
|
|
|
|
self.payment_rate_manager = PaymentRateManager(self.session.base_payment_rate_manager)
|
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
self.sd_identifier = sd_identifier
|
|
|
|
self.stream_hash = None
|
|
|
|
self.max_key_fee = max_key_fee
|
|
|
|
self.stream_info = None
|
|
|
|
self.stream_info_manager = None
|
2016-03-17 05:44:04 +01:00
|
|
|
self.d = defer.Deferred(None)
|
|
|
|
self.timeout = timeout
|
|
|
|
self.timeout_counter = 0
|
2016-04-07 09:12:09 +02:00
|
|
|
self.download_directory = download_directory
|
2016-03-17 05:44:04 +01:00
|
|
|
self.download_path = None
|
2016-04-24 10:42:42 +02:00
|
|
|
self.downloader = None
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished = defer.Deferred()
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker = LoopingCall(self.check_status)
|
2016-05-04 11:20:38 +02:00
|
|
|
self.code = STREAM_STAGES[0]
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2016-03-17 05:44:04 +01:00
|
|
|
def check_status(self):
|
|
|
|
self.timeout_counter += 1
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
if self.download_path:
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.stop()
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished.callback((self.stream_hash, self.download_path))
|
2016-03-17 05:44:04 +01:00
|
|
|
|
|
|
|
elif self.timeout_counter >= self.timeout:
|
2016-06-27 23:07:59 +02:00
|
|
|
log.info("Timeout downloading lbry://%s" % self.resolved_name)
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.stop()
|
|
|
|
self.d.cancel()
|
2016-05-04 11:20:38 +02:00
|
|
|
self.code = STREAM_STAGES[4]
|
2016-04-23 00:18:17 +02:00
|
|
|
self.finished.callback(False)
|
2016-03-17 05:44:04 +01:00
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
def start(self, stream_info, name):
|
|
|
|
self.resolved_name = name
|
2015-12-14 21:54:33 +01:00
|
|
|
self.stream_info = stream_info
|
2016-06-27 23:07:59 +02:00
|
|
|
if 'sources' in self.stream_info:
|
2016-05-05 05:40:05 +02:00
|
|
|
self.stream_hash = self.stream_info['sources']['lbry_sd_hash']
|
|
|
|
else:
|
|
|
|
raise InvalidStreamInfoError(self.stream_info)
|
2016-06-27 23:07:59 +02:00
|
|
|
if 'description' in self.stream_info:
|
2015-12-14 21:54:33 +01:00
|
|
|
self.description = self.stream_info['description']
|
2016-06-27 23:07:59 +02:00
|
|
|
if 'fee' in self.stream_info:
|
|
|
|
if 'LBC' in self.stream_info['fee']:
|
|
|
|
self.key_fee = float(self.stream_info['fee']['LBC']['amount'])
|
|
|
|
self.key_fee_address = self.stream_info['fee']['LBC']['address']
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
|
|
|
self.key_fee_address = None
|
|
|
|
else:
|
2016-05-05 05:40:05 +02:00
|
|
|
self.key_fee = None
|
|
|
|
self.key_fee_address = None
|
2015-12-14 21:54:33 +01:00
|
|
|
if self.key_fee > self.max_key_fee:
|
2016-06-27 23:07:59 +02:00
|
|
|
log.info("Key fee %f above limit of %f didn't download lbry://%s" % (self.key_fee, self.max_key_fee, self.resolved_name))
|
|
|
|
return defer.fail(None)
|
2015-12-14 21:54:33 +01:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
def _cause_timeout():
|
|
|
|
self.timeout_counter = self.timeout * 2
|
|
|
|
|
2016-05-04 11:20:38 +02:00
|
|
|
def _set_status(x, status):
|
2016-06-28 07:51:05 +02:00
|
|
|
log.info("Download lbry://%s status changed to %s" % (self.resolved_name, status))
|
2016-05-04 11:20:38 +02:00
|
|
|
self.code = next(s for s in STREAM_STAGES if s[0] == status)
|
|
|
|
return x
|
|
|
|
|
2016-03-17 05:44:04 +01:00
|
|
|
self.checker.start(1)
|
2016-01-20 03:07:16 +01:00
|
|
|
|
2016-05-04 11:20:38 +02:00
|
|
|
self.d.addCallback(lambda _: _set_status(None, DOWNLOAD_METADATA_CODE))
|
2016-03-17 05:44:04 +01:00
|
|
|
self.d.addCallback(lambda _: download_sd_blob(self.session, self.stream_hash, self.payment_rate_manager))
|
|
|
|
self.d.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
|
2016-05-04 11:20:38 +02:00
|
|
|
self.d.addCallback(lambda r: _set_status(r, DOWNLOAD_RUNNING_CODE))
|
2016-06-28 07:51:05 +02:00
|
|
|
self.d.addCallback(lambda metadata: (next(factory for factory in metadata.factories if isinstance(factory, ManagedLBRYFileDownloaderFactory)),
|
|
|
|
metadata))
|
2016-04-07 09:12:09 +02:00
|
|
|
self.d.addCallback(lambda (factory, metadata): factory.make_downloader(metadata,
|
|
|
|
[self.data_rate, True],
|
|
|
|
self.payment_rate_manager,
|
2016-05-05 03:25:46 +02:00
|
|
|
download_directory=self.download_directory,
|
|
|
|
file_name=self.file_name))
|
2016-04-23 00:18:17 +02:00
|
|
|
self.d.addCallbacks(self._start_download, lambda _: _cause_timeout())
|
2016-03-17 05:44:04 +01:00
|
|
|
self.d.callback(None)
|
|
|
|
|
2016-04-23 00:18:17 +02:00
|
|
|
return self.finished
|
2016-01-20 03:07:16 +01:00
|
|
|
|
|
|
|
def _start_download(self, downloader):
|
|
|
|
def _pay_key_fee():
|
|
|
|
if self.key_fee is not None and self.key_fee_address is not None:
|
|
|
|
reserved_points = self.wallet.reserve_points(self.key_fee_address, self.key_fee)
|
|
|
|
if reserved_points is None:
|
|
|
|
return defer.fail(InsufficientFundsError())
|
2016-06-27 23:07:59 +02:00
|
|
|
log.info("Key fee: %f --> %s" % (self.key_fee, self.key_fee_address))
|
2016-01-20 03:07:16 +01:00
|
|
|
return self.wallet.send_points_to_address(reserved_points, self.key_fee)
|
|
|
|
return defer.succeed(None)
|
2015-12-14 21:54:33 +01:00
|
|
|
|
2016-06-28 07:51:05 +02:00
|
|
|
d = _pay_key_fee()
|
2016-04-24 10:42:42 +02:00
|
|
|
self.downloader = downloader
|
2016-03-17 05:44:04 +01:00
|
|
|
self.download_path = os.path.join(downloader.download_directory, downloader.file_name)
|
2016-06-28 07:51:05 +02:00
|
|
|
d.addCallback(lambda _: log.info("[%s] Downloading %s --> %s" % (datetime.now(), self.stream_hash, self.downloader.file_name)))
|
2016-04-24 10:42:42 +02:00
|
|
|
d.addCallback(lambda _: self.downloader.start())
|
2016-06-28 07:51:05 +02:00
|
|
|
|