Преглед на файлове

Do not display old food for juvenes

Toni Fadjukoff преди 7 години
родител
ревизия
9ec32b6a23
променени са 2 файла, в които са добавени 8 реда и са изтрити 6 реда
  1. 1 1
      juvenes.py
  2. 7 5
      pnalib.py

+ 1 - 1
juvenes.py Целия файл

@@ -34,7 +34,7 @@ def get_restaurants(use_old, week):
34 34
         for weekday in range(1,7):
35 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 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 38
             if data and data["d"] != "null":
39 39
                 data = json.loads(data["d"])
40 40
                 cur_day_foods = []

+ 7 - 5
pnalib.py Целия файл

@@ -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)