This commit is contained in:
Stijn De Clercq 2021-09-26 13:10:57 +02:00
parent 7ab72aabfd
commit 1ee9900891
2 changed files with 41 additions and 35 deletions

View file

@ -1,29 +1,39 @@
import datetime
from datetime import datetime
from typing import Dict
import requests
def etenScript(weekDag, resto: str = "sterre"):
def get_type(menu: Dict, type_: str) -> str:
acc = ""
for m in menu["meals"]:
# API sometimes has empty results, also filter out wrong type
if not m["name"] or m["type"] != type_:
continue
if m["kind"] == "meat":
acc += ("* Vlees: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "fish":
acc += ("* Vis: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "vegetarian":
acc += ("* Vegetarisch: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "vegan":
acc += ("* Vegan: {} ({})\n".format(m["name"], m["price"]))
return acc
def etenScript(dag: datetime, resto: str = "sterre"):
# What day
weekdagen = ('ma', 'di', 'wo', 'do', 'vr', 'za', 'zo')
deltas = {'morgen': 1,
'overmorgen': 2,
'volgende': 7}
d = datetime.date.today()
if weekDag in deltas:
d += datetime.timedelta(deltas[weekDag])
if weekDag[0:2] in weekdagen:
while d.weekday() != weekdagen.index(weekDag[0:2]):
d += datetime.timedelta(1)
menuSoep = ""
menuHoofdgerechten = ""
menuKoud = ""
menuGroenten = ""
# Fetch from API
try:
menu = requests.get(f"https://zeus.ugent.be/hydra/api/2.0/resto/menu/nl-{resto}/{d.year}/{d.month}/{d.day}.json").json()
menu = requests.get(f"https://zeus.ugent.be/hydra/api/2.0/resto/menu/nl-{resto}/{dag.year}/{dag.month}/{dag.day}.json").json()
if not menu["meals"]:
raise Exception()
@ -34,20 +44,14 @@ def etenScript(weekDag, resto: str = "sterre"):
if s["kind"] == "soup":
menuSoep += ("* {} ({})\n".format(s["name"], s["price"]))
for m in menu["meals"]:
if m["kind"] == "meat":
menuHoofdgerechten += ("* Vlees: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "fish":
menuHoofdgerechten += ("* Vis: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "vegetarian":
menuHoofdgerechten += ("* Vegetarisch: {} ({})\n".format(m["name"], m["price"]))
elif m["kind"] == "vegan":
menuHoofdgerechten += ("* Vegan: {} ({})\n".format(m["name"], m["price"]))
menuHoofdgerechten = get_type(menu, "main")
menuKoud = get_type(menu, "cold")
for v in menu["vegetables"]:
menuGroenten += ("* {}\n".format(v))
except Exception:
except Exception as e:
menuSoep += "Restaurant gesloten"
menuGroenten += "Restaurant gesloten"
menuHoofdgerechten += "Restaurant gesloten"
return menuSoep, menuHoofdgerechten, menuGroenten
menuKoud += "Restaurant gesloten"
return menuSoep, menuHoofdgerechten, menuKoud, menuGroenten