123456789101112131415161718192021222324252627282930 |
- import os.path
- import urllib.request
- import urllib.error
- import json
-
- def get_jsonp_file(url, temp_fname, use_old):
- return get_file(url, temp_fname, use_old, jsonp_load)
-
- def get_json_file(url, temp_fname, use_old):
- return get_file(url, temp_fname, use_old, json.load)
-
- 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):
- 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
- try:
- with open(temp_fname, "r", encoding="utf-8") as fin:
- return consumer(fin)
- except OSError as e:
- pass
|