PNA.fi koodi

juvenes.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # encoding: UTF-8
  2. import pnalib
  3. import datetime
  4. import re
  5. import json
  6. # Last three columns are Kitchen Info ID, Menu Type ID and Concept ID. There may be multiple concepts in a single restauranta.
  7. # Juvenes API defines also Restaurant ID but that is not used.
  8. restaurant_info = [
  9. [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60, []],
  10. [ "(TaY) Yliopiston Ravintola / VegeBar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "", "left", 13, 5, []],
  11. [ "(TaY) Café Campus", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9campus.aspx", "", "left", 130019, 23, []],
  12. [ "(TaY) Café Pinni", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 130016, 60, []],
  13. [ "(TAYS) Arvo", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/arvo.aspx", "M", "left", 5, 60, []],
  14. [ "(TAYS) Café Lea (Fusion Kitchen)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 3, []],
  15. [ "(TAYS) Café Lea (My Salad)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 76, []],
  16. [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60, [1149]],
  17. [ "(TTY) Café Konehuone / Såås bar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "left", 60038, 77, [3663]],
  18. [ "(TTY) Café Konehuone / Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "middle", 60038, 3, [1674]],
  19. [ "(TAMK) Ziberia (Koto my plate)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 82, []],
  20. [ "(TAMK) Ziberia (Koto)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 79, []],
  21. [ "(TAMK) Ziberia (My Salad)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 76, []],
  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. concepts = info[6]
  30. title = info[0]
  31. cur_title = title
  32. open_hours = ""
  33. exception = None
  34. week_foods = {}
  35. for weekday in range(1,7):
  36. 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)
  37. temp_fname = "juvenes_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
  38. data = pnalib.get_jsonp_file(url, temp_fname, use_old, allow_old=False)
  39. if not data:
  40. # Try to find problem
  41. for concept in concepts:
  42. url = "http://www.juvenes.fi/DesktopModules/Talents.Restaurants/RestaurantsService.svc/GetConcept?menuId={concept}&lang=fi".format(concept=concept)
  43. temp_fname = "juvenes_{count}-{weekday}-{concept}.temp.js".format(count=count, weekday=weekday, concept=concept)
  44. data = pnalib.get_json_file(url, temp_fname, use_old, allow_old=False)
  45. if data and data["d"]:
  46. exception = data["d"]["OpenInfo"]["Exeption1InfoText"]
  47. elif data and data["d"] != "null":
  48. data = json.loads(data["d"])
  49. cur_day_foods = []
  50. mealoptions = data["MealOptions"]
  51. for meal_info in mealoptions:
  52. cur_food = []
  53. if "ForceMajoure" in meal_info and meal_info["ForceMajoure"] != "":
  54. cur_food = [meal_info["ForceMajoure"]]
  55. else:
  56. menuitems = meal_info["MenuItems"]
  57. for food_info in menuitems:
  58. name = food_info["Name"]
  59. name = re.sub(r"^\*", "", name)
  60. if food_info["Diets"]:
  61. cur_food.append("{name} ({diets})".format(name=name, diets=food_info["Diets"]))
  62. elif name:
  63. cur_food.append(name)
  64. if cur_food != ["-"]:
  65. cur_day_foods.append("\n".join(cur_food))
  66. week_foods[weekday-1] = cur_day_foods
  67. restaurants.append([title, open_hours, week, week_foods, info, exception])
  68. return restaurants