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
|
2018-08-10 23:23:50 +02:00
|
|
|
from binascii import hexlify, unhexlify
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-10-28 20:22:37 +02:00
|
|
|
from twisted.internet import defer
|
2019-01-21 21:55:50 +01:00
|
|
|
from lbrynet.conf import Config
|
2018-12-15 21:29:25 +01:00
|
|
|
from lbrynet.extras.compat import f2d
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.client.StreamProgressManager import FullStreamProgressManager
|
|
|
|
from lbrynet.p2p.HTTPBlobDownloader import HTTPBlobDownloader
|
2018-11-07 21:15:05 +01:00
|
|
|
from lbrynet.utils import short_hash
|
2018-11-04 20:55:01 +01:00
|
|
|
from lbrynet.blob.client.EncryptedFileDownloader import EncryptedFileSaver
|
|
|
|
from lbrynet.blob.EncryptedFileStatusReport import EncryptedFileStatusReport
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.StreamDescriptor import save_sd_info
|
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-12-06 21:08:13 +01:00
|
|
|
def log_status(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-12-29 20:09:21 +01:00
|
|
|
log.debug("stream %s is %s", 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"
|
2017-11-10 18:54:33 +01:00
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
def __init__(self, conf: Config, rowid, stream_hash, peer_finder, rate_limiter, blob_manager, storage,
|
|
|
|
lbry_file_manager, payment_rate_manager, wallet, download_directory, file_name, stream_name,
|
|
|
|
sd_hash, key, suggested_file_name, download_mirrors=None):
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(
|
2019-01-21 21:55:50 +01:00
|
|
|
conf, stream_hash, peer_finder, rate_limiter, blob_manager, storage, payment_rate_manager,
|
|
|
|
wallet, download_directory, key, stream_name, file_name
|
2018-02-12 20:03:39 +01:00
|
|
|
)
|
2017-12-29 20:09:54 +01:00
|
|
|
self.sd_hash = sd_hash
|
2016-01-16 07:16:37 +01:00
|
|
|
self.rowid = rowid
|
2018-08-10 23:23:50 +02:00
|
|
|
self.suggested_file_name = unhexlify(suggested_file_name).decode()
|
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
|
2018-02-12 20:03:39 +01:00
|
|
|
self.claim_id = None
|
|
|
|
self.outpoint = None
|
|
|
|
self.claim_name = None
|
|
|
|
self.txid = None
|
|
|
|
self.nout = None
|
|
|
|
self.channel_claim_id = None
|
|
|
|
self.channel_name = None
|
|
|
|
self.metadata = None
|
2018-08-02 20:33:03 +02:00
|
|
|
self.mirror = None
|
2019-01-21 21:55:50 +01:00
|
|
|
if download_mirrors or conf.download_mirrors:
|
2018-08-02 20:33:03 +02:00
|
|
|
self.mirror = HTTPBlobDownloader(
|
2019-01-21 21:55:50 +01:00
|
|
|
self.blob_manager, servers=download_mirrors or conf.download_mirrors
|
2018-08-02 20:33:03 +02:00
|
|
|
)
|
2018-02-12 20:03:39 +01:00
|
|
|
|
2018-05-09 15:50:44 +02:00
|
|
|
def set_claim_info(self, claim_info):
|
|
|
|
self.claim_id = claim_info['claim_id']
|
|
|
|
self.txid = claim_info['txid']
|
|
|
|
self.nout = claim_info['nout']
|
|
|
|
self.channel_claim_id = claim_info['channel_claim_id']
|
|
|
|
self.outpoint = "%s:%i" % (self.txid, self.nout)
|
|
|
|
self.claim_name = claim_info['name']
|
|
|
|
self.channel_name = claim_info['channel_name']
|
|
|
|
self.metadata = claim_info['value']['stream']['metadata']
|
|
|
|
|
2018-12-15 21:29:25 +01:00
|
|
|
async def get_claim_info(self, include_supports=True):
|
|
|
|
claim_info = await self.storage.get_content_claim(self.stream_hash, include_supports)
|
2018-02-12 20:03:39 +01:00
|
|
|
if claim_info:
|
2018-05-09 15:50:44 +02:00
|
|
|
self.set_claim_info(claim_info)
|
2018-12-15 21:29:25 +01:00
|
|
|
return claim_info
|
2017-01-11 18:53:43 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def saving_status(self):
|
|
|
|
return self._saving_status
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-12-29 20:10:16 +01:00
|
|
|
def restore(self, status):
|
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:
|
2017-12-29 20:10:16 +01:00
|
|
|
pass
|
2017-02-09 19:21:31 +01:00
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_FINISHED:
|
|
|
|
self.completed = True
|
|
|
|
else:
|
2018-10-18 12:42:45 +02:00
|
|
|
raise Exception(f"Unknown status for stream {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-06-15 20:31:02 +02:00
|
|
|
log.debug('Stopping download for stream %s', short_hash(self.stream_hash))
|
2018-06-10 09:57:06 +02:00
|
|
|
if self.mirror:
|
|
|
|
self.mirror.stop()
|
2016-10-14 08:13:37 +02:00
|
|
|
# EncryptedFileSaver deletes metadata when it's stopped. We don't want that here.
|
2019-01-21 21:55:50 +01:00
|
|
|
yield super().stop(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()
|
2018-08-02 20:33:03 +02:00
|
|
|
defer.returnValue(status)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
async def status(self):
|
|
|
|
blobs = await self.storage.get_blobs_for_stream(self.stream_hash)
|
2018-02-12 20:03:39 +01:00
|
|
|
blob_hashes = [b.blob_hash for b in blobs if b.blob_hash is not None]
|
2019-01-07 08:52:53 +01:00
|
|
|
completed_blobs = self.blob_manager.completed_blobs(blob_hashes)
|
2017-02-09 19:21:31 +01:00
|
|
|
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"
|
2019-01-07 08:52:53 +01:00
|
|
|
|
|
|
|
return EncryptedFileStatusReport(
|
2018-02-12 20:03:39 +01:00
|
|
|
self.file_name, num_blobs_completed, num_blobs_known, status
|
2019-01-07 08:52:53 +01:00
|
|
|
)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-02-16 15:09:21 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _start(self):
|
|
|
|
yield EncryptedFileSaver._start(self)
|
2016-12-30 07:51:03 +01:00
|
|
|
status = yield self._save_status()
|
2017-12-06 21:08:13 +01:00
|
|
|
log_status(self.sd_hash, status)
|
2018-06-10 09:57:06 +02:00
|
|
|
if self.mirror:
|
2018-08-09 19:19:59 +02:00
|
|
|
self.mirror.download_stream(self.stream_hash, self.sd_hash)
|
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
|
2019-01-07 21:35:03 +01:00
|
|
|
return 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
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class ManagedEncryptedFileDownloaderFactory:
|
2018-07-03 06:51:25 +02:00
|
|
|
#implements(IStreamDownloaderFactory)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2018-08-02 20:33:03 +02:00
|
|
|
def __init__(self, lbry_file_manager, blob_manager):
|
2015-08-20 17:27:15 +02:00
|
|
|
self.lbry_file_manager = lbry_file_manager
|
2018-08-02 20:33:03 +02:00
|
|
|
self.blob_manager = blob_manager
|
2015-08-20 17:27:15 +02:00
|
|
|
|
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
|
2018-08-02 20:33:03 +02:00
|
|
|
def make_downloader(self, metadata, data_rate, payment_rate_manager, download_directory, file_name=None,
|
|
|
|
download_mirrors=None):
|
|
|
|
stream_hash = yield save_sd_info(self.blob_manager,
|
2018-02-12 20:03:39 +01:00
|
|
|
metadata.source_blob_hash,
|
2017-02-09 19:21:31 +01:00
|
|
|
metadata.validator.raw_info)
|
2018-02-12 20:03:39 +01:00
|
|
|
if file_name:
|
2018-08-10 23:23:50 +02:00
|
|
|
file_name = hexlify(file_name.encode())
|
|
|
|
hex_download_directory = hexlify(download_directory.encode())
|
2018-12-15 21:29:25 +01:00
|
|
|
lbry_file = yield f2d(self.lbry_file_manager.add_downloaded_file(
|
2018-08-10 23:23:50 +02:00
|
|
|
stream_hash, metadata.source_blob_hash, hex_download_directory, payment_rate_manager,
|
2018-08-02 20:33:03 +02:00
|
|
|
data_rate, file_name=file_name, download_mirrors=download_mirrors
|
2018-12-15 21:29:25 +01:00
|
|
|
))
|
2017-02-09 19:21:31 +01:00
|
|
|
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"
|