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.

135 lines
3.6 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 lmensa(bot, update):
  62. mensa(bot, update, "turm")
  63. def mensa(bot, update, args):
  64. if update.message.chat_id in DISABLED_GROUPS:
  65. return
  66. which = "zentral"
  67. filter_categories = MODES["light"]
  68. diet = [""]
  69. auto_next_day = True
  70. today = datetime.datetime.now().date()
  71. for arg in args:
  72. arg = arg.lower()
  73. if arg in MENSA_NAME:
  74. which = arg
  75. elif arg in MODES:
  76. filter_categories = MODES[arg]
  77. elif arg in WEEKDAYS:
  78. date_offset = (6 - today.weekday() + WEEKDAYS[arg])
  79. today += datetime.timedelta(days=date_offset)
  80. auto_next_day = False
  81. elif arg in DIETS:
  82. diet = DIETS[arg]
  83. else:
  84. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  85. return
  86. if auto_next_day and datetime.datetime.now().time() > datetime.time(hour=16):
  87. today += datetime.timedelta(days=1)
  88. if today.weekday() == 6:
  89. update.message.reply_text("Sonntags hat die Mensa zu")
  90. return
  91. url = "https://app.mensaplan.de/api/11102/de.mensaplan.app.android.goettingen/%s.json" % MENSA_URL[which]
  92. request = requests.get(url)
  93. request.encoding = 'utf-8'
  94. data = request.json()
  95. today_data = get_data_for_day(today, data)
  96. if not today_data:
  97. message = today.strftime("Kein Speiseplan für den %d.%m.%Y vorhanden")
  98. else:
  99. message = "Das Essen für %s in der %s \n\n" % (today_data["date"], MENSA_NAME[which])
  100. for cat in today_data['categories']:
  101. label = cat['name']
  102. meals = cat['meals']
  103. if label not in filter_categories and (
  104. any(filtered in label for filtered in diet) or
  105. [item['name'].replace('\xad', '') for item in meals if
  106. any(filtered in item['name'].replace('\xad', '') for filtered in diet)]): # TODO: simplify
  107. meal_line = "*%s*\n" % label
  108. for meal in meals:
  109. meal_line += meal['name'].strip() + "\n"
  110. message += meal_line + '\n'
  111. update.message.reply_text(message, ParseMode.MARKDOWN)
  112. def get_data_for_day(day, complete_data):
  113. for data in complete_data['days']:
  114. if day.isoformat() == data['iso-date']:
  115. return data