import pnalib import datetime restaurant_info = [ [ "(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" ], #[ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0811&language=fi" ], [ "(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" ] ] def get_restaurants(use_old, week): today = datetime.date.today() week_day = today.isocalendar()[2] this_monday = today - datetime.timedelta(days=week_day-1) date_strings = [] for i in range(7): date = this_monday + datetime.timedelta(days=i) date_strings.append(date.strftime("%Y-%m-%d")) restaurants = [] for count, info in enumerate(restaurant_info): title = info[0] url = info[4] week_foods = handle_one(use_old, date_strings, count, url) restaurants.append([title, "", week, week_foods, info]) return restaurants def handle_one(use_old, date_strings, count, url): temp_fname = "amica_{count}.temp.json".format(count = count) week_foods = {} data = pnalib.get_json_file(url, temp_fname, use_old) if not data: print("Fazer no data") return week_foods lunch_menus = data["MenusForDays"] if not lunch_menus: print("Fazer no MenusForDays") return week_foods for lunch_menu in lunch_menus: date = lunch_menu["Date"].split("T")[0] try: week_day = date_strings.index(date) except: print("Fazer no date dates=({}), key=({})".format(date_strings, date)) continue current_day_foods = [] set_menus = lunch_menu["SetMenus"] html = lunch_menu.get("Html", "") if len(html): current_day_foods.append(handle_html(html)) else: for set_menu in set_menus: meals = set_menu["Components"] food = [] for meal in meals: food.append(format_meal_allergies(meal)) if food: current_day_foods.append("\n".join(food)) week_foods[week_day] = current_day_foods return week_foods # Onko enää tarpeellinen? def handle_html(html): menus = html.split("
")
for set_menu in menus:
meals = set_menu.split("
")
food = []
for meal in meals:
food.append(format_meal_allergies(meal))
return "\n".join(food)
def format_meal_allergies(meal):
parts = meal.split("(")
current_food = parts[0]
diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
if diets:
current_food += " ({allergies})".format(allergies=", ".join(diets))
return current_food