Update lbry daemon

This commit is contained in:
Jack 2015-12-08 00:37:49 -05:00
parent 8aee19b0ef
commit 203f7e7ea6
2 changed files with 46 additions and 62 deletions

View file

@ -114,12 +114,8 @@ class AutoAddStream(object):
class GetStream(object): class GetStream(object):
def __init__(self, console, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee):
console_on=True, pay_key=True):
self.finished_deferred = defer.Deferred(None) self.finished_deferred = defer.Deferred(None)
self.console_on = console_on
self.pay_key = pay_key
self.console = console
self.wallet = wallet self.wallet = wallet
self.resolved_name = None self.resolved_name = None
self.description = None self.description = None
@ -145,42 +141,23 @@ class GetStream(object):
def start(self, stream_info): def start(self, stream_info):
self.stream_info = stream_info self.stream_info = stream_info
try: if 'stream_hash' in self.stream_info.keys():
if 'stream_hash' in json.loads(self.stream_info['value']): self.description = self.stream_info['description']
self.resolved_name = self.stream_info.get('name', None) if 'key_fee' in self.stream_info.keys():
self.description = json.loads(self.stream_info['value']).get('description', None) self.key_fee = float(self.stream_info['key_fee'])
try: self.key_fee_address = self.stream_info['key_fee_address']
if 'key_fee' in json.loads(self.stream_info['value']):
self.key_fee = float(json.loads(self.stream_info['value'])['key_fee'])
except ValueError:
self.key_fee = None
self.key_fee_address = json.loads(self.stream_info['value']).get('key_fee_address', None)
self.stream_hash = json.loads(self.stream_info['value'])['stream_hash']
else: else:
raise InvalidStreamInfoError(self.stream_info) self.key_fee = None
self.stream_hash = self.stream_info['stream_hash']
else:
raise InvalidStreamInfoError(self.stream_info)
except: if self.key_fee > self.max_key_fee:
if 'stream_hash' in self.stream_info.keys(): print "Key fee (" + str(self.key_fee) + ") above limit of " + str(
self.description = self.stream_info['description'] self.max_key_fee) + ", didn't download lbry://" + str(self.resolved_name)
if 'key_fee' in self.stream_info.keys(): return self.finished_deferred.callback(None)
self.key_fee = float(self.stream_info['key_fee']) else:
self.key_fee_address = self.stream_info['key_fee_address'] pass
else:
self.key_fee = None
self.stream_hash = self.stream_info['stream_hash']
else:
raise InvalidStreamInfoError(self.stream_info)
# if self.key_fee < self.max_key_fee:
# if self.pay_key:
# if self.console_on:
# self.console.sendLine("Key fee (" + str(self.key_fee) + ") above limit of " + str(
# self.max_key_fee) + ", didn't download lbry://" + str(self.resolved_name))
# return self.finished_deferred.callback(None)
# else:
# pass
# else:
# pass
def _get_downloader_for_return(): def _get_downloader_for_return():
return defer.succeed(self.downloader) return defer.succeed(self.downloader)
@ -192,6 +169,7 @@ class GetStream(object):
self.loading_metadata_deferred.addCallback(self._handle_metadata) 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_canceled)
self.loading_metadata_deferred.addErrback(self._handle_load_failed) self.loading_metadata_deferred.addErrback(self._handle_load_failed)
self.loading_metadata_deferred.addCallback(lambda _: self._pay_key_fee())
self.loading_metadata_deferred.addCallback(lambda _: self._make_downloader()) self.loading_metadata_deferred.addCallback(lambda _: self._make_downloader())
self.loading_metadata_deferred.addCallback(lambda _: self.downloader.start()) self.loading_metadata_deferred.addCallback(lambda _: self.downloader.start())
self.loading_metadata_deferred.addErrback(self._handle_download_error) self.loading_metadata_deferred.addErrback(self._handle_download_error)
@ -205,9 +183,8 @@ class GetStream(object):
reserved_points = self.wallet.reserve_points(self.key_fee_address, self.key_fee) reserved_points = self.wallet.reserve_points(self.key_fee_address, self.key_fee)
if reserved_points is None: if reserved_points is None:
return defer.fail(InsufficientFundsError()) return defer.fail(InsufficientFundsError())
print 'Key fee: ' + str(self.key_fee) + ' | ' + str(self.key_fee_address)
return self.wallet.send_points_to_address(reserved_points, self.key_fee) return self.wallet.send_points_to_address(reserved_points, self.key_fee)
if self.console_on:
self.console.sendLine("Sent key fee" + str(self.key_fee_address) + " | " + str(self.key_fee))
return defer.succeed(None) return defer.succeed(None)
def _handle_load_canceled(self, err): def _handle_load_canceled(self, err):
@ -216,8 +193,6 @@ class GetStream(object):
def _handle_load_failed(self, err): def _handle_load_failed(self, err):
self.loading_failed = True self.loading_failed = True
if self.console_on:
self.console.sendLine("handle load failed: " + str(err.getTraceback()))
log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback()) log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback())
print 'Load Failed: ', err.getTraceback() print 'Load Failed: ', err.getTraceback()
self.finished_deferred.callback(None) self.finished_deferred.callback(None)
@ -225,15 +200,11 @@ class GetStream(object):
def _handle_metadata(self, metadata): def _handle_metadata(self, metadata):
self.metadata = metadata self.metadata = metadata
self.factory = self.metadata.factories[0] self.factory = self.metadata.factories[0]
return defer.succeed(None) #self._start_download() return defer.succeed(None)
def _handle_download_error(self, err): def _handle_download_error(self, err):
if self.console_on: if err.check(InsufficientFundsError):
if err.check(InsufficientFundsError): print "Download stopped due to insufficient funds."
self.console.sendLine("Download stopped due to insufficient funds.")
else:
self.console.sendLine(
"Autoaddstream: An unexpected error has caused the download to stop: %s" % err.getTraceback())
else: else:
print "Autoaddstream: An unexpected error has caused the download to stop: ", err.getTraceback() print "Autoaddstream: An unexpected error has caused the download to stop: ", err.getTraceback()

