Adds a command to automatically download files as names are claimed

Defaults on only trying to download wonderfullife
This commit is contained in:
Jack 2015-11-12 00:03:49 -05:00
parent a1e827fb3b
commit 657c1a4626

View file

@ -415,6 +415,7 @@ class AddStream(CommandHandler):
self.payment_rate_manager = PaymentRateManager(base_payment_rate_manager)
def start(self):
self.console.sendLine("Starting addstream...")
self.console.sendLine(self.cancel_prompt)
self.loading_metadata_deferred.addCallback(self._handle_metadata)
self.loading_metadata_deferred.addErrback(self._handle_load_canceled)
@ -649,6 +650,7 @@ class AddStream(CommandHandler):
return choice_string
def _start_download(self):
print 'Starting addstream._start_download'
d = self._make_downloader()
d.addCallback(lambda stream_downloader: stream_downloader.start())
d.addErrback(self._handle_download_error)
@ -662,6 +664,7 @@ class AddStream(CommandHandler):
self.console.sendLine("An unexpected error has caused the download to stop. See console.log for details.")
def _make_downloader(self):
print 'Making downloader'
return self.factory.make_downloader(self.metadata, self.options_chosen,
self.payment_rate_manager)
@ -693,6 +696,7 @@ class AddStreamFromHash(AddStream):
self.session = session
def start(self, sd_hash):
print 'Starting addstreamfromhash'
self.loading_metadata_deferred = download_sd_blob(self.session, sd_hash,
self.payment_rate_manager)
self.loading_metadata_deferred.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
@ -736,12 +740,14 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
self.name = None
def start(self, name):
print 'Starting addstreamfromlbrycrdname'
self.name = name
self.loading_metadata_deferred = self._resolve_name(name)
self.loading_metadata_deferred.addCallback(lambda stream_hash: download_sd_blob(self.session,
stream_hash,
self.payment_rate_manager))
self.loading_metadata_deferred.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
print 'Sending addstream.start from crdname'
AddStream.start(self)
def _resolve_name(self, name):
@ -759,6 +765,7 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
return stream_info['stream_hash']
d = self.wallet.get_stream_info_for_name(name)
d.addCallback(get_name_from_info)
print 'Resolved name'
return d
def _handle_load_failed(self, err):
@ -780,11 +787,13 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
return AddStreamFromHash._handle_load_failed(self, err)
def _start_download(self):
print 'Startind download in addstreamlbrycrd'
d = self._pay_key_fee()
d.addCallback(lambda _: AddStream._start_download(self))
return d
def _pay_key_fee(self):
print 'Paying kee fee'
if self.key_fee is not None and self.key_fee_address is not None:
reserved_points = self.wallet.reserve_points(self.key_fee_address, self.key_fee)
if reserved_points is None:
@ -2032,6 +2041,7 @@ class ImmediateAnnounceAllBlobs(CommandHandler):
class ImmediateAnnounceAllBlobsFactory(CommandHandlerFactory):
control_handler_class = ImmediateAnnounceAllBlobs
command = "announce-blobs"
short_help = "Announce all blobs to the dht"
full_help = "Immediately re-broadcast all hashes associated with the server to " \
"the distributed hash table."
@ -2360,20 +2370,28 @@ class StatusFactory(CommandHandlerFactory):
class AutoAddStream(object):
def __init__(self, sd_identifier, base_payment_rate_manager, lbry_file_manager):
self.lbry_file_manager = lbry_file_manager
self.sd_identifier = sd_identifier
def __init__(self):
self.console = None
self.session = None
self.loading_metadata_deferred = None
self.finished_deferred = defer.Deferred()
self.lbry_file_manager = None
self.sd_identifier = None
self.finished_deferred = None
self.metadata = None
self.downloader = None
self.loading_failed = False
self.payment_rate_manager = PaymentRateManager(base_payment_rate_manager)
self.factory = None
self.description = None
self.key_fee = None
self.key_fee_address = None
self.payment_rate_manager = None
def start(self):
self.console.sendLine("Started autoaddstream")
self.loading_metadata_deferred.addCallback(self._handle_metadata)
self.loading_metadata_deferred.addErrback(self._handle_load_canceled)
self.loading_metadata_deferred.addErrback(self._handle_load_failed)
self.console.sendLine("AutoAddStream starting download")
self._start_download()
def _load_metadata(self, sd_file):
@ -2385,36 +2403,41 @@ class AutoAddStream(object):
def _handle_load_failed(self, err):
self.loading_failed = True
self.console.sendLine("handle load failed: " + str(err.getTraceback()))
log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback())
self.finished_deferred.callback(None)
def _handle_metadata(self, metadata):
self.loading_metadata_deferred = None
self.console.sendLine("Metadata: " + str(metadata))
self.metadata = metadata
self.factory = self.metadata.factories[0]
def _start_download(self):
self.console.sendLine("Autoaddstream _start_download, making downloader")
d = self._make_downloader()
self.console.sendLine("Autoaddstream starting download")
d.addCallback(lambda stream_downloader: stream_downloader.start())
d.addErrback(self._handle_download_error)
return d
def _handle_download_error(self, err):
if err.check(InsufficientFundsError):
print "Download stopped due to insufficient funds."
self.console.sendLine("Download stopped due to insufficient funds.")
else:
print "An unexpected error has caused the download to stop: %s" % err.getTraceback()
self.console.sendLine("Autoaddstream: An unexpected error has caused the download to stop: %s" % err.getTraceback())
#TODO fix this, shouldn't be a LBRYFileManager object, it doesn't have a make_downloader method
def _make_downloader(self):
return self.lbry_file_manager.make_downloader(self.metadata, [0.5, True], self.payment_rate_manager)
self.console.sendLine("making downloader, factory: " + str(self.factory))
self.downloader = self.factory.make_downloader(self.metadata, [0.5, True], self.payment_rate_manager)
return defer.succeed(self.downloader)
class AutoAddStreamFromHash(AutoAddStream):
def __init__(self, sd_identifier, session, lbry_file_manager):
AutoAddStream.__init__(self, sd_identifier, session.base_payment_rate_manager, lbry_file_manager)
self.session = session
def __init__(self):
AutoAddStream.__init__(self)
def start(self, sd_hash):
self.console.sendLine("Started AutoAddStreamFromHash")
self.loading_metadata_deferred = download_sd_blob(self.session, sd_hash,
self.payment_rate_manager)
self.loading_metadata_deferred.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
@ -2432,27 +2455,39 @@ class AutoAddStreamFromHash(AutoAddStream):
class AutoAddStreamFromLBRYcrdName(AutoAddStreamFromHash):
def __init__(self, sd_identifier, session, wallet, lbry_file_manager):
AutoAddStreamFromHash.__init__(self, sd_identifier, session, lbry_file_manager)
def __init__(self, console, sd_identifier, session, wallet, lbry_file_manager):
AutoAddStreamFromHash.__init__(self)
self.wallet = wallet
self.resolved_name = None
self.description = None
self.key_fee = None
self.key_fee_address = None
self.name = None
self.session = session
self.payment_rate_manager = PaymentRateManager(self.session.base_payment_rate_manager)
self.console = console
self.loading_metadata_deferred = None
self.lbry_file_manager = lbry_file_manager
self.sd_identifier = sd_identifier
self.finished_deferred = None
self.metadata = None
self.loading_failed = False
def start(self, name):
self.console.sendLine("Started AutoAddStreamFromLBRYcrdName, file: " + name)
self.name = name
self.loading_metadata_deferred = self._resolve_name(name)
self.loading_metadata_deferred.addCallback(lambda stream_hash: download_sd_blob(self.session,
stream_hash,
self.payment_rate_manager))
self.loading_metadata_deferred.addCallback(self.sd_identifier.get_metadata_for_sd_blob)
log.error("Starting AutoAddStream")
AutoAddStream.start(self)
self.console.sendLine("Starting autoaddstream...")
self.loading_metadata_deferred.addCallback(AutoAddStream.start(self))
self.loading_metadata_deferred.addCallback(self._start_download())
def _resolve_name(self, name):
def get_name_from_info(stream_info):
self.console.sendLine("1 Stream info: " + str(stream_info))
if 'stream_hash' not in stream_info:
raise InvalidStreamInfoError(name)
self.resolved_name = stream_info.get('name', None)
@ -2463,8 +2498,10 @@ class AutoAddStreamFromLBRYcrdName(AutoAddStreamFromHash):
except ValueError:
self.key_fee = None
self.key_fee_address = stream_info.get('key_fee_address', None)
self.stream_hash = stream_info['stream_hash']
return stream_info['stream_hash']
d = self.wallet.get_stream_info_for_name(name)
self.console.sendLine("2 Stream info: " + str(d))
d.addCallback(get_name_from_info)
return d
@ -2485,6 +2522,7 @@ class AutoAddStreamFromLBRYcrdName(AutoAddStreamFromHash):
return AutoAddStreamFromHash._handle_load_failed(self, err)
def _start_download(self):
self.console.sendLine("crd name _start_download")
d = self._pay_key_fee()
d.addCallback(lambda _: AutoAddStream._start_download(self))
return d
@ -2495,73 +2533,67 @@ class AutoAddStreamFromLBRYcrdName(AutoAddStreamFromHash):
if reserved_points is None:
return defer.fail(InsufficientFundsError())
return self.wallet.send_points_to_address(reserved_points, self.key_fee)
self.console.sendLine("Sent key fee" + str(self.key_fee_address) + " | " + str(self.key_fee))
return defer.succeed(True)
#TODO see problem on line 2408
class AutoFetcher(CommandHandler):
def __init__(self, console, session, lbry_file_manager, lbry_file_metadata_manager, wallet, sd_identifier):
CommandHandler.__init__(self, console)
self.d = None
self.console = console
self.sd_identifier = sd_identifier
self.wallet = wallet
self.session = session
self.lbry_file_manager = lbry_file_manager
self.lbry_metadata_manager = lbry_file_metadata_manager
self.seen = []
settings = self.session.wallet.get_rpc_conf()
rpc_user = settings["username"]
rpc_pass = settings["password"]
rpc_port = settings["rpc_port"]
rpc_url = "127.0.0.1"
rpc_conn_string = "http://%s:%s@%s:%s" % (rpc_user, rpc_pass, rpc_url, str(rpc_port))
self.rpc_conn = AuthServiceProxy(rpc_conn_string)
self.lastbestblock = None
self.rpc_conn = self.wallet.get_rpc_conn_x()
def start(self):
self._listen_for_names()
self.d = self._get_names()
self.d.addCallback(self._download_claims)
self.d.addErrback(self.finished_deferred.callback(None))
def _listen_for_names(self):
l = 0
firstrun = True
while True:
def _get_names(self):
c = self.rpc_conn.getblockchaininfo()
if l != c:
if self.lastbestblock != c:
block = self.rpc_conn.getblock(c['bestblockhash'])
txids = block['tx']
transactions = [self.rpc_conn.decoderawtransaction(self.rpc_conn.getrawtransaction(t)) for t in txids]
claimflag = False
claims = None
nmsg = []
tmsg = []
rtn = []
for t in transactions:
claims = self.rpc_conn.getclaimsfortx(t['txid'])
if firstrun:
claims = [{'name': 'wonderfullife'}]
#claims = self.rpc_conn.getclaimsfortx(t['txid'])
claims = self.rpc_conn.getclaimsfortx("c3684bd587856ba5cc38c4afdbcd2c6efc60cb2d1ed21188485ea58048b419a8")
if claims:
for claim in claims:
if claim not in self.seen:
print claim
print '* Trying to get claim', claim['name']
get_name = AutoAddStreamFromLBRYcrdName(self.sd_identifier, self.session,
self.wallet, self.lbry_file_manager)
get_name.start(claim['name'])
nmsg.append(claim['name'])
#self.console.sendLine(str(claim))
self.console.sendLine("lbry://" + str(claim['name']) + " | stream hash: " +
str(json.loads(claim['value'])['stream_hash']))
self.console.sendLine(str(json.loads(claim['value'])))
rtn.append(claim['name'])
self.seen.append(claim)
claimflag = True
else:
tmsg.append("Transaction: " + str(t['txid']))
if claimflag:
print '! New name claims'
for c in nmsg:
print ' ' + str(c)
else:
print 'No name claims in this block'
for t in tmsg:
print ' ' + str(t)
if not len(rtn):
self.console.sendLine("No claims in block " + str(block['hash']) + ", height: " + str(block['height']))
l = c
firstrun = False
sleep(1)
self.lastbestblock = c
if len(rtn):
return defer.succeed(rtn)
else:
return defer.failure(None)
def _download_claims(self, claims):
for claim in claims:
# stream = AutoAddStreamFromHash(self.console, self.sd_identifier,
# self.session, self.lbry_file_manager, stream_hash,
# description, key_fee, key_fee_address)
stream = AutoAddStreamFromLBRYcrdName(self.console, self.sd_identifier, self.session,
self.wallet, self.lbry_file_manager)
d = threads.deferToThread(stream.start(str(claim)))
self.console.sendLine("Download complete")
return defer.succeed(d)
class AutoFetcherFactory(CommandHandlerFactory):