# Copyright 2018 Toni Fadjukoff. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. 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