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}}