forked from beheh/openmensa-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-mensa.py
executable file
·132 lines (110 loc) · 3.96 KB
/
parser-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
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import sys
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup as parse, NavigableString, Tag
from pyopenmensa.feed import LazyBuilder
import datetime
if len(sys.argv) < 3 or "--help" in sys.argv[1:] or "-h" in sys.argv[1:]:
print("Usage: {} <town> <mensa>".format(sys.argv[0]))
sys.exit()
mensas = {
"flensburg": {
"mensa": 7,
# "cafeteria": 14,
},
"heide": {"mensa": 11},
"kiel": {
"mensa-i": 1,
"mensa-ii": 2,
"kesselhaus": 4,
"schwentine": 5,
"gaarden": 3,
# "cafeteria-i": 413,
# "cafeteria-ii": 423,
"cafeteria-fh": 6,
"cafeteria-dockside": 16,
},
"luebeck": {
"mensa": 8,
# "cafeteria": 442,
"musikhochschule": 9,
},
"wedel": {"cafeteria": 10},
}
town = sys.argv[1]
mensa = sys.argv[2]
if town not in mensas:
print('Town "{}" not found.'.format(sys.argv[1]))
sys.exit()
if mensa not in mensas[town]:
print('Town "{}" does not have a mensa "{}".'.format(town, mensa))
sys.exit()
mensa_id = mensas[sys.argv[1]][mensa]
legend = {}
legendcontent = urlopen(
'https://studentenwerk.sh/de/essen-uebersicht?ort=1&mensa=1').read()
legenddocument = parse(legendcontent, 'html.parser')
legends = legenddocument.find_all("div", {"class": "filterbutton"})
for current_legend in legends:
legend[current_legend['data-wert']
] = current_legend.select_one('span:not(.abk)').string.strip()
canteen = LazyBuilder()
# parse current and next week
for next_week in range(0, 2):
url = "https://studentenwerk.sh/de/mensaplandruck?mensa={}&nw={}".format(
mensa_id, next_week)
content = urlopen(url).read()
document = parse(content, "html.parser")
days = document.find_all("div", {"class": "tag_headline"})
for day in days:
date = datetime.datetime.strptime(day.attrs['data-day'], '%Y-%m-%d')
meals = day.find_all("div", {"class": "mensa_menu_detail"})
for meal in meals:
notes = []
notes.extend(meal['data-arten'].split('|'))
notes.extend(meal['data-allergene'].split('|'))
notes.extend(meal['data-zusatzstoffe'].split('|'))
notes = list(filter(lambda x: len(x) > 0, notes))
notes = list(map(lambda x: legend[x], notes))
category = meal.find(
"div", {"class": "menu_art"}).contents[0].select_one("span[class^=mensatyp_]").string.strip()
title = ""
def extract(part, title):
if isinstance(part, NavigableString):
title += str(part)
elif isinstance(part, Tag):
if part.name == "small":
note_string = part.string.strip(" ()")
else:
if part.name == "br":
if title[-1] != " ":
title += " "
for child in part.children:
title = extract(child, title)
return title
title = extract(
meal.find("div", {"class": "menu_name"}), "")
title = re.sub("\\s+", " ", title).strip()
if len(title) < 1:
continue
price = meal.find("div", {"class": "menu_preis"}).string.strip()
prices = {}
if price:
subprice = price.split("/")
if len(subprice) == 3:
prices = {
"student": subprice[0],
"employee": subprice[1],
"other": subprice[2],
}
else:
prices = {"other": price}
canteen.addMeal(
datetime.date(date.year, date.month, date.day),
category,
title,
notes=notes,
prices=prices,
)
print(canteen.toXMLFeed())