Browse Source

Do not display old food for juvenes

Toni Fadjukoff 7 years ago
parent
commit
9ec32b6a23
2 changed files with 8 additions and 6 deletions
  1. 1 1
      juvenes.py
  2. 7 5
      pnalib.py

+ 1 - 1
juvenes.py View File

34
         for weekday in range(1,7):
34
         for weekday in range(1,7):
35
             url = "http://www.juvenes.fi/DesktopModules/Talents.LunchMenu/LunchMenuServices.asmx/GetMenuByWeekday?KitchenId={kitchen}&MenuTypeId={menutype}&Week={week}&Weekday={weekday}&lang='fi'&format=json".format(kitchen=kitchen, menutype=menutype, week=week, weekday=weekday)
35
             url = "http://www.juvenes.fi/DesktopModules/Talents.LunchMenu/LunchMenuServices.asmx/GetMenuByWeekday?KitchenId={kitchen}&MenuTypeId={menutype}&Week={week}&Weekday={weekday}&lang='fi'&format=json".format(kitchen=kitchen, menutype=menutype, week=week, weekday=weekday)
36
             temp_fname = "juvenes_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
36
             temp_fname = "juvenes_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
37
-            data = pnalib.get_jsonp_file(url, temp_fname, use_old)
37
+            data = pnalib.get_jsonp_file(url, temp_fname, use_old, allow_old=False)
38
             if data and data["d"] != "null":
38
             if data and data["d"] != "null":
39
                 data = json.loads(data["d"])
39
                 data = json.loads(data["d"])
40
                 cur_day_foods = []
40
                 cur_day_foods = []

+ 7 - 5
pnalib.py View File

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