PNA.fi koodi

juvenes.py 4.6KB

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