From be33a203c9c4914e5ae4fc0e9437f6df792d79fb Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Sat, 9 Feb 2019 11:50:52 +0100 Subject: [PATCH] fixed mensa with given day --- mensa.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/mensa.py b/mensa.py index 9859438..d6e6504 100644 --- a/mensa.py +++ b/mensa.py @@ -17,6 +17,7 @@ WEEKDAYS = { "donnerstag": 4, "freitag": 5, "samstag": 6, + "sonntag": 7 } MENSA_URL = { @@ -73,9 +74,8 @@ def mensa(bot, update, args): return which = "zentral" filter_categories = MODES["light"] + auto_next_day = True today = datetime.datetime.now().date() - if datetime.datetime.now().time() > datetime.time(hour=16): - today += datetime.timedelta(days=1) for arg in args: arg = arg.lower() @@ -84,15 +84,21 @@ def mensa(bot, update, args): elif arg in MODES: filter_categories = MODES[arg] elif arg in WEEKDAYS: - today = WEEKDAYS[arg] + date_offset = (6 - today.weekday() + WEEKDAYS[arg]) + today += datetime.timedelta(days=date_offset) + auto_next_day = False # elif arg in DIETS: # diet = DIETS[arg] else: update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.") return - # if today == 0: - # update.message.reply_text("Sonntags hat die Mensa zu") - # return + + if auto_next_day and datetime.datetime.now().time() > datetime.time(hour=16): + today += datetime.timedelta(days=1) + + if today.weekday() == 6: + update.message.reply_text("Sonntags hat die Mensa zu") + return url = "https://app.mensaplan.de/api/11102/de.mensaplan.app.android.goettingen/%s.json" % MENSA_URL[which] request = requests.get(url) @@ -100,13 +106,16 @@ def mensa(bot, update, args): data = request.json() today_data = get_data_for_day(today, data) - message = "Das Essen für %s in der %s \n\n" % (today_data["date"], MENSA_NAME[which]) - for cat in today_data['categories']: - if cat['name'] not in filter_categories: - meal_line = "*%s*\n" % cat['name'] - for meal in cat['meals']: - meal_line += meal['name'].strip() + "\n" - message += meal_line + '\n' + if not today_data: + message = today.strftime("Kein Speiseplan für den %d.%m.%Y vorhanden") + else: + message = "Das Essen für %s in der %s \n\n" % (today_data["date"], MENSA_NAME[which]) + for cat in today_data['categories']: + if cat['name'] not in filter_categories: + meal_line = "*%s*\n" % cat['name'] + for meal in cat['meals']: + meal_line += meal['name'].strip() + "\n" + message += meal_line + '\n' update.message.reply_text(message, ParseMode.MARKDOWN)