36 lines
935 B
Python
36 lines
935 B
Python
"""Fetches json data and prints it nicely with pprint
|
|
"""
|
|
|
|
from sys import exit
|
|
from pprint import pprint
|
|
from json import loads
|
|
from urllib.request import urlopen
|
|
from urllib.error import HTTPError
|
|
|
|
json_url = "http://webservices-v2.crous-mobile.fr:8080/feed/dijon/externe/crous-dijon.min.jsonsdf"
|
|
|
|
def retrieve_json(json_url):
|
|
"""
|
|
Retrieves and pretty prints indented json from a given url
|
|
|
|
:param str json_url: full url of the json file
|
|
:return: None
|
|
:rtype: str
|
|
:raises IOError: if the json cannot be retrieved
|
|
"""
|
|
try:
|
|
with urlopen(json_url) as resp:
|
|
contents = resp.read().decode()
|
|
pprint(loads(contents))
|
|
except HTTPError as e:
|
|
raise e
|
|
except:
|
|
raise IOError("The json {0} cannot be retrieved".format(json_url))
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
retrieve_json(json_url)
|
|
except Exception as e:
|
|
print(e)
|
|
exit(1)
|