PNA.fi koodi

sodexo.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import datetime
  2. import os
  3. import urllib.request
  4. import urllib.error
  5. import json
  6. restaurant_info = [
  7. [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "", "right", 92],
  8. #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "", "left", 100]
  9. [ "(TTY) Sodexo Hertsi", "http://www.sodexo.fi/tty-tietotalo", "", "right", 12812]
  10. ]
  11. def get_restaurants(use_old, week):
  12. restaurants = []
  13. for count, info in enumerate(restaurant_info):
  14. kitchen = info[4]
  15. title = info[0]
  16. open_hours = ""
  17. week_foods = {}
  18. today = datetime.date.today()
  19. week_day = today.isocalendar()[2]
  20. last_sunday = today - datetime.timedelta(days=week_day)
  21. for weekday in range(1,7):
  22. date = last_sunday + datetime.timedelta(days=weekday)
  23. timestr = date.strftime("%Y/%m/%d")
  24. url = "http://www.sodexo.fi/ruokalistat/output/daily_json/{kitchen}/{timestr}/fi".format(kitchen=kitchen, timestr=timestr)
  25. temp_fname = "sodexo_{count}-{weekday}.temp.js".format(count=count, weekday=weekday)
  26. if not use_old or not os.path.isfile(temp_fname):
  27. try:
  28. urllib.request.urlretrieve(url, temp_fname)
  29. except urllib.error.HTTPError as e:
  30. print("Failed to download {url}".format(url=url))
  31. try:
  32. with open(temp_fname, "r", encoding="utf-8") as fin:
  33. data = json.load(fin)
  34. except OSError as e:
  35. continue
  36. current_day_foods = []
  37. courses = data["courses"]
  38. for course_info in courses:
  39. print(course_info)
  40. if course_info["category"] != "Aamupuuro":
  41. food = course_info["title_fi"]
  42. if "properties" in course_info:
  43. food += " ({allergies})".format(allergies=course_info["properties"])
  44. current_day_foods.append(food)
  45. week_foods[weekday-1] = current_day_foods
  46. restaurants.append([title, open_hours, week, week_foods, info])
  47. return restaurants