Separate error fetching from generation

This commit is contained in:
Lonami Exo 2018-04-14 13:20:44 +02:00
parent 88597f0da8
commit 8b2afa3530
2 changed files with 34 additions and 28 deletions

View File

@ -1,10 +1,7 @@
import json
import re
import urllib.request
from collections import defaultdict
URL = 'https://rpc.pwrtelegram.xyz/?all'
known_base_classes = {
303: 'InvalidDCError',
400: 'BadRequestError',
@ -25,26 +22,6 @@ known_codes = {
}
def fetch_errors(output, url=URL):
print('Opening a connection to', url, '...')
r = urllib.request.urlopen(urllib.request.Request(
url, headers={'User-Agent' : 'Mozilla/5.0'}
))
print('Checking response...')
data = json.loads(
r.read().decode(r.info().get_param('charset') or 'utf-8')
)
if data.get('ok'):
print('Response was okay, saving data')
with open(output, 'w', encoding='utf-8') as f:
json.dump(data, f, sort_keys=True)
return True
else:
print('The data received was not okay:')
print(json.dumps(data, indent=4, sort_keys=True))
return False
def get_class_name(error_code):
if isinstance(error_code, int):
return known_base_classes.get(
@ -170,8 +147,5 @@ def generate_code(output, json_file, errors_desc):
if __name__ == '__main__':
if input('generate (y/n)?: ').lower() == 'y':
generate_code('../telethon/errors/rpc_error_list.py',
'errors.json', 'error_descriptions')
elif input('fetch (y/n)?: ').lower() == 'y':
fetch_errors('errors.json')
generate_code('../telethon/errors/rpc_error_list.py',
'errors.json', 'error_descriptions')

View File

@ -0,0 +1,32 @@
import sys
import json
import urllib.request
OUT = 'errors.json'
URL = 'https://rpc.pwrtelegram.xyz/?all'
def fetch_errors(output, url=URL):
print('Opening a connection to', url, '...')
r = urllib.request.urlopen(urllib.request.Request(
url, headers={'User-Agent' : 'Mozilla/5.0'}
))
print('Checking response...')
data = json.loads(
r.read().decode(r.info().get_param('charset') or 'utf-8')
)
if data.get('ok'):
print('Response was okay, saving data')
with open(output, 'w', encoding='utf-8') as f:
json.dump(data, f, sort_keys=True)
return True
else:
print('The data received was not okay:')
print(json.dumps(data, indent=4, sort_keys=True))
return False
if __name__ == '__main__':
out = OUT if len(sys.argv) < 2 else sys.argv[2]
url = URL if len(sys.argv) < 3 else sys.argv[3]
fetch_errors(out, url)