import pnalib import datetime restaurant_info = [ [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "https://www.amica.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] temp_fname = "amica_{count}.temp.js".format(count = count) data = pnalib.get_json_file(url, temp_fname, use_old) if not data: continue week_foods = {} lunch_menus = data["MenusForDays"] if not lunch_menus: continue for lunch_menu in lunch_menus: date = lunch_menu["Date"].split("T")[0] try: week_day = date_strings.index(date) except: 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)) current_day_foods.append("\n".join(food)) week_foods[week_day] = current_day_foods restaurants.append([title, "", week, week_foods, info]) return restaurants # 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