PNA.fi koodi

food.py 18KB

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