import os.path import urllib.request import urllib.error import json def get_jsonp_file(url, temp_fname, use_old, allow_old=True): try: return get_file(url, temp_fname, use_old, jsonp_load, allow_old) except json.decoder.JSONDecodeError as e: print("Failed to parse JSON from {}".format(temp_fname)) raise def get_json_file(url, temp_fname, use_old, allow_old=True): try: return get_file(url, temp_fname, use_old, json.load, allow_old) except json.decoder.JSONDecodeError as e: print("Failed to parse JSON from {}".format(temp_fname)) raise 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