Browse Source

Start creating python version of scripts

Toni Fadjukoff 7 years ago
parent
commit
985b22a42b
5 changed files with 511 additions and 0 deletions
  1. 8 0
      amica.py
  2. 408 0
      food.py
  3. 75 0
      juvenes.py
  4. 12 0
      pikante.py
  5. 8 0
      sodexo.py

+ 8 - 0
amica.py View File

@@ -0,0 +1,8 @@
1
+restaurant_info = [
2
+  [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=7381" ],
3
+  [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=6655" ],
4
+  [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ]
5
+]
6
+
7
+def get_restaurants(use_old):
8
+    return []

+ 408 - 0
food.py View File

@@ -0,0 +1,408 @@
1
+#!/usr/bin/env perl
2
+# encoding: UTF-8
3
+
4
+# Ruokalistaparseri
5
+# Copyright (c) 2016 Toni Fadjukoff
6
+
7
+# Based on food.pl by
8
+# Copyright (c) 2007-2010 Timo Sirainen
9
+#               2011-2016 Toni Fadjukoff
10
+# This is Public Domain
11
+
12
+day_names = [ "Maanantai", "Tiistai", "Keskiviikko", "Torstai", 
13
+	       "Perjantai", "Lauantai", "Sunnuntai" ]
14
+
15
+import amica
16
+import sodexo
17
+import juvenes
18
+import pikante
19
+
20
+allergies = [ "M", "L", "VL", "G", "K", "Ve" ]
21
+allergy_descriptions = {
22
+        "M": "Maidoton",
23
+        "L": "Laktoositon","VL": "Vähälaktoosinen",
24
+        "G": "Gluteiiniton",
25
+        "K": "Kasvis",
26
+        "Ve": "Vegaani"
27
+        }
28
+
29
+import os
30
+import time
31
+import datetime
32
+import re
33
+
34
+global_prefix = "";
35
+use_old = True; # 1 is good for testing, 0 for production system!
36
+unordered = []
37
+
38
+l = time.localtime()
39
+this_week = datetime.datetime.now().isocalendar()[1]
40
+
41
+unordered += amica.get_restaurants(use_old)
42
+unordered += juvenes.get_restaurants(use_old)
43
+unordered += sodexo.get_restaurants(use_old)
44
+unordered += pikante.get_restaurants(use_old)
45
+
46
+print(unordered)
47
+
48
+max_week = 0;
49
+for r in unordered:
50
+  week = r[2]
51
+  max_week = week if week > max_week or week == 1 else max_week
52
+
53
+if l[6] != 0 and this_week != max_week:
54
+  # it's not sunday, don't force next week's menu yet
55
+  max_week = this_week
56
+
57
+stamp = time.time() - 3600*24*7
58
+max_week_daterange = ""
59
+if max_week >= 1 and max_week <= 52:
60
+  # figure out the date range
61
+  while True:
62
+    stamp_week = int(time.strftime("%W", time.localtime(stamp)))
63
+    if stamp_week == max_week:
64
+            break
65
+    stamp += 3600*24
66
+  l1 = time.localtime(stamp)
67
+  l2 = time.localtime(stamp + 3600*24*6)
68
+  if l1[1] == l2[1]:
69
+    # same month
70
+    max_week_daterange = "{monday}-{sunday}.{month}.".format(monday=l1[2], sunday=l2[2], month=l1[1])
71
+  else:
72
+    # different months
73
+    max_week_daterange = "{monday}.{month}.-{sunday}.{next_month}.".format(monday=l1[2], month=l1[1],
74
+            sunday=l2[2], next_month=l2[1])
75
+  max_week_daterange = " (" + str(max_week_daterange) + ")"
76
+
77
+file_header = '''<?xml version="1.0" encoding="utf-8"?>
78
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
79
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi" lang="fi">
80
+<head>
81
+  <title>Ruokalistat</title>
82
+  <link rel="stylesheet" type="text/css" href="{global_prefix}/ruoka.css" />
83
+</head>
84
+<body>
85
+
86
+<div id="notice" style="border: 1px solid black; border-radius: 5px; padding: 5px;">
87
+PNA.fi on kolmannen osapuolen tarjoama palvelu. En voi taata ruokalistojen oikeellisuutta.  Virallisen ruokalistan saat näkyviin siirtymällä ravintolan omille sivuille painamalla sen nimestä. Jos huomaat ruokalistassa virheen, nopeiten virhe saadaan pois näkyvistä kun lähetät minulle siitä sähköpostia: <a href="mailto:lamperi+pna@gmail.com">lamperi+pna@gmail.com</a>
88
+</div>
89
+
90
+<form method="get" action="/cgi-bin/food.cgi">
91
+'''.format(global_prefix=global_prefix)
92
+
93
+file_footer = """
94
+      <div class="footer">Päivitetty {stamp}"
95
+        <input type="submit\" value=\"Päivitä nyt\" />
96
+          / Palaute <a href=\"mailto:lamperi+pna\@gmail.com\">lamperi+pna\@gmail.com</a>
97
+          / <a href="{global_prefix}/code.html\">Koodit täältä</a>
98
+         / <a href="{global_prefix}/pna.html\">Mikä on PNA?</a>
99
+      </div>
100
+    </form>
101
+  </body>
102
+</html>
103
+""".format(global_prefix=global_prefix, stamp=time.strftime("%d.%m.%Y %H:%M:%S"))
104
+
105
+def find_last_day_with_foods(restaurants):
106
+    last_day = 0
107
+    for r in restaurants:
108
+        for day in range(7):
109
+            if day in r[3]:
110
+                last_day = day if day > last_day else last_day
111
+    return last_day
112
+
113
+def write_days_header(fout, day, last_day):
114
+    fout.write( "  <span class=\"days\">")
115
+    for i in range(last_day):
116
+        if i == day:
117
+            fout.write("{0} ".format(day_names[i]))
118
+        else:
119
+            fout.write("<a href=\"{0}.html\">{1}</a> ".format(i+1, day_names[i]))
120
+  
121
+    if day < 0:
122
+        fout.write("Taulukko")
123
+    else:
124
+        fout.write("<a href=\"table.html\">Taulukko</a>")
125
+    fout.write("</span>\n")
126
+
127
+def write_prefix_header(fout, prefix, day):
128
+    day = "table" if day == 0 else day
129
+    fout.write("<span class=\"location\">")
130
+    if prefix == "":
131
+        fout.write("Kaikki ")
132
+    else:
133
+        fout.write("<a href=\"{global_prefix}/{day}.html\">Kaikki</a> ".format(global_prefix=global_prefix, day=day))
134
+    if prefix == "tay/":
135
+        fout.write("TaY ")
136
+    else:
137
+        fout.write("<a href=\"{global_prefix}/tay/{day}.html\">TaY</a> ".format(global_prefix=global_prefix, day=day))
138
+    if prefix == "tays/":
139
+        fout.write("TAYS ")
140
+    else:
141
+        fout.write("<a href=\"{global_prefix}/tays/{day}.html\">TAYS</a> ".format(global_prefix=global_prefix, day=day))
142
+    if prefix == "tty/":
143
+        fout.write("TTY ")
144
+    else:
145
+        fout.write("<a href=\"{global_prefix}/tty/{day}.html\">TTY</a> ".format(global_prefix=global_prefix, day=day))
146
+    fout.write("</span>\n")
147
+
148
+def write_day(day, header, outfname, last_day, restaurants, prefix):
149
+    with open(outfname, "w") as fout:
150
+        fout.write("{file_header}<h1>{header}</h1>\n".format(file_header=file_header, header=header))
151
+        # print weekday links
152
+        fout.write("<div class=\"title\">\n")
153
+        write_days_header(fout, day, last_day)
154
+        fout.write(" <span class=\"allergy\">Näytä: ")
155
+        for a in allergies:
156
+            fout.write("<input type=\"checkbox\" name=\"allergy_{a}\" id=\"allergy_{a}\" onclick=\"highlight()\" />".format(a=a))
157
+            fout.write("<span title=\"{allergy_description}\">{a}</span>".format(allergy_description=allergy_descriptions[a], a=a))
158
+        fout.write("</span>\n")
159
+        write_prefix_header(fout, prefix, day+1);
160
+        fout.write("</div>\n")
161
+
162
+        # print foods
163
+        foodnum = 0
164
+        eatable_food_numbers = {}
165
+        maybe_eatable_food_numbers = {}
166
+        for a in allergies:
167
+            eatable_food_numbers[a] = []
168
+            maybe_eatable_food_numbers[a] = []
169
+        css_class = "left"
170
+        fout.write("div class=\"foods\"><div class=\"{css_class}\">\n".format(css_class=css_class))
171
+        for r in restaurants:
172
+            title, open_hours, week, week_foods, info = r
173
+            title2, url, lazy_allergies, info_class = info[0:4]
174
+            if day in week_foods or day < 5:
175
+                if info_class != css_class:
176
+                    css_class = info_class
177
+                    fout.write("</div><div class=\"{css_class}\">\n".format(css_class=css_class))
178
+                url = url.replace("&", "&amp;")
179
+                fout.write("<h2><a href=\"{url}\">{title}</a></h2>\n".format(url=url, title=title))
180
+                if not day in week_foods:
181
+                    fout.write("<p class=\"missing\">Ruokalistaa ei saatavilla.</p>")
182
+                    continue
183
+                if week != "" and week != max_week:
184
+                    if week > max_week or (week == 1 and max_week == 52):
185
+                        # early..
186
+                        fout.write("<p class=\"nextweek\">Viikon {week} ruokalista:</p>".format(week=week))
187
+                    else:
188
+                        fout.write("<p class=\"missing\">Saatavilla vain viikon {week} ruokalista.</p>".format(week=week))
189
+                        continue
190
+                if len(week_foods[day]) == 0:
191
+                    fout.write("<p class=\"missing\">Ei ruokatietoja päivälle.</p>")
192
+                    continue
193
+                fout.write("<ul class=\"food\">\n")
194
+                for food in week_foods[day]:
195
+                    output = ""
196
+                    total_allergies = {}
197
+                    maybe_allergies = {}
198
+                    for a in allergies:
199
+                        total_allergies[a] = 0
200
+                        maybe_allergies[a] = 0
201
+                    part_count = 0
202
+                    for part in food.split("\n"):
203
+                        # who cares?
204
+                        if re.match("Peruna|Riisi", part):
205
+                            continue
206
+	                # fries: well, maybe we do care, but we don't care about allergy stuff
207
+                        # and keep it in the same line as the previous food so as not to
208
+	                # waste visible space
209
+                        (part, fries) = re.subn("Tikkuperunat|Ranskalaiset perunat", "", part)
210
+                        fries = fries > 0
211
+                        part_count += 1
212
+
213
+                        # add missing () around allergies
214
+                        part = re.sub(" (([MLGK]|VL|Ve|Veg|Hot)(,([MLGK]|VL|Ve|Veg|Hot|))+)$", " (\\1)", part)
215
+                        match = re.match("^(.*) \\(([^\\)]+)\\)$", part)
216
+                        if match:
217
+                            # fix allergy issues
218
+                            food = match.group(1)
219
+                            allergy = match.group(2)
220
+                            # standardization
221
+                            allergy = re.sub("Kasvis", "K", allergy)
222
+                            allergy = re.sub("([MLGK]|VL)([MLGK]|[VL])", "\\1,\\2", allergy)
223
+                            # spaces to commas
224
+                            allergy = re.sub("saatavana[: ]+(.*)$", "eriks: \\1", allergy)
225
+                            allergy = re.sub(" +", ",", allergy)
226
+                            # remove double commas
227
+                            allergy = re.sub(",+", ",", allergy)
228
+                            # eriks: standardization
229
+                            allergy = re.sub(",?eriks:,?", ", eriks: ", allergy)
230
+                            # remove extra commas/spaces from beginning/end
231
+                            allergy = re.sub("^[, ]+", "", allergy)
232
+                            allergy = re.sub("[, ]+$", "", allergy)
233
+                            part = "{food} ({allergy})".format(food=food, allergy=allergy)
234
+
235
+                        if output != "" and not fries:
236
+                            output += "<br />\n"
237
+
238
+                        match = re.search("Saatavana myös: (.*)", part)
239
+                        if match:
240
+                            alt = match.group(1)
241
+                            alt = re.sub(r"^\((.*)\)$", r"\1", alt)
242
+                            alt = re.sub("[, ]+", r",", alt)
243
+                            alt = re.sub("^,+", "", alt)
244
+                            alt = re.sub(",+$", "", alt)
245
+                            part = re.sub(r"\)[- ]*Saatavana myös:.*", "eriks: {alt})".format(alt=alt), part)
246
+                            part = re.sub(r"[- ]*Saatavana myös:.*", "(eriks: {alt})".format(alt=alt), part)
247
+
248
+                        match = re.match(r"^(.*)(\([^\)]+\))$", part)
249
+                        if match:
250
+                            text = match.group(1)
251
+                            allergy = match.group(2)
252
+                            if fries:
253
+                                output += ", {text}".format(text=text)
254
+                            else:
255
+                                output += "{text} <span class=\"allergy\">{allergy}</span>".format(text=text, allergy=allergy)
256
+                            allergy = re.sub(r"^\((.*)\)$", r"\1", allergy)
257
+                            allergy = re.sub(" *eriks: ", "", allergy)
258
+                            this_allergies = set()
259
+                            for a in re.split("[, ]", allergy):
260
+                                for al in allergies:
261
+                                    if a == al:
262
+                                        this_allergies.add(a)
263
+                                        break
264
+                            if "L" in this_allergies:
265
+                                this_allergies.add("VL")
266
+                            for a in this_allergies:
267
+                                if a in total_allergies:
268
+                                    total_allergies[a] += 1
269
+                                if a in maybe_allergies:
270
+                                    maybe_allergies[a] += 1
271
+                            match = re.search("M", lazy_allergies)
272
+                            if match:
273
+                                if "L" in this_allergies and not "M" in this_allergies:
274
+                                    maybe_allergies["M"] += 1
275
+                        else:
276
+                            if lazy_allergies == "all":
277
+                                for a in allergies:
278
+                                    maybe_allergies[a] += 1
279
+
280
+                            output += part
281
+                    allergy_output = ""
282
+                    for a in allergies:
283
+                        if total_allergies[a] == part_count:
284
+                            eatable_food_numbers[a].append(foodnum)
285
+                        elif maybe_allergies[a]== part_count:
286
+                            maybe_eatable_food_numbers[a].append(foodnum)
287
+                    fout.write("  <li id=\"f{foodnum}\">{output}</li>\n".format(foodnum=foodnum, output=output))
288
+                    foodnum += 1
289
+                fout.write("</ul>\n")
290
+        # write allergy scripts
291
+        fout.write('<script type="text/javascript" src="{global_prefix}/ruoka.js"></script>'.format(global_prefix=global_prefix))
292
+        fout.write('<script type="text/javascript">')
293
+        fout.write("var eatable_foods = [];")
294
+        fout.write("var maybe_eatable_foods = [];")
295
+        for a in allergies:
296
+            fout.write("eatable_foods[\"{a}\"] = [{eatable_food_number}];\n".format(a=a, eatable_food_number=",".join(str(e) for e in eatable_food_numbers[a])))
297
+            fout.write("maybe_eatable_foods[\"{a}\"] = [{maybe_eatable_food_number}];\n".format(a=a, maybe_eatable_food_number=",".join(str(e) for e in maybe_eatable_food_numbers[a])))
298
+        allergy_string = ",".join('"{a}"'.format(a=a) for a in allergies)
299
+        fout.write("var allergies = {allergies}];\n".format(allergies = allergy_string))
300
+        fout.write("var food_count = $foodnum;\n")
301
+        fout.write("window.onload = function() { set_allergies(); show_warning(); };\n")
302
+        fout.write("</script>\n")
303
+        fout.write("</div></div>{file_footer}".format(file_footer=file_footer))
304
+
305
+def write_all_days(restaurants, prefix, title):
306
+    try:
307
+        os.mkdir(prefix)
308
+    except OSError as err:
309
+        pass # hope it already exists
310
+    last_day = find_last_day_with_foods(restaurants);
311
+    for day in range(7):
312
+        outfname = "{prefix}{day}.html".format(prefix=prefix, day=day+1)
313
+        if day > last_day:
314
+            try:
315
+                os.unlink(outfname)
316
+            except OSError as err:
317
+                pass # probably did not exist
318
+            continue
319
+        header = "{day_name} - {title} vko {max_week}{max_week_daterange}".format(day_name=day_names[day], title=title,
320
+                max_week=max_week, max_week_daterange=max_week_daterange)
321
+        write_day(day, header, outfname, last_day, restaurants, prefix)
322
+ 
323
+def write_table(restaurants, prefix, title):
324
+    last_day = find_last_day_with_foods(restaurants);
325
+    outfname = "{prefix}table.html".format(prefix=prefix)
326
+    with open(outfname, "w") as fout:
327
+        header = "{title} vko {max_week}{max_week_daterange}".format(title=title, max_week=max_week, max_week_daterange=max_week_daterange)
328
+        fout.write("{file_header}<h1>{header}</h1>\n".format(file_header=file_header, header=header))
329
+        fout.write("<div class=\"title\">\n")
330
+
331
+        write_days_header(fout, -1, last_day)
332
+        write_prefix_header(fout, prefix, 0)
333
+        fout.write("</div><table border=\"1\"><tr><th>Päivä</th>")
334
+        for r in restaurants:
335
+            (title, open_hours, week, week_foods, info) = r
336
+            (title2, url) = info[0:2]
337
+            url = re.sub("&", "&nbsp;", url)
338
+            fout.write("<th><a href=\"{url}\">{title}</a></th>")
339
+        fout.write("</tr>\n")
340
+        for day in range(last_day):
341
+            fout.write("<tr><td>{day_name}</td>\n".format(day_name=day_names[day]))
342
+            for r in restaurants:
343
+                (title, open_hours, week, week_foods, info) = r
344
+                if day in week_foods and (week == "" or week == max_week):
345
+                    fout.write("<td><ul>\n")
346
+                    for food in week_foods[day]:
347
+                        fout.write("<li>{food}</li>".format(food=food))
348
+                    fout.write("</ul></td>\n")
349
+                else:
350
+                    fout.write("<td></td>\n")
351
+            fout.write("</tr\n")
352
+        fout.write("</table>{file_footer}".format(file_footer=file_footer))
353
+
354
+def get_restaurants_sorted(restaurants):
355
+    # consider writing comparator
356
+    out = []
357
+    for r in restaurants:
358
+        if r[4][3] == "left":
359
+            out.append(r)
360
+    for r in restaurants:
361
+        if r[4][3] == "right":
362
+            out.append(r)
363
+    for r in restaurants:
364
+        if r[4][3] == "middle" and not re.search("TAMK", r[4][1]):
365
+            out.append(r)
366
+    for r in restaurants:
367
+        if r[4][3] == "middle" and re.search("TAMK", r[4][1]):
368
+            out.append(r)
369
+    return out
370
+
371
+def get_restaurants_with_prefix(prefix, restaurants):
372
+    out = []
373
+    for r in restaurants:
374
+        if re.search(prefix, r[4][1]):
375
+            out.append(r)
376
+    return get_restaurants_sorted(out)
377
+
378
+tty_title = "TTY:n ruokalistat";
379
+tty = get_restaurants_with_prefix("TTY", unordered);
380
+write_all_days(tty, "tty/", tty_title);
381
+write_table(tty, "tty/", tty_title);
382
+
383
+tay_title = "Tampereen yliopiston ruokalistat";
384
+tay = get_restaurants_with_prefix("TaY", unordered);
385
+write_all_days(tay, "tay/", tay_title);
386
+write_table(tay, "tay/", tay_title);
387
+
388
+tays_title = "TAYS:n ruokalistat";
389
+tays = get_restaurants_with_prefix("TAYS", unordered);
390
+write_all_days(tays, "tays/", tays_title);
391
+write_table(tays, "tays/", tays_title);
392
+
393
+for r in unordered:
394
+    if re.search(r"^\(TaY\)", r[0]):
395
+        r[4][3] = "left"
396
+    if re.search(r"^\(TTY\)", r[0]):
397
+        r[4][3] = "right"
398
+    if re.search(r"^\(TAYS\)", r[0]):
399
+        r[4][3] = "middle"
400
+
401
+all_title = "Tampereen yliopistojen ruokalistat";
402
+all_restaurants = get_restaurants_sorted(unordered);
403
+# move fusion kitchen last
404
+#fusion = splice(@all_restaurants, 1, 1);
405
+#splice(@all_restaurants, 4, 0, @fusion);
406
+
407
+write_all_days(all_restaurants, "", all_title);
408
+write_table(all_restaurants, "", all_title);

+ 75 - 0
juvenes.py View File

@@ -0,0 +1,75 @@
1
+# encoding: UTF-8
2
+import datetime
3
+import urllib.request
4
+import urllib.error
5
+import json
6
+import re
7
+import os
8
+
9
+# last two in order are $kitchen_info_id, $menu_type_id
10
+restaurant_info = [
11
+  [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60 ],
12
+  [ "(TaY) Yliopiston Ravintola / VegeBar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "", "left", 13, 5 ],
13
+  [ "(TaY) Café Campus", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9campus.aspx", "", "left", 130019, 23 ],
14
+  [ "(TaY) Café Pinni", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 130016, 60 ],
15
+  [ "(TAYS) Arvo", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/arvo.aspx", "M", "left", 5, 60 ],
16
+  [ "(TAYS) Café Lea (Fusion Kitchen)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 3 ],
17
+  [ "(TAYS) Café Lea (My Salad)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 76 ],
18
+  [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60],
19
+  [ "(TTY) Café Konehuone / Såås bar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone/s%C3%A5%C3%A5sbar.aspx", "", "left", 60038, 77],
20
+  [ "(TTY) Café Konehuone / Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone/fusionkitchen.aspx", "", "middle", 60038, 3 ],
21
+  [ "(TAMK) Ziberia", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 60 ],
22
+#  [ "(TAMK) Frenckell", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/frenckell.aspx", "", "middle", 33, 60 ],
23
+  [ "(TAMK) Frenckell / Såås bar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/frenckell.aspx", "", "middle", 33, 77 ]
24
+]
25
+
26
+
27
+def get_restaurants(use_old):
28
+    count = 0
29
+    restaurants = []
30
+    for info in restaurant_info:
31
+        kitchen = info[4]
32
+        menutype = info[5]
33
+        title = info[0]
34
+        cur_title = title
35
+        open_hours = ""
36
+        week_foods = {}
37
+        week = datetime.datetime.now().isocalendar()[1]
38
+        for weekday in range(1,7):
39
+            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)
40
+            temp_fname = "juvenes_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
41
+            if not use_old and not os.path.isfile(temp_fname):
42
+                try:
43
+                    urllib.request.urlretrieve(url, temp_fname)
44
+                except urllib.error.HTTPError as e:
45
+                    print("Failed to download {url}".format(url=url))
46
+                    # Juvenes may fail with error code 500 if food is not available
47
+            try:
48
+                with open(temp_fname, "r") as fin:
49
+                    jsonp = fin.read()
50
+                    data = json.loads(jsonp[1:-2])
51
+            except OSError as e:
52
+                continue
53
+            if data and data["d"] != "null":
54
+                data = json.loads(data["d"])
55
+                cur_day_foods = []
56
+                mealoptions = data["MealOptions"]
57
+                for meal_info in mealoptions:
58
+                    cur_food = []
59
+                    if "ForceMajoure" in meal_info and meal_info["ForceMajoure"] != "":
60
+                        cur_food = [meal_info["ForceMajoure"]]
61
+                    else:
62
+                        menuitems = meal_info["MenuItems"]
63
+                        for food_info in menuitems:
64
+                            name = food_info["Name"]
65
+                            name = re.sub(r"^\*", "", name)
66
+                            if food_info["Diets"]:
67
+                                cur_food.append("{name} ({diets})".format(name=name, diets=food_info["Diets"]))
68
+                            else:
69
+                                cur_food.append(name)
70
+                    if cur_food != ["-"]:
71
+                        cur_day_foods.append("\n".join(cur_food))
72
+                week_foods[weekday-1] = cur_day_foods
73
+        restaurants.append([title, open_hours, week, week_foods, info])
74
+        count += 1
75
+    return restaurants

+ 12 - 0
pikante.py View File

@@ -0,0 +1,12 @@
1
+# encoding: UTF-8
2
+
3
+pikante_url = "http://www.pikante.fi/lounaslistat-pdf";
4
+
5
+restaurant_info = [
6
+  [ "(TAYS) Finn-Medi", "$pky_url", "all", "middle" ],
7
+  [ "(TAYS) Café Olive", "$pky_url", "all", "middle" ],
8
+  [ "(TAYS) Ellipsi", "$pky_url", "all", "middle" ]
9
+]
10
+
11
+def get_restaurants(use_old):
12
+    return []

+ 8 - 0
sodexo.py View File

@@ -0,0 +1,8 @@
1
+restaurant_info = [
2
+  [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "", "right", 92],
3
+  #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "", "left", 100]
4
+  [ "(TTY) Sodexo Hertsi", "http://www.sodexo.fi/tty-tietotalo", "", "right", 12812]
5
+]
6
+
7
+def get_restaurants(use_old):
8
+    return []