-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkbutils.py
91 lines (73 loc) · 2.58 KB
/
kbutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import aiohttp
import asyncio
import json
import urllib.parse
import logging
import hashlib
import discord
KB_API_URL = 'https://keybase.io/_/api/1.0/%s.json'
KB_LOOKUP_URL = KB_API_URL % 'user/lookup'
loop = asyncio.get_event_loop()
logger = logging.getLogger('kbutils')
# define custom errors
class JSONError(Exception):
pass
class AiohttpError(Exception):
pass
class KeybaseError(Exception):
pass
def mkcolor(string):
return discord.Colour(int(hashlib.md5(string.encode("utf-8")).hexdigest()[:6], 16))
async def json_load(string):
try:
future_json = loop.run_in_executor(None, json.loads, string)
return (await future_json)
except Exception as err:
raise JSONError("Error parsing JSON data")
async def http_get(url, **kwargs):
timeout = kwargs.get('timeout', 5)
try:
response = await asyncio.wait_for(aiohttp.request('GET', url), timeout)
content = await response.text()
return content
except Exception as err:
logger.error('http_get', exc_info=True)
raise err
async def keybase_request(url, **kwargs):
logger.info("[ḱbrequest] %r", url)
content = await http_get(url, **kwargs)
data = await json_load(content)
# check the data for errors
status = data['status']
stcode = status['code']
if stcode != 0:
print(data, status)
raise KeybaseError('%s: %s' % (status['name'], status['desc']))
logger.info("[kbreq] length: %d", len(content))
return data
async def kblookup(lookup_string, lookup_type='usernames', _fields=[]):
r = {lookup_type: lookup_string}
querystr = urllib.parse.urlencode(r)
fields = ','.join(_fields)
url = f'{KB_LOOKUP_URL}?{querystr}&fields={fields}'
data = await keybase_request(url)
return data
async def make_ping(loop, target, dump=False):
'''Modification of https://gist.github.com/athoune/0736f73368fac38f066ac7cbf82ff5eb'''
create = asyncio.create_subprocess_exec('ping', '-c', '3', target,
stdout=asyncio.subprocess.PIPE)
proc = await create
lines = []
while True:
line = await proc.stdout.readline()
if line == b'':
break
l = line.decode('utf8').rstrip()
if dump:
print(l)
lines.append(l)
transmited, received = [int(a.split(' ')[0]) for a
in lines[-2].split(', ')[:2]]
stats, unit = lines[-1].split(' = ')[-1].split(' ')
min_, avg, max_, stddev = [float(a) for a in stats.split('/')]
return transmited, received, unit, min_, avg, max_, stddev