PNA.fi koodi

amica.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. week_date = this_monday.strftime("%Y-%m-%d")
  13. restaurants = []
  14. for count, info in enumerate(restaurant_info):
  15. title = info[0]
  16. url = info[4]
  17. temp_fname = "amica_{count}.temp.js".format(count = count)
  18. data = pnalib.get_json_file(url, temp_fname, use_old)
  19. if not data:
  20. continue
  21. week_foods = {}
  22. lunch_menus = data["MenusForDays"]
  23. for week_day, lunch_menu in enumerate(lunch_menus):
  24. current_day_foods = []
  25. set_menus = lunch_menu["SetMenus"]
  26. html = lunch_menu.get("Html", "")
  27. if len(html):
  28. current_day_foods.append(handle_html(html))
  29. else:
  30. for set_menu in set_menus:
  31. meals = set_menu["Components"]
  32. food = []
  33. for meal in meals:
  34. food.append(format_meal_allergies(meal))
  35. current_day_foods.append("\n".join(food))
  36. week_foods[week_day] = current_day_foods
  37. restaurants.append([title, "", week, week_foods, info])
  38. return restaurants
  39. # Onko enää tarpeellinen?
  40. def handle_html(html):
  41. menus = html.split("<p>")
  42. for set_menu in menus:
  43. meals = set_menu.split("<br />")
  44. food = []
  45. for meal in meals:
  46. food.append(format_meal_allergies(meal))
  47. return "\n".join(food)
  48. def format_meal_allergies(meal):
  49. parts = meal.split("(")
  50. current_food = parts[0]
  51. diets = [s.strip() for s in parts[1].split(")")[0].split(",")]
  52. if diets:
  53. current_food += " ({allergies})".format(allergies=", ".join(diets))
  54. return current_food