PNA.fi koodi

amica.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import pnalib
  2. import datetime
  3. restaurant_info = [
  4. [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "https://www.amica.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. temp_fname = "amica_{count}.temp.js".format(count = count)
  21. data = pnalib.get_json_file(url, temp_fname, use_old)
  22. if not data:
  23. continue
  24. week_foods = {}
  25. lunch_menus = data["MenusForDays"]
  26. if not lunch_menus:
  27. continue
  28. for lunch_menu in lunch_menus:
  29. date = lunch_menu["Date"].split("T")[0]
  30. try:
  31. week_day = date_strings.index(date)
  32. except:
  33. continue
  34. current_day_foods = []
  35. set_menus = lunch_menu["SetMenus"]
  36. html = lunch_menu.get("Html", "")
  37. if len(html):
  38. current_day_foods.append(handle_html(html))
  39. else:
  40. for set_menu in set_menus:
  41. meals = set_menu["Components"]
  42. food = []
  43. for meal in meals:
  44. food.append(format_meal_allergies(meal))
  45. current_day_foods.append("\n".join(food))
  46. week_foods[week_day] = current_day_foods
  47. restaurants.append([title, "", week, week_foods, info])
  48. return restaurants
  49. # Onko enää tarpeellinen?
  50. def handle_html(html):
  51. menus = html.split("<p>")
  52. for set_menu in menus:
  53. meals = set_menu.split("<br />")
  54. food = []
  55. for meal in meals:
  56. food.append(format_meal_allergies(meal))
  57. return "\n".join(food)
  58. def format_meal_allergies(meal):
  59. parts = meal.split("(")
  60. current_food = parts[0]
  61. diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
  62. if diets:
  63. current_food += " ({allergies})".format(allergies=", ".join(diets))
  64. return current_food