2015-08-20 17:27:15 +02:00
|
|
|
"""
|
|
|
|
Download LBRY Files from LBRYnet and save them to disk.
|
|
|
|
"""
|
2016-10-20 21:40:35 +02:00
|
|
|
import logging
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
from zope.interface import implements
|
2016-10-28 20:22:37 +02:00
|
|
|
from twisted.internet import defer
|
2016-10-20 21:40:35 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.core.client.StreamProgressManager import FullStreamProgressManager
|
2017-02-16 05:39:17 +01:00
|
|
|
from lbrynet.core.Error import NoSuchSDHash, NoSuchStreamHash
|
2017-02-16 15:09:21 +01:00
|
|
|
from lbrynet.core.utils import short_hash
|
2015-09-22 04:06:04 +02:00
|
|
|
from lbrynet.core.StreamDescriptor import StreamMetadata
|
2016-11-30 21:20:45 +01:00
|
|
|
from lbrynet.lbryfile.client.EncryptedFileDownloader import EncryptedFileSaver
|
|
|
|
from lbrynet.lbryfile.client.EncryptedFileDownloader import EncryptedFileDownloader
|
2016-09-27 20:18:16 +02:00
|
|
|
from lbrynet.lbryfilemanager.EncryptedFileStatusReport import EncryptedFileStatusReport
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.interfaces import IStreamDownloaderFactory
|
|
|
|
from lbrynet.lbryfile.StreamDescriptor import save_sd_info
|
2017-03-08 15:31:36 +01:00
|
|
|
from lbrynet.core.Wallet import ClaimOutpoint
|
2016-10-20 21:40:35 +02:00
|
|
|
|
2016-09-15 04:27:57 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2017-03-06 22:52:16 +01:00
|
|
|
def log_status(name, sd_hash, status):
|
2017-02-16 15:09:21 +01:00
|
|
|
if status == ManagedEncryptedFileDownloader.STATUS_RUNNING:
|
|
|
|
status_string = "running"
|
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_STOPPED:
|
|
|
|
status_string = "stopped"
|
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_FINISHED:
|
|
|
|
status_string = "finished"
|
|
|
|
else:
|
|
|
|
status_string = "unknown"
|
2017-03-06 22:52:16 +01:00
|
|
|
log.info("lbry://%s (%s) is %s", name, short_hash(sd_hash), status_string)
|
2017-02-16 15:09:21 +01:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class ManagedEncryptedFileDownloader(EncryptedFileSaver):
|
2015-08-20 17:27:15 +02:00
|
|
|
STATUS_RUNNING = "running"
|
|
|
|
STATUS_STOPPED = "stopped"
|
|
|
|
STATUS_FINISHED = "finished"
|
|
|
|
|
2016-11-30 21:20:45 +01:00
|
|
|
def __init__(self, rowid, stream_hash, peer_finder, rate_limiter,
|
|
|
|
blob_manager, stream_info_manager, lbry_file_manager,
|
|
|
|
payment_rate_manager, wallet, download_directory,
|
2016-12-20 03:16:37 +01:00
|
|
|
file_name=None):
|
2016-11-30 21:20:45 +01:00
|
|
|
EncryptedFileSaver.__init__(self, stream_hash, peer_finder,
|
|
|
|
rate_limiter, blob_manager,
|
|
|
|
stream_info_manager,
|
|
|
|
payment_rate_manager, wallet,
|
|
|
|
download_directory,
|
2016-12-20 03:16:37 +01:00
|
|
|
file_name)
|
2016-04-24 10:42:42 +02:00
|
|
|
self.sd_hash = None
|
2016-05-11 01:03:14 +02:00
|
|
|
self.txid = None
|
2016-10-14 08:13:37 +02:00
|
|
|
self.nout = None
|
2017-03-08 15:31:36 +01:00
|
|
|
self.outpoint = None
|
2017-03-06 22:52:16 +01:00
|
|
|
self.name = None
|
2016-08-05 00:44:12 +02:00
|
|
|
self.claim_id = None
|
2016-01-16 07:16:37 +01:00
|
|
|
self.rowid = rowid
|
2015-08-20 17:27:15 +02:00
|
|
|
self.lbry_file_manager = lbry_file_manager
|
2017-01-11 18:53:43 +01:00
|
|
|
self._saving_status = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def saving_status(self):
|
|
|
|
return self._saving_status
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-09 19:21:31 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def restore(self):
|
2017-02-16 15:09:21 +01:00
|
|
|
yield self.load_file_attributes()
|
|
|
|
|
2017-02-09 19:21:31 +01:00
|
|
|
status = yield self.lbry_file_manager.get_lbry_file_status(self)
|
2017-03-06 22:52:16 +01:00
|
|
|
log_status(self.name, self.sd_hash, status)
|
2017-02-16 15:09:21 +01:00
|
|
|
|
2017-02-09 19:21:31 +01:00
|
|
|
if status == ManagedEncryptedFileDownloader.STATUS_RUNNING:
|
2017-02-14 17:25:34 +01:00
|
|
|
# start returns self.finished_deferred
|
|
|
|
# which fires when we've finished downloading the file
|
|
|
|
# and we don't want to wait for the entire download
|
|
|
|
self.start()
|
2017-02-09 19:21:31 +01:00
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_STOPPED:
|
|
|
|
defer.returnValue(False)
|
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_FINISHED:
|
|
|
|
self.completed = True
|
|
|
|
defer.returnValue(True)
|
|
|
|
else:
|
|
|
|
raise Exception("Unknown status for stream %s: %s", self.stream_hash, status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-01-11 18:53:43 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-09-17 07:43:41 +02:00
|
|
|
def stop(self, err=None, change_status=True):
|
2017-02-16 15:09:21 +01:00
|
|
|
log.debug('Stopping download for %s', short_hash(self.sd_hash))
|
2016-10-14 08:13:37 +02:00
|
|
|
# EncryptedFileSaver deletes metadata when it's stopped. We don't want that here.
|
2017-01-11 18:53:43 +01:00
|
|
|
yield EncryptedFileDownloader.stop(self, err=err)
|
2015-08-20 17:27:15 +02:00
|
|
|
if change_status is True:
|
2017-01-11 18:53:43 +01:00
|
|
|
status = yield self._save_status()
|
2017-02-09 19:21:31 +01:00
|
|
|
defer.returnValue(status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-09 19:21:31 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def status(self):
|
2017-02-09 19:21:31 +01:00
|
|
|
blobs = yield self.stream_info_manager.get_blobs_for_stream(self.stream_hash)
|
|
|
|
blob_hashes = [b[0] for b in blobs if b[0] is not None]
|
|
|
|
completed_blobs = yield self.blob_manager.completed_blobs(blob_hashes)
|
|
|
|
num_blobs_completed = len(completed_blobs)
|
|
|
|
num_blobs_known = len(blob_hashes)
|
|
|
|
|
|
|
|
if self.completed:
|
|
|
|
status = "completed"
|
|
|
|
elif self.stopped:
|
|
|
|
status = "stopped"
|
|
|
|
else:
|
|
|
|
status = "running"
|
|
|
|
defer.returnValue(EncryptedFileStatusReport(self.file_name, num_blobs_completed,
|
|
|
|
num_blobs_known, status))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-12-30 07:51:03 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-02-16 15:09:21 +01:00
|
|
|
def load_file_attributes(self):
|
2016-12-30 07:51:03 +01:00
|
|
|
sd_hash = yield self.stream_info_manager.get_sd_blob_hashes_for_stream(self.stream_hash)
|
2017-02-16 15:09:21 +01:00
|
|
|
if sd_hash:
|
2016-12-30 07:51:03 +01:00
|
|
|
self.sd_hash = sd_hash[0]
|
2017-02-16 15:09:21 +01:00
|
|
|
else:
|
2017-02-16 05:39:17 +01:00
|
|
|
raise NoSuchStreamHash(self.stream_hash)
|
2017-02-16 15:09:21 +01:00
|
|
|
stream_metadata = yield self.wallet.get_claim_metadata_for_sd_hash(self.sd_hash)
|
|
|
|
if stream_metadata:
|
|
|
|
name, txid, nout = stream_metadata
|
2017-03-06 22:52:16 +01:00
|
|
|
self.name = name
|
2017-02-16 15:09:21 +01:00
|
|
|
self.txid = txid
|
|
|
|
self.nout = nout
|
2017-03-08 15:31:36 +01:00
|
|
|
self.outpoint = ClaimOutpoint(self.txid, self.nout)
|
2017-02-16 15:09:21 +01:00
|
|
|
else:
|
|
|
|
raise NoSuchSDHash(self.sd_hash)
|
2017-03-06 22:52:16 +01:00
|
|
|
self.claim_id = yield self.wallet.get_claimid(self.name, self.txid, self.nout)
|
2017-02-16 15:09:21 +01:00
|
|
|
defer.returnValue(None)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _start(self):
|
|
|
|
yield EncryptedFileSaver._start(self)
|
|
|
|
yield self.load_file_attributes()
|
2016-12-30 07:51:03 +01:00
|
|
|
status = yield self._save_status()
|
2017-03-06 22:52:16 +01:00
|
|
|
log_status(self.name, self.sd_hash, status)
|
2016-12-30 07:51:03 +01:00
|
|
|
defer.returnValue(status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _get_finished_deferred_callback_value(self):
|
|
|
|
if self.completed is True:
|
|
|
|
return "Download successful"
|
|
|
|
else:
|
|
|
|
return "Download stopped"
|
|
|
|
|
2017-01-13 15:16:42 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def _save_status(self):
|
2017-01-13 15:16:42 +01:00
|
|
|
self._saving_status = True
|
2015-08-20 17:27:15 +02:00
|
|
|
if self.completed is True:
|
2017-01-13 15:16:42 +01:00
|
|
|
status = ManagedEncryptedFileDownloader.STATUS_FINISHED
|
2015-08-20 17:27:15 +02:00
|
|
|
elif self.stopped is True:
|
2017-01-13 15:16:42 +01:00
|
|
|
status = ManagedEncryptedFileDownloader.STATUS_STOPPED
|
2015-08-20 17:27:15 +02:00
|
|
|
else:
|
2017-01-13 15:16:42 +01:00
|
|
|
status = ManagedEncryptedFileDownloader.STATUS_RUNNING
|
2017-02-09 19:21:31 +01:00
|
|
|
status = yield self.lbry_file_manager.change_lbry_file_status(self, status)
|
2017-01-13 15:16:42 +01:00
|
|
|
self._saving_status = False
|
2017-02-09 19:21:31 +01:00
|
|
|
defer.returnValue(status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-16 15:09:21 +01:00
|
|
|
def save_status(self):
|
|
|
|
return self._save_status()
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def _get_progress_manager(self, download_manager):
|
2016-11-30 21:20:45 +01:00
|
|
|
return FullStreamProgressManager(self._finished_downloading,
|
|
|
|
self.blob_manager, download_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
class ManagedEncryptedFileDownloaderFactory(object):
|
2015-08-20 17:27:15 +02:00
|
|
|
implements(IStreamDownloaderFactory)
|
|
|
|
|
|
|
|
def __init__(self, lbry_file_manager):
|
|
|
|
self.lbry_file_manager = lbry_file_manager
|
|
|
|
|
2015-08-27 21:41:17 +02:00
|
|
|
def can_download(self, sd_validator):
|
2017-02-09 19:21:31 +01:00
|
|
|
# TODO: add a sd_validator for non live streams, use it
|
2015-08-27 21:41:17 +02:00
|
|
|
return True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-09 19:21:31 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-02-14 20:18:42 +01:00
|
|
|
def make_downloader(self, metadata, options, payment_rate_manager, download_directory=None,
|
2017-02-09 19:21:31 +01:00
|
|
|
file_name=None):
|
2017-02-14 20:18:42 +01:00
|
|
|
assert len(options) == 1
|
|
|
|
data_rate = options[0]
|
2017-02-09 19:21:31 +01:00
|
|
|
stream_hash = yield save_sd_info(self.lbry_file_manager.stream_info_manager,
|
|
|
|
metadata.validator.raw_info)
|
|
|
|
if metadata.metadata_source == StreamMetadata.FROM_BLOB:
|
|
|
|
yield self.lbry_file_manager.save_sd_blob_hash_to_stream(stream_hash,
|
|
|
|
metadata.source_blob_hash)
|
|
|
|
lbry_file = yield self.lbry_file_manager.add_lbry_file(stream_hash, payment_rate_manager,
|
2016-12-20 03:16:37 +01:00
|
|
|
data_rate,
|
2017-02-09 19:21:31 +01:00
|
|
|
download_directory, file_name)
|
|
|
|
defer.returnValue(lbry_file)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-22 04:06:04 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_description():
|
2016-10-14 08:13:37 +02:00
|
|
|
return "Save the file to disk"
|