lbry-sdk/scripts/download_blob_from_peer.py

54 lines
1.7 KiB
Python
Raw Permalink Normal View History

2019-01-29 04:41:16 +01:00
import sys
import os
import asyncio
import socket
2019-01-31 18:28:59 +01:00
import ipaddress
from lbry.conf import Config
from lbry.extras.daemon.storage import SQLiteStorage
from lbry.blob.blob_manager import BlobManager
from lbry.blob_exchange.client import BlobExchangeClientProtocol, request_blob
2019-01-29 04:41:16 +01:00
import logging
log = logging.getLogger("lbry")
2019-01-29 04:41:16 +01:00
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
async def main(blob_hash: str, url: str):
conf = Config()
loop = asyncio.get_running_loop()
host_url, port = url.split(":")
2019-01-31 18:28:59 +01:00
try:
host = None
if ipaddress.ip_address(host_url):
host = host_url
except ValueError:
host = None
if not host:
host_info = await loop.getaddrinfo(
host_url, 'https',
proto=socket.IPPROTO_TCP,
)
host = host_info[0][4][0]
2019-01-29 04:41:16 +01:00
storage = SQLiteStorage(conf, os.path.join(conf.data_dir, "lbrynet.sqlite"))
2019-10-17 20:10:30 +02:00
blob_manager = BlobManager(loop, os.path.join(conf.data_dir, "blobfiles"), storage, conf)
2019-01-29 04:41:16 +01:00
await storage.open()
await blob_manager.setup()
blob = blob_manager.get_blob(blob_hash)
2019-01-31 18:28:59 +01:00
success, keep = await request_blob(loop, blob, host, int(port), conf.peer_connect_timeout,
conf.blob_download_timeout)
print(f"{'downloaded' if success else 'failed to download'} {blob_hash} from {host}:{port}\n"
f"keep connection: {keep}")
2019-01-29 04:41:16 +01:00
if blob.get_is_verified():
await blob_manager.delete_blobs([blob.blob_hash])
2019-01-31 18:28:59 +01:00
print(f"deleted {blob_hash}")
2019-01-29 04:41:16 +01:00
if __name__ == "__main__": # usage: python download_blob_from_peer.py <blob_hash> [host url:port]
url = 'reflector.lbry.com:5567'
2019-01-29 04:41:16 +01:00
if len(sys.argv) > 2:
url = sys.argv[2]
asyncio.run(main(sys.argv[1], url))