didier/functions/eten.py

58 lines
1.7 KiB
Python
Raw Normal View History

2021-09-26 13:10:57 +02:00
from datetime import datetime
from typing import Dict
import requests
2021-09-26 13:10:57 +02:00
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
2021-09-26 13:10:57 +02:00
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"]))
2021-09-26 13:10:57 +02:00
return acc
2021-09-26 13:10:57 +02:00
2021-11-05 23:36:12 +01:00
def etenScript(dag: datetime):
2021-09-26 13:10:57 +02:00
# What day
menuSoep = ""
menuHoofdgerechten = ""
2021-09-26 13:10:57 +02:00
menuKoud = ""
menuGroenten = ""
# Fetch from API
try:
2021-11-05 23:36:12 +01:00
menu = requests.get(f"https://zeus.ugent.be/hydra/api/2.0/resto/menu/nl/{dag.year}/{dag.month}/{dag.day}.json").json()
2021-08-19 19:45:00 +02:00
if not menu["meals"]:
raise Exception()
# Print menu
for s in menu["meals"]:
if s["kind"] == "soup":
menuSoep += ("* {} ({})\n".format(s["name"], s["price"]))
2021-09-26 13:10:57 +02:00
menuHoofdgerechten = get_type(menu, "main")
menuKoud = get_type(menu, "cold")
for v in menu["vegetables"]:
menuGroenten += ("* {}\n".format(v))
except Exception:
menuSoep += "Restaurant gesloten"
menuGroenten += "Restaurant gesloten"
menuHoofdgerechten += "Restaurant gesloten"
2021-09-26 13:10:57 +02:00
menuKoud += "Restaurant gesloten"
return menuSoep, menuHoofdgerechten, menuKoud, menuGroenten