PNA.fi koodi

amica.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import pnalib
  2. import datetime
  3. restaurant_info = [
  4. [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=7381" ],
  5. [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=6655" ],
  6. [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ]
  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. week_date = this_monday.strftime("%Y-%m-%d")
  13. restaurants = []
  14. for count, info in enumerate(restaurant_info):
  15. title = info[0]
  16. url = info[4]
  17. temp_fname = "amica_{count}.temp.js".format(count = count)
  18. url = "{url}&weekDate={week_date}".format(url=url, week_date=week_date)
  19. data = pnalib.get_json_file(url, temp_fname, use_old)
  20. if not data:
  21. continue
  22. week_foods = {}
  23. lunch_menus = data["LunchMenus"]
  24. for week_day, lunch_menu in enumerate(lunch_menus):
  25. current_day_foods = []
  26. set_menus = lunch_menu["SetMenus"]
  27. html = lunch_menu["Html"]
  28. if len(html):
  29. menus = html.split("<p>")
  30. for set_menu in menus:
  31. meals = set_menu.split("<br />")
  32. food = []
  33. for meal in meals:
  34. parts = meal.split("(")
  35. current_food = parts[0]
  36. diets = [s.strip() for s in parts.split(")")[0].split(",")]
  37. if diets:
  38. current_food += " ({allergies})".format(allergies=", ".join(diets))
  39. food.append(current_food)
  40. current_day_foods.append("\n".join(food))
  41. else:
  42. for set_menu in set_menus:
  43. meals = set_menu["Meals"]
  44. food = []
  45. for meal in meals:
  46. current_food = meal["Name"]
  47. if "Diets" in meal:
  48. current_food += " ({allergies})".format(allergies=", ".join(meal["Diets"]))
  49. food.append(current_food)
  50. current_day_foods.append("\n".join(food))
  51. week_foods[week_day] = current_day_foods
  52. restaurants.append([title, "", week, week_foods, info])
  53. return restaurants