|
@@ -3,11 +3,11 @@ import urllib.request
|
3
|
3
|
import urllib.error
|
4
|
4
|
import json
|
5
|
5
|
|
6
|
|
-def get_jsonp_file(url, temp_fname, use_old):
|
7
|
|
- return get_file(url, temp_fname, use_old, jsonp_load)
|
|
6
|
+def get_jsonp_file(url, temp_fname, use_old, allow_old=True):
|
|
7
|
+ return get_file(url, temp_fname, use_old, jsonp_load, allow_old)
|
8
|
8
|
|
9
|
|
-def get_json_file(url, temp_fname, use_old):
|
10
|
|
- return get_file(url, temp_fname, use_old, json.load)
|
|
9
|
+def get_json_file(url, temp_fname, use_old, allow_old=True):
|
|
10
|
+ return get_file(url, temp_fname, use_old, json.load, allow_old)
|
11
|
11
|
|
12
|
12
|
def jsonp_load(fp):
|
13
|
13
|
return json.loads(fp.read()[1:-2])
|
|
@@ -15,13 +15,15 @@ def jsonp_load(fp):
|
15
|
15
|
def read_all(fp):
|
16
|
16
|
return fp.read()
|
17
|
17
|
|
18
|
|
-def get_file(url, temp_fname, use_old, consumer=read_all):
|
|
18
|
+def get_file(url, temp_fname, use_old, consumer=read_all, allow_old=True):
|
19
|
19
|
if not use_old or not os.path.isfile(temp_fname):
|
20
|
20
|
try:
|
21
|
21
|
urllib.request.urlretrieve(url, temp_fname)
|
22
|
22
|
except urllib.error.HTTPError as e:
|
23
|
23
|
print("Failed to download {url}".format(url=url))
|
24
|
24
|
# Juvenes may fail with error code 500 if food is not available
|
|
25
|
+ if not allow_old:
|
|
26
|
+ return None
|
25
|
27
|
try:
|
26
|
28
|
with open(temp_fname, "r", encoding="utf-8") as fin:
|
27
|
29
|
return consumer(fin)
|