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 random
|
|
|
|
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
|
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
|
2016-10-28 21:48:51 +02:00
|
|
|
from lbrynet.reflector import reupload
|
2016-10-28 22:47:45 +02:00
|
|
|
from lbrynet.conf import settings
|
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
|
|
|
|
|
|
|
|
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,
|
|
|
|
upload_allowed, file_name=None):
|
|
|
|
EncryptedFileSaver.__init__(self, stream_hash, peer_finder,
|
|
|
|
rate_limiter, blob_manager,
|
|
|
|
stream_info_manager,
|
|
|
|
payment_rate_manager, wallet,
|
|
|
|
download_directory,
|
2016-10-14 08:13:37 +02:00
|
|
|
upload_allowed, 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
|
2016-05-11 01:03:14 +02:00
|
|
|
self.uri = 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
|
|
|
|
self.saving_status = False
|
|
|
|
|
|
|
|
def restore(self):
|
2016-04-24 10:42:42 +02:00
|
|
|
d = self.stream_info_manager._get_sd_blob_hashes_for_stream(self.stream_hash)
|
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
def _save_stream_info(sd_hash):
|
|
|
|
if sd_hash:
|
2016-05-05 05:27:40 +02:00
|
|
|
self.sd_hash = sd_hash[0]
|
2016-10-03 22:37:27 +02:00
|
|
|
d = self.wallet.get_claim_metadata_for_sd_hash(self.sd_hash)
|
2016-10-14 08:13:37 +02:00
|
|
|
d.addCallback(lambda r: _save_claim(r[0], r[1], r[2]))
|
|
|
|
return d
|
2016-05-11 01:03:14 +02:00
|
|
|
else:
|
2016-10-14 08:13:37 +02:00
|
|
|
return None
|
2016-05-11 01:03:14 +02:00
|
|
|
|
2016-08-05 00:44:12 +02:00
|
|
|
def _save_claim_id(claim_id):
|
|
|
|
self.claim_id = claim_id
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
def _notify_bad_claim(name, txid, nout):
|
|
|
|
err_msg = "Error loading name claim for lbry file: \
|
|
|
|
lbry://%s, tx %s output %i does not contain a valid claim, deleting it"
|
|
|
|
log.error(err_msg, name, txid, nout)
|
2016-09-15 04:27:57 +02:00
|
|
|
return self.lbry_file_manager.delete_lbry_file(self)
|
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
def _save_claim(name, txid, nout):
|
2016-05-11 01:03:14 +02:00
|
|
|
self.uri = name
|
|
|
|
self.txid = txid
|
2016-10-14 08:13:37 +02:00
|
|
|
self.nout = nout
|
|
|
|
d = self.wallet.get_claimid(name, txid, nout)
|
|
|
|
d.addCallbacks(_save_claim_id, lambda err: _notify_bad_claim(name, txid, nout))
|
2016-08-05 00:44:12 +02:00
|
|
|
return d
|
2016-04-24 10:42:42 +02:00
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
d.addCallback(_save_stream_info)
|
2016-12-14 17:33:14 +01:00
|
|
|
d.addCallback(lambda _: self._reupload())
|
2016-04-24 10:42:42 +02:00
|
|
|
d.addCallback(lambda _: self.lbry_file_manager.get_lbry_file_status(self))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def restore_status(status):
|
2016-09-27 20:18:16 +02:00
|
|
|
if status == ManagedEncryptedFileDownloader.STATUS_RUNNING:
|
2015-08-20 17:27:15 +02:00
|
|
|
return self.start()
|
2016-09-27 20:18:16 +02:00
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_STOPPED:
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed(False)
|
2016-09-27 20:18:16 +02:00
|
|
|
elif status == ManagedEncryptedFileDownloader.STATUS_FINISHED:
|
2015-08-20 17:27:15 +02:00
|
|
|
self.completed = True
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
d.addCallback(restore_status)
|
|
|
|
return d
|
|
|
|
|
2016-12-14 17:33:14 +01:00
|
|
|
def _reupload(self):
|
|
|
|
if not settings.reflector_reupload:
|
|
|
|
return
|
|
|
|
reflector_server = random.choice(settings.reflector_servers)
|
|
|
|
return reupload.check_and_restore_availability(self, reflector_server)
|
|
|
|
|
2015-09-17 07:43:41 +02:00
|
|
|
def stop(self, err=None, change_status=True):
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def set_saving_status_done():
|
|
|
|
self.saving_status = False
|
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
# EncryptedFileSaver deletes metadata when it's stopped. We don't want that here.
|
|
|
|
d = EncryptedFileDownloader.stop(self, err=err)
|
2015-08-20 17:27:15 +02:00
|
|
|
if change_status is True:
|
|
|
|
self.saving_status = True
|
|
|
|
d.addCallback(lambda _: self._save_status())
|
|
|
|
d.addCallback(lambda _: set_saving_status_done())
|
|
|
|
return d
|
|
|
|
|
|
|
|
def status(self):
|
|
|
|
def find_completed_blobhashes(blobs):
|
|
|
|
blobhashes = [b[0] for b in blobs if b[0] is not None]
|
|
|
|
|
|
|
|
def get_num_completed(completed_blobs):
|
|
|
|
return len(completed_blobs), len(blobhashes)
|
|
|
|
|
|
|
|
inner_d = self.blob_manager.completed_blobs(blobhashes)
|
|
|
|
inner_d.addCallback(get_num_completed)
|
|
|
|
return inner_d
|
|
|
|
|
|
|
|
def make_full_status(progress):
|
|
|
|
num_completed = progress[0]
|
|
|
|
num_known = progress[1]
|
|
|
|
if self.completed is True:
|
|
|
|
s = "completed"
|
|
|
|
elif self.stopped is True:
|
|
|
|
s = "stopped"
|
|
|
|
else:
|
|
|
|
s = "running"
|
2016-09-27 20:18:16 +02:00
|
|
|
status = EncryptedFileStatusReport(self.file_name, num_completed, num_known, s)
|
2015-08-20 17:27:15 +02:00
|
|
|
return status
|
|
|
|
|
|
|
|
d = self.stream_info_manager.get_blobs_for_stream(self.stream_hash)
|
|
|
|
d.addCallback(find_completed_blobhashes)
|
|
|
|
d.addCallback(make_full_status)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _start(self):
|
|
|
|
|
2016-09-27 20:18:16 +02:00
|
|
|
d = EncryptedFileSaver._start(self)
|
2016-11-30 21:20:45 +01:00
|
|
|
d.addCallback(
|
|
|
|
lambda _: self.stream_info_manager.get_sd_blob_hashes_for_stream(self.stream_hash))
|
2016-04-24 10:42:42 +02:00
|
|
|
|
|
|
|
def _save_sd_hash(sd_hash):
|
2016-05-11 02:47:35 +02:00
|
|
|
if len(sd_hash):
|
|
|
|
self.sd_hash = sd_hash[0]
|
2016-10-03 22:37:27 +02:00
|
|
|
d = self.wallet.get_claim_metadata_for_sd_hash(self.sd_hash)
|
2016-05-11 02:47:35 +02:00
|
|
|
else:
|
|
|
|
d = defer.succeed(None)
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
2016-10-14 08:13:37 +02:00
|
|
|
def _save_claim(name, txid, nout):
|
2016-05-11 02:47:35 +02:00
|
|
|
self.uri = name
|
|
|
|
self.txid = txid
|
2016-11-30 21:20:45 +01:00
|
|
|
self.nout = nout
|
2016-04-24 10:42:42 +02:00
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
d.addCallback(_save_sd_hash)
|
2016-10-14 08:13:37 +02:00
|
|
|
d.addCallback(lambda r: _save_claim(r[0], r[1], r[2]) if r else None)
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._save_status())
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _get_finished_deferred_callback_value(self):
|
|
|
|
if self.completed is True:
|
|
|
|
return "Download successful"
|
|
|
|
else:
|
|
|
|
return "Download stopped"
|
|
|
|
|
|
|
|
def _save_status(self):
|
|
|
|
if self.completed is True:
|
2016-09-27 20:18:16 +02:00
|
|
|
s = ManagedEncryptedFileDownloader.STATUS_FINISHED
|
2015-08-20 17:27:15 +02:00
|
|
|
elif self.stopped is True:
|
2016-09-27 20:18:16 +02:00
|
|
|
s = ManagedEncryptedFileDownloader.STATUS_STOPPED
|
2015-08-20 17:27:15 +02:00
|
|
|
else:
|
2016-09-27 20:18:16 +02:00
|
|
|
s = ManagedEncryptedFileDownloader.STATUS_RUNNING
|
2016-01-16 07:16:37 +01:00
|
|
|
return self.lbry_file_manager.change_lbry_file_status(self, s)
|
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):
|
|
|
|
return True
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-11-30 21:20:45 +01:00
|
|
|
def make_downloader(self, metadata, options, payment_rate_manager,
|
|
|
|
download_directory=None, file_name=None):
|
2015-08-20 17:27:15 +02:00
|
|
|
data_rate = options[0]
|
|
|
|
upload_allowed = options[1]
|
|
|
|
|
2015-09-22 04:06:04 +02:00
|
|
|
def save_source_if_blob(stream_hash):
|
|
|
|
if metadata.metadata_source == StreamMetadata.FROM_BLOB:
|
2016-11-30 21:20:45 +01:00
|
|
|
# TODO: should never have to dig this deep into a another classes
|
|
|
|
# members. lbry_file_manager should have a
|
|
|
|
# save_sd_blob_hash_to_stream method
|
|
|
|
d = self.lbry_file_manager.stream_info_manager.save_sd_blob_hash_to_stream(
|
|
|
|
stream_hash, metadata.source_blob_hash)
|
2015-09-22 04:06:04 +02:00
|
|
|
else:
|
|
|
|
d = defer.succeed(True)
|
|
|
|
d.addCallback(lambda _: stream_hash)
|
|
|
|
return d
|
|
|
|
|
|
|
|
d = save_sd_info(self.lbry_file_manager.stream_info_manager, metadata.validator.raw_info)
|
|
|
|
d.addCallback(save_source_if_blob)
|
2016-11-30 21:20:45 +01:00
|
|
|
d.addCallback(lambda stream_hash: self.lbry_file_manager.add_lbry_file(
|
|
|
|
stream_hash,
|
|
|
|
payment_rate_manager,
|
|
|
|
data_rate,
|
|
|
|
upload_allowed,
|
|
|
|
download_directory=download_directory,
|
|
|
|
file_name=file_name))
|
2015-08-20 17:27:15 +02:00
|
|
|
return d
|
|
|
|
|
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"
|