This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmensa.py
120 lines (83 loc) · 3.34 KB
/
mensa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
from typing import List
import os
os.environ['WDM_LOG_LEVEL'] = '0'
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.select import Select
from webdriver_manager.chrome import ChromeDriverManager
url = "https://sws2.maxmanager.xyz/"
ingredients = json.load(open("ingredients.json"))
def init_browser() -> object:
""" Initialize new chrome browser
Returns:
(object): webdriver
"""
chrome_service = webdriver.chrome.service.Service(executable_path=ChromeDriverManager().install())
browser = webdriver.Chrome(service=chrome_service)
return browser
def get_locations(browser: object) -> dict:
""" Return all available canteen locations with id and name
Args:
browser (object): selenium webdriver browser
Returns:
(list): locations, format: {id: name, id: name}
"""
browser.get(url)
# find select
select_location = Select(browser.find_element(value="listbox-locations"))
locations = {}
for opt in select_location.options:
locations[int(opt.get_attribute("value"))] = opt.text
return locations
def get_today_menu(browser: object, loc_id: int, lang: str = "en") -> List[dict]:
""" Return today's menu of specified canteen
Code based on https://github.com/cvzi/mensahd/blob/master/stuttgart/stuttgart_old_2020/__init__.py
Args:
browser (object): selenium webdriver browser
loc_id (int): location id of canteen
lang (str): Language [de, en]
Returns:
(List[dict]): list of dishes
"""
if lang not in ["de", "en"]:
lang = "en"
browser.get(url + f"?lang={lang}")
# change location to specified location id
select_location = Select(browser.find_element(value="listbox-locations"))
# check if location id is valid
if str(loc_id) not in [opt.get_attribute("value") for opt in select_location.options]:
return [{"error": "Unknown location id"}]
select_location.select_by_value(str(loc_id))
# load html content to BeautifulSoup
content = browser.page_source
document = BeautifulSoup(content, "html.parser")
divs = document.find(
"div", {"class": "container-fluid"}).find_all("div", {"class", "row"})
menu = []
next_is_menu = False
category_name = ""
for div in divs:
is_category = div.find("div", {"class": "gruppenname"})
if is_category:
category_name = is_category.text.strip()
category_name = category_name.replace("*", "").strip()
category_name = category_name[0] + category_name[1:].lower()
next_is_menu = False if category_name in ("Hinweis", "Information") else True
continue
elif next_is_menu:
try:
meal_name = div.find("div", {"class": "visible-xs-block"}).text.strip()
except AttributeError:
continue
if meal_name.lower() == "geschlossen":
next_is_menu = False
continue
notes = div["lang"].split(",")
# load allergens/ additives
if len(notes):
notes = [ingredients[lang][i] for i in notes if i in ingredients[lang]]
else:
notes = None
menu.append({"category": category_name, "meal": meal_name, "ingredients": notes})
return menu