PNA.fi koodi

amica.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2018 Toni Fadjukoff. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import pnalib
  15. import datetime
  16. restaurant_info = [
  17. [ "(TaY) Amica Minerva", "https://www.fazerfoodco.fi/ravintolat/Ravintolat-kaupungeittain/tampere/minerva/", "", "middle", "https://www.fazerfoodco.fi/modules/json/json/Index?costNumber=0815&language=fi" ],
  18. #[ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0811&language=fi" ],
  19. [ "(TTY) Ravintola Reaktori", "https://www.fazerfoodco.fi/ravintolat/Ravintolat-kaupungeittain/tampere/reaktori/", "", "middle", "https://www.fazerfoodco.fi/modules/json/json/Index?costNumber=0812&language=fi" ]
  20. ]
  21. def get_restaurants(use_old, week):
  22. today = datetime.date.today()
  23. week_day = today.isocalendar()[2]
  24. this_monday = today - datetime.timedelta(days=week_day-1)
  25. date_strings = []
  26. for i in range(7):
  27. date = this_monday + datetime.timedelta(days=i)
  28. date_strings.append(date.strftime("%Y-%m-%d"))
  29. restaurants = []
  30. for count, info in enumerate(restaurant_info):
  31. title = info[0]
  32. url = info[4]
  33. week_foods = handle_one(use_old, date_strings, count, url)
  34. restaurants.append([title, "", week, week_foods, info])
  35. return restaurants
  36. def handle_one(use_old, date_strings, count, url):
  37. temp_fname = "amica_{count}.temp.json".format(count = count)
  38. week_foods = {}
  39. data = pnalib.get_json_file(url, temp_fname, use_old)
  40. if not data:
  41. print("Fazer no data")
  42. return week_foods
  43. lunch_menus = data["MenusForDays"]
  44. if not lunch_menus:
  45. print("Fazer no MenusForDays")
  46. return week_foods
  47. for lunch_menu in lunch_menus:
  48. date = lunch_menu["Date"].split("T")[0]
  49. try:
  50. week_day = date_strings.index(date)
  51. except:
  52. print("Fazer no date dates=({}), key=({})".format(date_strings, date))
  53. continue
  54. current_day_foods = []
  55. set_menus = lunch_menu["SetMenus"]
  56. html = lunch_menu.get("Html", "")
  57. if len(html):
  58. current_day_foods.append(handle_html(html))
  59. else:
  60. for set_menu in set_menus:
  61. meals = set_menu["Components"]
  62. food = []
  63. for meal in meals:
  64. food.append(format_meal_allergies(meal))
  65. if food:
  66. current_day_foods.append("\n".join(food))
  67. week_foods[week_day] = current_day_foods
  68. return week_foods
  69. # Onko enää tarpeellinen?
  70. def handle_html(html):
  71. menus = html.split("<p>")
  72. for set_menu in menus:
  73. meals = set_menu.split("<br />")
  74. food = []
  75. for meal in meals:
  76. food.append(format_meal_allergies(meal))
  77. return "\n".join(food)
  78. def format_meal_allergies(meal):
  79. parts = meal.split("(")
  80. current_food = parts[0]
  81. diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
  82. if diets:
  83. current_food += " ({allergies})".format(allergies=", ".join(diets))
  84. return current_food