PNA.fi koodi

amica.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pnalib
  2. import datetime
  3. restaurant_info = [
  4. [ "(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" ],
  5. #[ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0811&language=fi" ],
  6. [ "(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" ]
  7. ]
  8. def get_restaurants(use_old, week):
  9. today = datetime.date.today()
  10. week_day = today.isocalendar()[2]
  11. this_monday = today - datetime.timedelta(days=week_day-1)
  12. date_strings = []
  13. for i in range(7):
  14. date = this_monday + datetime.timedelta(days=i)
  15. date_strings.append(date.strftime("%Y-%m-%d"))
  16. restaurants = []
  17. for count, info in enumerate(restaurant_info):
  18. title = info[0]
  19. url = info[4]
  20. week_foods = handle_one(use_old, date_strings, count, url)
  21. restaurants.append([title, "", week, week_foods, info])
  22. return restaurants
  23. def handle_one(use_old, date_strings, count, url):
  24. temp_fname = "amica_{count}.temp.json".format(count = count)
  25. week_foods = {}
  26. data = pnalib.get_json_file(url, temp_fname, use_old)
  27. if not data:
  28. print("Fazer no data")
  29. return week_foods
  30. lunch_menus = data["MenusForDays"]
  31. if not lunch_menus:
  32. print("Fazer no MenusForDays")
  33. return week_foods
  34. for lunch_menu in lunch_menus:
  35. date = lunch_menu["Date"].split("T")[0]
  36. try:
  37. week_day = date_strings.index(date)
  38. except:
  39. print("Fazer no date dates=({}), key=({})".format(date_strings, date))
  40. continue
  41. current_day_foods = []
  42. set_menus = lunch_menu["SetMenus"]
  43. html = lunch_menu.get("Html", "")
  44. if len(html):
  45. current_day_foods.append(handle_html(html))
  46. else:
  47. for set_menu in set_menus:
  48. meals = set_menu["Components"]
  49. food = []
  50. for meal in meals:
  51. food.append(format_meal_allergies(meal))
  52. if food:
  53. current_day_foods.append("\n".join(food))
  54. week_foods[week_day] = current_day_foods
  55. return week_foods
  56. # Onko enää tarpeellinen?
  57. def handle_html(html):
  58. menus = html.split("<p>")
  59. for set_menu in menus:
  60. meals = set_menu.split("<br />")
  61. food = []
  62. for meal in meals:
  63. food.append(format_meal_allergies(meal))
  64. return "\n".join(food)
  65. def format_meal_allergies(meal):
  66. parts = meal.split("(")
  67. current_food = parts[0]
  68. diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
  69. if diets:
  70. current_food += " ({allergies})".format(allergies=", ".join(diets))
  71. return current_food