From 5dd68a7ed4c26fe99c621ec3eb0d0522dd49aa45 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 28 Jul 2016 14:55:17 -0400 Subject: [PATCH] add get_claim_info, clean up _get_stream_info_from_value --- lbrynet/core/LBRYWallet.py | 61 +++++++++++++++++++++------- lbrynet/lbrynet_daemon/LBRYDaemon.py | 11 +++++ 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/lbrynet/core/LBRYWallet.py b/lbrynet/core/LBRYWallet.py index 7d7ab8db7..60696624b 100644 --- a/lbrynet/core/LBRYWallet.py +++ b/lbrynet/core/LBRYWallet.py @@ -320,26 +320,57 @@ class LBRYWallet(object): return d def _get_stream_info_from_value(self, result, name): - if 'value' in result: - value = result['value'] + def _check_result_fields(r): + for k in ['value', 'txid', 'n', 'height', 'amount']: + assert k in r, "getvalueforname response missing field %s" % k - try: - value_dict = json.loads(value) - except (ValueError, TypeError): - return Failure(InvalidStreamInfoError(name)) - m = Metadata(value_dict) - if 'txid' in result: - d = self._save_name_metadata(name, str(result['txid']), m['sources']['lbry_sd_hash']) - d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, m.meta_version))) - d.addCallback(lambda _: m) - return d - elif 'error' in result: + if 'error' in result: log.warning("Got an error looking up a name: %s", result['error']) return Failure(UnknownNameError(name)) - else: - log.warning("Got an error looking up a name: %s", json.dumps(result)) + + _check_result_fields(result) + + try: + metadata = Metadata(json.loads(result['value'])) + except (ValueError, TypeError): + return Failure(InvalidStreamInfoError(name)) + + d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) + d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) + d.addCallback(lambda _: metadata) + return d + + def _get_claim_info(self, result, name): + def _check_result_fields(r): + for k in ['value', 'txid', 'n', 'height', 'amount']: + assert k in r, "getvalueforname response missing field %s" % k + + def _build_response(m, result): + result['value'] = m + return result + + if 'error' in result: + log.warning("Got an error looking up a name: %s", result['error']) return Failure(UnknownNameError(name)) + _check_result_fields(result) + + try: + metadata = Metadata(json.loads(result['value'])) + except (ValueError, TypeError): + return Failure(InvalidStreamInfoError(name)) + + d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) + d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) + d.addCallback(lambda _: _build_response(metadata, result)) + return d + + def get_claim_info(self, name): + d = self._get_value_for_name(name) + d.addCallback(lambda r: self._get_claim_info(r, name)) + return d + + def claim_name(self, name, bid, m): metadata = Metadata(m) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 914d6360b..b41f4eecf 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1718,6 +1718,17 @@ class LBRYDaemon(jsonrpc.JSONRPC): d.addCallbacks(lambda info: self._render_response(info, OK_CODE), lambda _: server.failure) return d + def jsonrpc_get_claim_info(self, p): + def _convert_amount_to_float(r): + r['amount'] = float(r['amount']) / 10**8 + return r + + name = p['name'] + d = self.session.wallet.get_claim_info(name) + d.addCallback(_convert_amount_to_float) + d.addCallback(lambda r: self._render_response(r, OK_CODE)) + return d + def jsonrpc_get(self, p): """ Download stream from a LBRY uri