import os.path import urllib.request import urllib.error import json def get_jsonp_file(url, temp_fname, use_old, allow_old=True): return get_file(url, temp_fname, use_old, jsonp_load, allow_old) def get_json_file(url, temp_fname, use_old, allow_old=True): return get_file(url, temp_fname, use_old, json.load, allow_old) def jsonp_load(fp): return json.loads(fp.read()[1:-2]) def read_all(fp): return fp.read() def get_file(url, temp_fname, use_old, consumer=read_all, allow_old=True): if not use_old or not os.path.isfile(temp_fname): try: urllib.request.urlretrieve(url, temp_fname) except urllib.error.HTTPError as e: print("Failed to download {url}".format(url=url)) # Juvenes may fail with error code 500 if food is not available if not allow_old: return None try: with open(temp_fname, "r", encoding="utf-8") as fin: return consumer(fin) except OSError as e: pass