Compare commits

...

2 commits

Author SHA1 Message Date
Izalia Mae 90566894de add pyjson.py 2021-11-16 07:45:21 -05:00
Izalia Mae 2fea6cf1ae move scripts to subdir 2021-10-13 18:53:59 -04:00
22 changed files with 28 additions and 0 deletions

28
bin/pyjson.py Normal file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python3
# PyJSON v1.0
# Fetches JSON data from the specified url and pretty prints it.
#
# Usage:
# pyjson.py <url>
import sys, json
from urllib.request import urlopen, Request
if len(sys.argv) < 2:
print('Please provide a url')
sys.exit()
url = sys.argv[1]
if not url.startswith('http'):
url = 'https://' + url
with urlopen(Request(url, headers={'accept': 'application/json', 'user-agent': 'PyJSON/1.0'})) as r:
data = r.read().decode()
if r.status not in [200,202]:
print('Received an error when trying to fetch json:', r.status, data)
sys.exit()
print(json.dumps(json.loads(data), indent=4))