PNA.fi koodi

food.py 18KB

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