PNA.fi koodi

juvenes.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # encoding: UTF-8
  2. # Copyright 2018 Toni Fadjukoff. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import pnalib
  16. import datetime
  17. import re
  18. import json
  19. # Last three columns are Kitchen Info ID, Menu Type ID and Concept ID. There may be multiple concepts in a single restauranta.
  20. # Juvenes API defines also Restaurant ID but that is not used.
  21. restaurant_info = [
  22. [ "(TaY) Yliopiston Ravintola",
  23. "https://www.juvenes.fi/yoravintola",
  24. "M", "left", 13, 60, []],
  25. [ "(TaY) Yliopiston Ravintola / VegeBar",
  26. "https://www.juvenes.fi/yoravintola",
  27. "", "left", 13, 5, []],
  28. [ "(TaY) Café Alakuppila (Paniini & Combosalaatti)",
  29. "https://www.juvenes.fi/alakuppila",
  30. "", "left", 130018, 58, []],
  31. [ "(TaY) Café Pinni",
  32. "https://www.juvenes.fi/pinni",
  33. "M", "middle", 130016, 60, []],
  34. [ "(TAYS) Arvo",
  35. "https://www.juvenes.fi/arvo",
  36. "M", "left", 5, 60, []],
  37. [ "(TAYS) Café Lea (Fusion Kitchen)",
  38. "https://www.juvenes.fi/lea",
  39. "M", "left", 50026, 3, []],
  40. # [ "(TAYS) Café Lea (My Salad)",
  41. # "https://www.juvenes.fi/lea",
  42. # "M", "left", 50026, 76, []],
  43. [ "(TTY) Newton",
  44. "https://www.juvenes.fi/newton",
  45. "", "left", 6, 60, [1149]],
  46. [ "(TTY) Café Konehuone / Såås bar",
  47. "https://www.juvenes.fi/konehuone",
  48. "", "left", 60038, 77, [3663]],
  49. [ "(TTY) Café Konehuone / Fusion Kitchen",
  50. "https://www.juvenes.fi/konehuone",
  51. "", "middle", 60038, 3, [1674]],
  52. [ "(TAMK) Ziberia (Koto my plate)",
  53. "https://www.juvenes.fi/ziberia",
  54. "", "middle", 11, 82, []],
  55. [ "(TAMK) Ziberia (Koto)",
  56. "https://www.juvenes.fi/ziberia",
  57. "", "middle", 11, 79, []],
  58. [ "(TAMK) Ziberia (My Salad)",
  59. "https://www.juvenes.fi/ziberia",
  60. "", "middle", 11, 76, []],
  61. [ "(TAMK) Frenckell / Koto",
  62. "https://www.juvenes.fi/frenckell",
  63. "", "middle", 33, 60, []],
  64. [ "(TAMK) Frenckell / Iltapäivälounas",
  65. "https://www.juvenes.fi/frenckell",
  66. "", "middle", 33, 85, []]
  67. ]
  68. def get_restaurants(use_old, week):
  69. restaurants = []
  70. for count, info in enumerate(restaurant_info):
  71. kitchen = info[4]
  72. menutype = info[5]
  73. concepts = info[6]
  74. title = info[0]
  75. cur_title = title
  76. open_hours = ""
  77. exception = None
  78. week_foods = {}
  79. for weekday in range(1,7):
  80. 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)
  81. temp_fname = "juvenes_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
  82. data = pnalib.get_json_file(url, temp_fname, use_old, allow_old=False)
  83. if not data:
  84. # Try to find problem
  85. for concept in concepts:
  86. url = "http://www.juvenes.fi/DesktopModules/Talents.Restaurants/RestaurantsService.svc/GetConcept?menuId={concept}&lang=fi".format(concept=concept)
  87. temp_fname = "juvenes_{count}-{weekday}-{concept}.temp.js".format(count=count, weekday=weekday, concept=concept)
  88. data = pnalib.get_json_file(url, temp_fname, use_old, allow_old=False)
  89. if data and data["d"]:
  90. exception = data["d"]["OpenInfo"]["Exeption1InfoText"]
  91. elif data and data["d"] != "null":
  92. data = json.loads(data["d"])
  93. cur_day_foods = []
  94. mealoptions = data["MealOptions"]
  95. for meal_info in mealoptions:
  96. cur_food = []
  97. if "ForceMajoure" in meal_info and meal_info["ForceMajoure"] != "":
  98. cur_food = [meal_info["ForceMajoure"]]
  99. else:
  100. menuitems = meal_info["MenuItems"]
  101. for food_info in menuitems:
  102. name = food_info["Name"]
  103. name = re.sub(r"^\*", "", name)
  104. if food_info["Diets"]:
  105. cur_food.append("{name} ({diets})".format(name=name, diets=food_info["Diets"]))
  106. elif name:
  107. cur_food.append(name)
  108. if cur_food != ["-"]:
  109. cur_day_foods.append("\n".join(cur_food))
  110. week_foods[weekday-1] = cur_day_foods
  111. restaurants.append([title, open_hours, week, week_foods, info, exception])
  112. return restaurants