2021-10-24 22:25:27 +02:00
|
|
|
import asyncio
|
2021-11-09 19:16:21 +01:00
|
|
|
import logging
|
2021-10-24 22:25:27 +02:00
|
|
|
|
|
|
|
from lbry.stream.downloader import StreamDownloader
|
|
|
|
|
|
|
|
|
2021-11-09 19:16:21 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-24 22:25:27 +02:00
|
|
|
class BackgroundDownloader:
|
2021-10-24 23:19:06 +02:00
|
|
|
def __init__(self, conf, storage, blob_manager, dht_node=None):
|
2021-10-24 22:25:27 +02:00
|
|
|
self.storage = storage
|
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.node = dht_node
|
|
|
|
self.conf = conf
|
|
|
|
|
|
|
|
async def download_blobs(self, sd_hash):
|
|
|
|
downloader = StreamDownloader(asyncio.get_running_loop(), self.conf, self.blob_manager, sd_hash)
|
|
|
|
try:
|
|
|
|
await downloader.start(self.node, save_stream=False)
|
2021-11-03 19:51:51 +01:00
|
|
|
for blob_info in downloader.descriptor.blobs[:-1]:
|
|
|
|
await downloader.download_stream_blob(blob_info)
|
2021-10-24 22:25:27 +02:00
|
|
|
except ValueError:
|
|
|
|
return
|
2021-11-09 19:16:21 +01:00
|
|
|
except asyncio.CancelledError:
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
log.error("Unexpected download error on background downloader")
|
2021-11-03 19:51:51 +01:00
|
|
|
finally:
|
|
|
|
downloader.stop()
|