This commit is contained in:
Victor Shyba 2019-04-23 15:40:10 -03:00 committed by Lex Berezhny
parent 2145bfcabf
commit 7bdef3146b
3 changed files with 12 additions and 32 deletions

View file

@ -25,7 +25,7 @@ def display(data):
print(json.dumps(data, indent=2)) print(json.dumps(data, indent=2))
async def execute_command(conf, method, params): async def execute_command(conf, method, params, callback=display):
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
try: try:
message = {'method': method, 'params': params} message = {'method': method, 'params': params}
@ -33,12 +33,12 @@ async def execute_command(conf, method, params):
try: try:
data = await resp.json() data = await resp.json()
if 'result' in data: if 'result' in data:
display(data['result']) return callback(data['result'])
elif 'error' in data: elif 'error' in data:
if 'message' in data['error']: if 'message' in data['error']:
display(data['error']['message']) return callback(data['error']['message'])
else: else:
display(data['error']) return callback(data['error'])
except Exception as e: except Exception as e:
log.exception('Could not process response from server:', exc_info=e) log.exception('Could not process response from server:', exc_info=e)
except aiohttp.ClientConnectionError: except aiohttp.ClientConnectionError:

View file

@ -1,27 +1,6 @@
import aiohttp
import logging
from lbrynet.conf import Config from lbrynet.conf import Config
from lbrynet.extras.cli import execute_command
log = logging.getLogger(__name__)
USER_AGENT = "AuthServiceProxy/0.1"
TWISTED_SECURE_SESSION = "TWISTED_SECURE_SESSION"
TWISTED_SESSION = "TWISTED_SESSION"
LBRY_SECRET = "LBRY_SECRET"
HTTP_TIMEOUT = 30
class JSONRPCException(Exception): def daemon_rpc(conf: Config, method: str, **kwargs):
def __init__(self, rpc_error): return execute_command(conf, method, kwargs, callback=lambda data: data)
super().__init__()
self.error = rpc_error
async def daemon_rpc(conf: Config, method: str, *args, **kwargs):
async with aiohttp.ClientSession() as session:
message = {'method': method, 'params': [args, kwargs]}
async with session.get(conf.api_connection_url, json=message) as resp:
data = await resp.json()
if 'result' in data:
return data['result']
elif 'error' in data:
raise JSONRPCException(data['error'])

View file

@ -90,15 +90,16 @@ async def main(uris=None, allow_fees=False):
print("**********************************************") print("**********************************************")
resolvable = [] resolvable = []
for name in uris: async def __resolve(name):
resolved = await daemon_rpc(conf, 'resolve', name) resolved = await daemon_rpc(conf, 'resolve', urls=[name])
if 'error' not in resolved.get(name, {}): if 'error' not in resolved.get(name, {}):
if ("fee" not in resolved[name]['claim']['value']['stream']) or allow_fees: if ("fee" not in resolved[name]['claim']['value']) or allow_fees:
resolvable.append(name) resolvable.append(name)
else: else:
print(f"{name} has a fee, skipping it") print(f"{name} has a fee, skipping it")
else: else:
print(f"failed to resolve {name}: {resolved[name]['error']}") print(f"failed to resolve {name}: {resolved[name]['error']}")
await asyncio.gather(*(__resolve(name) for name in uris))
print(f"attempting to download {len(resolvable)}/{len(uris)} frontpage streams") print(f"attempting to download {len(resolvable)}/{len(uris)} frontpage streams")
first_byte_times = [] first_byte_times = []
@ -113,7 +114,7 @@ async def main(uris=None, allow_fees=False):
for i, uri in enumerate(resolvable): for i, uri in enumerate(resolvable):
start = time.time() start = time.time()
try: try:
await daemon_rpc(conf, 'get', uri) await daemon_rpc(conf, 'get', uri=uri)
first_byte = time.time() first_byte = time.time()
first_byte_times.append(first_byte - start) first_byte_times.append(first_byte - start)
print(f"{i + 1}/{len(resolvable)} - {first_byte - start} {uri}") print(f"{i + 1}/{len(resolvable)} - {first_byte - start} {uri}")