From 3442e96c23607c5f010a988947e8a8a506a4d6bb Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Mon, 28 Jan 2019 14:31:38 +0100 Subject: [PATCH] splitted files, started using the 'official' api --- db.py | 2 + mensa.py | 111 +++++++++++++++++++++++++++++++++++++++++++++ schneiderbot.py | 118 +----------------------------------------------- 3 files changed, 115 insertions(+), 116 deletions(-) create mode 100644 mensa.py diff --git a/db.py b/db.py index 2d88653..196ee93 100644 --- a/db.py +++ b/db.py @@ -1,8 +1,10 @@ """DB interface""" import sqlite3 + class Db(): """ Abstract interface for db actions """ + def __init__(self): self.conn = sqlite3.connect("deaths.db") table_exist = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='death'") diff --git a/mensa.py b/mensa.py new file mode 100644 index 0000000..76c9d3b --- /dev/null +++ b/mensa.py @@ -0,0 +1,111 @@ +import datetime +from telegram import ParseMode +import requests + +DIETS = { + "veggy": "fleischlos", + "fleisch": "mit Fleisch", + "fisch": "mit Fisch/Meeresfrüchten" +} + +WEEKDAYS = { + "montag": 1, + "dienstag": 2, + "mittwoch": 3, + "donnerstag": 4, + "freitag": 5, + "samstag": 6, +} + +MENSA_URL = { + "zentral": "goe1", + "turm": "goe2", + "nord": "goe3", + "italia": "goe4", + "fasthochschule": "goe5" +} + +MENSA_NAME = { + "zentral": "Zentralmensa", + "nord": "Nordmensa", + "turm": "Turmmensa", + "italia": "Mensa Italia", + "fasthochschule": "Bistro Fasthochschule" +} + +HIDE_CATEGORIES_LIGHT = { + "CampusCurry", + "natürlich fit", + "Fitnesscenter", + "Salatbuffet", + "Studentenfutter", + "Süße Versuchung", + "Süße Spezial Tagesangebot", + "Vollwert & Co. Stärke", + "Vollwert & Co. Gemüse", + "Bio-Beilagen", + "Dessertbuffet", + "Last Minute ab 14:30 Uhr", + ## Nordmensa: + "Salatbuffet/Pastapoint", + "Last Minute ab 13:30 Uhr", + ## Turmmensa: + "Beilagen", + "Last Minute ab 14:00Uhr" +} + +HIDE_CATEGORIES_FULL = { + "Last Minute ab 14:30 Uhr", + "Last Minute ab 13:30 Uhr", + "Last Minute ab 14:00Uhr" +} + +MODES = { + "light": HIDE_CATEGORIES_LIGHT, + "full": HIDE_CATEGORIES_FULL +} + + +def mensa(bot, update, args): + which = "zentral" + filter_categories = MODES["light"] + offset = 0 + # today = datetime.datetime.now().date().weekday() + 1 + if datetime.datetime.now().time() > datetime.time(hour=16): + offset += 1 + # + # today = today % 7 + + for arg in args: + arg = arg.lower() + if arg in MENSA_NAME: + which = arg + elif arg in MODES: + filter_categories = MODES[arg] + elif arg in WEEKDAYS: + today = WEEKDAYS[arg] + # 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 + + url = "https://app.mensaplan.de/api/11102/de.mensaplan.app.android.goettingen/%s.json" % MENSA_URL[which] + request = requests.get(url) + request.encoding = 'utf-8' + data = request.json() + + today_data = data['days'][offset] + + 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) diff --git a/schneiderbot.py b/schneiderbot.py index c7db496..1670e38 100755 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -4,78 +4,14 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram import ParseMode from db import Db +from mensa import mensa import logging -import os import random import requests -import datetime -import cat import pydog -DIETS = { - "veggy": "fleischlos", - "fleisch": "mit Fleisch", - "fisch": "mit Fisch/ Meeresfrüchten" -} - -WEEKDAYS = { - "montag": 1, - "dienstag": 2, - "mittwoch": 3, - "donnerstag": 4, - "freitag": 5, - "samstag": 6 -} - -MENSA_URL = { - "zentral": "zentralmensa", - "nord": "nordmensa", - "turm": "turmmensa", - "italia": "mensaitalia", - "fasthochschule": "bistrohawk" -} - -MENSA_NAME = { - "zentral": "Zentralmensa", - "nord": "Nordmensa", - "turm": "Turmmensa", - "italia": "Mensa Italia", - "fasthochschule": "Bistro Fasthochschule" -} - -HIDE_CATEGORIES_LIGHT = { - "CampusCurry", - "natürlich fit", - "Fitnesscenter", - "Salatbuffet", - "Studentenfutter", - "Süße Versuchung", - "Süße Spezial Tagesangebot", - "Vollwert & Co. Stärke", - "Vollwert & Co. Gemüse", - "Bio-Beilagen", - "Dessertbuffet", - "Last Minute ab 14:30 Uhr", - ## Nordmensa: - "Salatbuffet/Pastapoint", - "Last Minute ab 13:30 Uhr", - ## Turmmensa: - "Beilagen", - "Last Minute ab 14:00Uhr" -} - -HIDE_CATEGORIES_FULL = { - "Last Minute ab 14:30 Uhr", - "Last Minute ab 13:30 Uhr", - "Last Minute ab 14:00Uhr" -} - -MODES = { - "light": HIDE_CATEGORIES_LIGHT, - "full": HIDE_CATEGORIES_FULL -} - # Enable logging + logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) @@ -234,56 +170,6 @@ def simon(bot, update, args): % (name, animal)) -def mensa(bot, update, args): - which = "zentral" - filter_categories = MODES["light"] - diet = "" - today = datetime.datetime.now().date().weekday() + 1 - if datetime.datetime.now().time() > datetime.time(hour=16): - # Es ist zu spät am Tag, zeig das essen für morgen an - today += 1 - - today = today % 7 - for arg in args: - arg = arg.lower() - if arg in MENSA_NAME: - which = arg - elif arg in MODES: - filter_categories = MODES[arg] - elif arg in WEEKDAYS: - today = WEEKDAYS[arg] - 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 - url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today) - request = requests.get(url) - request.encoding = 'utf-8' - data = request.json() - - message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which]) - - if len(data["meals"]) > 1: - for meal in data["meals"]: - if meal["category"] not in filter_categories and diet in meal["diet"]: - meal_line = "*%s*\n" % meal["category"] - meal_line += meal["title"].strip() + "\n" - - # Discord only allows up to 2000 chars per message - if len(message) + len(meal_line) > 2000: - update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN) - message = meal_line + "\n" - else: - message += meal_line + "\n" - update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN) - else: - update.message.reply_text("Nix zu futtern heute :(") - - def echo(bot, update): """Echo the user message.""" update.message.reply_text(update.message.text)