Sort keys and use Mozilla agent on error generator, update file

This commit is contained in:
Lonami Exo 2018-01-28 14:02:42 +01:00
parent 700b4c3169
commit 7286f77008
2 changed files with 14 additions and 4 deletions

View File

@ -26,7 +26,9 @@ known_codes = {
def fetch_errors(output, url=URL):
print('Opening a connection to', url, '...')
r = urllib.request.urlopen(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')
@ -34,11 +36,11 @@ def fetch_errors(output, url=URL):
if data.get('ok'):
print('Response was okay, saving data')
with open(output, 'w', encoding='utf-8') as f:
json.dump(data, f)
json.dump(data, f, sort_keys=True)
return True
else:
print('The data received was not okay:')
print(json.dumps(data, indent=4))
print(json.dumps(data, indent=4, sort_keys=True))
return False
@ -164,3 +166,11 @@ def generate_code(output, json_file, errors_desc):
for pattern, name in patterns:
f.write(' {}: {},\n'.format(repr(pattern), name))
f.write('}\n')
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')

File diff suppressed because one or more lines are too long