From 4f4ec345fbe9c100df6f86d722eed0411e4b1ae1 Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Wed, 11 Sep 2024 15:55:58 +0200 Subject: [PATCH] =?UTF-8?q?Bugfix=20-=20Check=20for=20sp=C3=A4tzle=20on=20?= =?UTF-8?q?current=20day=20only=20rather=20than=20full=20week?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + requirements.txt | 3 ++- src/api.py | 24 ++++++++++++++++++------ src/template/main.html | 13 +++++++++---- 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7e3308d..57bf8ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ fastapi[standard]==0.114.0 Jinja2==3.1.4 requests==2.32.3 -cachetools==5.5.0 \ No newline at end of file +cachetools==5.5.0 +beautifulsoup4==4.12.3 \ No newline at end of file diff --git a/src/api.py b/src/api.py index f35d870..a3bdeeb 100644 --- a/src/api.py +++ b/src/api.py @@ -2,8 +2,10 @@ from fastapi import FastAPI from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates -import requests from cachetools import cached, TTLCache +from bs4 import BeautifulSoup +import requests + cache = TTLCache(maxsize=1, ttl=3600) app = FastAPI() @@ -12,13 +14,23 @@ templates = Jinja2Templates(directory="template") @cached(cache) def checkMensa(): print("Checking Mensa") + url = 'https://seezeit.com/essen/speiseplaene/mensa-giessberg/' r = requests.get(url) - text = r.text - pos = text.find('Spätzle') - if pos != -1: - cut = r.text[r.text.find('Spätzle'):r.text.find('Spätzle')+50] - ingredients = cut.split('(')[1].split(')')[0].split(',') + soup = BeautifulSoup(r.text, 'html.parser') + + today = soup.find('a', class_='heute')['rel'][0] + today_menu = soup.find('div', id=f'tab{today}') + meals = today_menu.find_all('div', class_='title') + + spaetzle = None + for m in meals: + if "Spätzle" in m.text: + spaetzle = m + break + + if spaetzle is not None: + ingredients = spaetzle.find('sup').text if '28' in ingredients: answer = "Spätzle with egg today" color = "green" diff --git a/src/template/main.html b/src/template/main.html index 728908a..e96ffff 100644 --- a/src/template/main.html +++ b/src/template/main.html @@ -7,10 +7,10 @@ - {{text}} - {{emoji}} + {{text}} - {{emoji}}