PNA.fi koodi

amica.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import pnalib
  2. import datetime
  3. restaurant_info = [
  4. [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0815&language=fi" ],
  5. [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0811&language=fi" ],
  6. [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "https://www.amica.fi/modules/json/json/Index?costNumber=0812&language=fi" ]
  7. ]
  8. def get_restaurants(use_old, week):
  9. today = datetime.date.today()
  10. week_day = today.isocalendar()[2]
  11. this_monday = today - datetime.timedelta(days=week_day-1)
  12. date_strings = []
  13. for i in range(7):
  14. date = this_monday + datetime.timedelta(days=i)
  15. date_strings.append(date.strftime("%Y-%m-%d"))
  16. restaurants = []
  17. for count, info in enumerate(restaurant_info):
  18. title = info[0]
  19. url = info[4]
  20. temp_fname = "amica_{count}.temp.js".format(count = count)
  21. data = pnalib.get_json_file(url, temp_fname, use_old)
  22. if not data:
  23. continue
  24. week_foods = {}
  25. lunch_menus = data["MenusForDays"]
  26. for lunch_menu in lunch_menus:
  27. date = lunch_menu["Date"].split("T")[0]
  28. try:
  29. week_day = date_strings.index(date)
  30. except:
  31. continue
  32. current_day_foods = []
  33. set_menus = lunch_menu["SetMenus"]
  34. html = lunch_menu.get("Html", "")
  35. if len(html):
  36. current_day_foods.append(handle_html(html))
  37. else:
  38. for set_menu in set_menus:
  39. meals = set_menu["Components"]
  40. food = []
  41. for meal in meals:
  42. food.append(format_meal_allergies(meal))
  43. current_day_foods.append("\n".join(food))
  44. week_foods[week_day] = current_day_foods
  45. restaurants.append([title, "", week, week_foods, info])
  46. return restaurants
  47. # Onko enää tarpeellinen?
  48. def handle_html(html):
  49. menus = html.split("<p>")
  50. for set_menu in menus:
  51. meals = set_menu.split("<br />")
  52. food = []
  53. for meal in meals:
  54. food.append(format_meal_allergies(meal))
  55. return "\n".join(food)
  56. def format_meal_allergies(meal):
  57. parts = meal.split("(")
  58. current_food = parts[0]
  59. diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
  60. if diets:
  61. current_food += " ({allergies})".format(allergies=", ".join(diets))
  62. return current_food