Telegram version of schneiderbot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
  1. import datetime
  2. from telegram import ParseMode
  3. import requests
  4. DISABLED_GROUPS = [-1001301570558]
  5. DIETS = {
  6. "veggy": ["Vegetarisch", "Vegan"],
  7. "vegan": ["Vegan"]
  8. }
  9. WEEKDAYS = {
  10. "montag": 1,
  11. "dienstag": 2,
  12. "mittwoch": 3,
  13. "donnerstag": 4,
  14. "freitag": 5,
  15. "samstag": 6,
  16. "sonntag": 7
  17. }
  18. MENSA_URL = {
  19. "zentral": "goe1",
  20. "turm": "goe2",
  21. "nord": "goe3",
  22. "italia": "goe4",
  23. "fasthochschule": "goe5"
  24. }
  25. MENSA_NAME = {
  26. "zentral": "Zentralmensa",
  27. "nord": "Nordmensa",
  28. "turm": "Turmmensa",
  29. "italia": "Mensa Italia",
  30. "fasthochschule": "Bistro Fasthochschule"
  31. }
  32. HIDE_CATEGORIES_LIGHT = {
  33. "CampusCurry",
  34. "natürlich fit",
  35. "Fitnesscenter",
  36. "Salatbuffet",
  37. "Studentenfutter",
  38. "Süße Versuchung",
  39. "Süße Spezial Tagesangebot",
  40. "Vollwert & Co. Stärke",
  41. "Vollwert & Co. Gemüse",
  42. "Bio-Beilagen",
  43. "Dessertbuffet",
  44. "Last Minute ab 14:30 Uhr",
  45. ## Nordmensa:
  46. "Salatbuffet/Pastapoint",
  47. "Last Minute ab 13:30 Uhr",
  48. ## Turmmensa:
  49. "Beilagen",
  50. "Last Minute ab 14:00Uhr"
  51. }
  52. HIDE_CATEGORIES_FULL = {
  53. "Last Minute ab 14:30 Uhr",
  54. "Last Minute ab 13:30 Uhr",
  55. "Last Minute ab 14:00Uhr"
  56. }
  57. MODES = {
  58. "light": HIDE_CATEGORIES_LIGHT,
  59. "full": HIDE_CATEGORIES_FULL
  60. }
  61. def mensa(bot, update, args):
  62. if update.message.chat_id in DISABLED_GROUPS:
  63. return
  64. which = "zentral"
  65. filter_categories = MODES["light"]
  66. diet = [""]
  67. auto_next_day = True
  68. today = datetime.datetime.now().date()
  69. for arg in args:
  70. arg = arg.lower()
  71. if arg in MENSA_NAME:
  72. which = arg
  73. elif arg in MODES:
  74. filter_categories = MODES[arg]
  75. elif arg in WEEKDAYS:
  76. date_offset = (6 - today.weekday() + WEEKDAYS[arg])
  77. today += datetime.timedelta(days=date_offset)
  78. auto_next_day = False
  79. elif arg in DIETS:
  80. diet = DIETS[arg]
  81. else:
  82. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  83. return
  84. if auto_next_day and datetime.datetime.now().time() > datetime.time(hour=16):
  85. today += datetime.timedelta(days=1)
  86. if today.weekday() == 6:
  87. update.message.reply_text("Sonntags hat die Mensa zu")
  88. return
  89. url = "https://app.mensaplan.de/api/11102/de.mensaplan.app.android.goettingen/%s.json" % MENSA_URL[which]
  90. request = requests.get(url)
  91. request.encoding = 'utf-8'
  92. data = request.json()
  93. today_data = get_data_for_day(today, data)
  94. if not today_data:
  95. message = today.strftime("Kein Speiseplan für den %d.%m.%Y vorhanden")
  96. else:
  97. message = "Das Essen für %s in der %s \n\n" % (today_data["date"], MENSA_NAME[which])
  98. for cat in today_data['categories']:
  99. label = cat['name']
  100. meals = cat['meals']
  101. if label not in filter_categories and (
  102. any(filtered in label for filtered in diet) or
  103. [item['name'].replace('\xad', '') for item in meals if
  104. any(filtered in item['name'].replace('\xad', '') for filtered in diet)]): # TODO: simplify
  105. meal_line = "*%s*\n" % label
  106. for meal in meals:
  107. meal_line += meal['name'].strip() + "\n"
  108. message += meal_line + '\n'
  109. update.message.reply_text(message, ParseMode.MARKDOWN)
  110. def get_data_for_day(day, complete_data):
  111. for data in complete_data['days']:
  112. if day.isoformat() == data['iso-date']:
  113. return data