import datetime import os import urllib.request import urllib.error import json restaurant_info = [ [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "", "right", 92], #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "", "left", 100] [ "(TTY) Sodexo Hertsi", "http://www.sodexo.fi/tty-tietotalo", "", "right", 12812] ] def get_restaurants(use_old, week): restaurants = [] for count, info in enumerate(restaurant_info): kitchen = info[4] title = info[0] open_hours = "" week_foods = {} today = datetime.date.today() week_day = today.isocalendar()[2] last_sunday = today - datetime.timedelta(days=week_day) for weekday in range(1,7): date = last_sunday + datetime.timedelta(days=weekday) timestr = date.strftime("%Y/%m/%d") url = "http://www.sodexo.fi/ruokalistat/output/daily_json/{kitchen}/{timestr}/fi".format(kitchen=kitchen, timestr=timestr) temp_fname = "sodexo_{count}-{weekday}.temp.js".format(count=count, weekday=weekday) 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 current_day_foods = [] courses = data["courses"] for course_info in courses: print(course_info) if course_info["category"] != "Aamupuuro": food = course_info["title_fi"] if "properties" in course_info: food += " ({allergies})".format(allergies=course_info["properties"]) current_day_foods.append(food) week_foods[weekday-1] = current_day_foods restaurants.append([title, open_hours, week, week_foods, info]) return restaurants