1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import datetime
- import os
- import urllib.request
- import urllib.error
- import json
-
- restaurant_info = [
- [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=7381" ],
- [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=6655" ],
- [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ]
- ]
-
- def get_restaurants(use_old, week):
- today = datetime.date.today()
- week_day = today.isocalendar()[2]
- this_monday = today - datetime.timedelta(days=week_day-1)
- week_date = this_monday.strftime("%Y-%m-%d")
- restaurants = []
- for count, info in enumerate(restaurant_info):
- title = info[0]
- url = info[4]
- temp_fname = "amica_{count}.temp.js".format(count = count)
- url = "{url}&weekDate={week_date}".format(url=url, week_date=week_date)
- if not use_old or not os.path.isfile(temp_fname):
- try:
- urllib.request.urlretrieve(url, temp_fname)
- except urllib.error.HTTPError as e:
- print("Failed to download {url}".format(url=url))
- try:
- with open(temp_fname, "r", encoding="utf-8") as fin:
- data = json.load(fin)
- except OSError as e:
- continue
-
- week_foods = {}
- lunch_menus = data["LunchMenus"]
- for week_day, lunch_menu in enumerate(lunch_menus):
- current_day_foods = []
- set_menus = lunch_menu["SetMenus"]
- html = lunch_menu["Html"]
- if len(html):
- menus = html.split("<p>")
- for set_menu in menus:
- meals = set_menu.split("<br />")
- food = []
- for meal in meals:
- parts = meal.split("(")
- current_food = parts[0]
- diets = [s.strip() for s in parts.split(")")[0].split(",")]
- if diets:
- current_food += " ({allergies})".format(allergies=", ".join(diets))
- food.append(current_food)
- current_day_foods.append("\n".join(food))
- else:
- for set_menu in set_menus:
- meals = set_menu["Meals"]
- food = []
- for meal in meals:
- current_food = meal["Name"]
- if "Diets" in meal:
- current_food += " ({allergies})".format(allergies=", ".join(meal["Diets"]))
- food.append(current_food)
- current_day_foods.append("\n".join(food))
- week_foods[week_day] = current_day_foods
-
- restaurants.append([title, "", week, week_foods, info])
- return restaurants
|