View file

@ -46,7 +46,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
self.sd_identifier = StreamDescriptorIdentifier() self.sd_identifier = StreamDescriptorIdentifier()
self.stream_info_manager = TempLBRYFileMetadataManager() self.stream_info_manager = TempLBRYFileMetadataManager()
self.wallet_rpc_port = 8332 self.wallet_rpc_port = 8332
self.download_deferreds = [] self.downloads = []
self.stream_frames = [] self.stream_frames = []
self.default_blob_data_payment_rate = MIN_BLOB_DATA_PAYMENT_RATE self.default_blob_data_payment_rate = MIN_BLOB_DATA_PAYMENT_RATE
self.use_upnp = True self.use_upnp = True
@ -64,6 +64,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
self.lbrycrd_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd") self.lbrycrd_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd")
self.lbrycrd_conf = os.path.join(self.lbrycrd_dir, "lbrycrd.conf") self.lbrycrd_conf = os.path.join(self.lbrycrd_dir, "lbrycrd.conf")
self.rpc_conn = None self.rpc_conn = None
self.files = []
return defer.succeed(None) return defer.succeed(None)
d = defer.Deferred() d = defer.Deferred()
@ -208,7 +209,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory) self.sd_identifier.add_stream_downloader_factory(LBRYFileStreamType, downloader_factory)
return defer.succeed(True) return defer.succeed(True)
def xmlrpc_getbalance(self): def xmlrpc_get_balance(self):
""" """
Get LBC balance Get LBC balance
""" """
@ -233,6 +234,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
d = defer.Deferred() d = defer.Deferred()
d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name)) d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name))
d.addErrback(lambda _: 'UnknownNameError')
d.addCallback(_disp) d.addCallback(_disp)
d.callback(None) d.callback(None)
return d return d
@ -244,7 +246,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
downloads = [] downloads = []
for stream in self.download_deferreds: for stream in self.downloads:
try: try:
downloads.append({'stream_hash': stream.stream_hash, downloads.append({'stream_hash': stream.stream_hash,
'path': os.path.join(stream.downloader.download_directory, stream.downloader.file_name)}) 'path': os.path.join(stream.downloader.download_directory, stream.downloader.file_name)})
@ -260,23 +262,34 @@ class LBRYDaemon(xmlrpc.XMLRPC):
def _disp(): def _disp():
try: try:
stream = self.download_deferreds[-1] stream = self.downloads[-1]
print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash) print '[' + str(datetime.now()) + ']' + ' Downloading: ' + str(stream.stream_hash)
return defer.succeed(None) return defer.succeed(None)
except: except:
pass pass
stream = GetStream(None, self.sd_identifier, self.session, self.session.wallet, stream = GetStream(self.sd_identifier, self.session, self.session.wallet, self.lbry_file_manager, 25.0)
self.lbry_file_manager, 25.0, console_on=False, pay_key=True) self.downloads.append(stream)
self.download_deferreds.append(stream)
d = defer.Deferred() d = self.session.wallet.get_stream_info_for_name(name)
d.addCallback(lambda _: self.session.wallet.get_stream_info_for_name(name))
d.addCallback(lambda stream_info: stream.start(stream_info)) d.addCallback(lambda stream_info: stream.start(stream_info))
d.addCallback(lambda _: _disp()) d.addCallback(lambda _: _disp())
d.callback(None) # d.addCallback(lambda _: self.files.append({'name': name, 'stream_hash': stream.stream_hash,
msg = {'ts': datetime.now(),'name': name} # 'path': os.path.join(stream.downloader.download_directory, stream.downloader.file_name)}))
return defer.succeed(str(msg)) d.addCallback(lambda _: {'ts': datetime.now(),'name': name})
d.addErrback(lambda _: 'UnknownNameError')
return d
def xmlrpc_path_from_name(self, name):
d = self.session.wallet.get_stream_info_for_name(name)
d.addCallback(lambda stream_info: stream_info['stream_hash'])
d.addCallback(lambda stream_hash: [{'stream_hash': stream.stream_hash,
'path': os.path.join(stream.downloader.download_directory,
stream.downloader.file_name)}
for stream in self.downloads if stream.stream_hash == stream_hash])
return d
def main(): def main():
daemon = LBRYDaemon() daemon = LBRYDaemon()