-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
305 lines (260 loc) · 10.9 KB
/
menu.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
This is the main file of the project.
TODO: Add description
@Author: Harlock Official https://github.com/HarlockOfficial
"""
import datetime
import enum
import os
from typing import List, Dict
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from lxml.html.clean import Cleaner
import data_base as db
load_dotenv()
class Canteen(enum.Enum):
"""
This enum contains all the canteens of the university
"""
MATTEOTTI = "Matteotti"
PETRARCA = "Petrarca"
STRABONE = "Strabone"
TENNA = "Tenna"
PARADISO = "Paradiso"
DAVACK = "Davack"
GMA = "Gma"
ACCORETTI = "Accoretti"
BERTELLI = "Bertelli"
DUCA = "Duca"
TRIDENTE = "Tridente"
def __str__(self):
return self.value
def build_daily_url(date: datetime.date, canteen: Canteen):
"""
Builds the url of the daily menu of the specified canteen for the specified day
@param date: The date of which you want to get the menu
@param canteen: The canteen of which you want to get the menu
@return: The url of the daily menu of the specified canteen for the specified day
"""
month = str(date.month) if date.month > 9 else "0" + str(date.month)
day = str(date.day) if date.day > 9 else "0" + str(date.day)
canteen = str(canteen)
return "https://www.erdis.it/menu/Mensa_" + canteen + \
"/Menu_Del_Giorno_" + str(date.year) + "_" + month + "_" + day + "_" + canteen + ".html"
def sanitise(dirty_html):
"""
Sanitises the html of the daily menu
"""
cleaner = Cleaner(page_structure=True,
meta=True,
embedded=True,
links=True,
style=True,
processing_instructions=True,
inline_style=True,
scripts=True,
javascript=True,
comments=True,
frames=True,
forms=True,
annoying_tags=True,
remove_unknown_tags=True,
safe_attrs_only=True,
safe_attrs=frozenset(['src','color', 'href', 'title', 'class', 'name', 'id', 'rowspan']),
remove_tags=()
)
return cleaner.clean_html(dirty_html)
def get_daily_menu(menu_table) -> Dict[str, Dict[str, List[str]]]:
"""
Gets the daily menu from the html of the daily menu
"""
full_turn_list = menu_table.select('tr>td')
full_turn_list = list(filter(lambda x: x.text in ['Pranzo', 'Cena'], full_turn_list))
out = {
'Pranzo': {
'Primo': [],
'Secondo': [],
'Contorno': [],
'Frutta': []
},
'Cena': {
'Primo': [],
'Secondo': [],
'Contorno': [],
'Frutta': []
}
}
lunch_turn = list(filter(lambda x: x.text in ['Pranzo'], full_turn_list))[0]
selector = 'tr:nth-child(-n+ ' + lunch_turn['rowspan'] + ')'
lunch_turn_rows = menu_table.select(selector)[1:]
for row in lunch_turn_rows:
lst = list(filter(lambda x: x.text in ['Primo'], row.select('td[rowspan]')))
if len(lst)>0:
first_turn = lst[0]
break
selector = 'tr:nth-child(-n+ ' + str(int(first_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Pranzo']['Primo'].append(row.select('td:not([rowspan])')[0].text.lower())
for row in lunch_turn_rows:
lst = list(filter(lambda x: x.text in ['Secondo'], row.select('td[rowspan]')))
if len(lst)>0:
second_turn = lst[0]
break
selector = 'tr:nth-child(n+ ' + str(int(first_turn['rowspan'])+1) + '):nth-child(-n+' + str(int(first_turn['rowspan']) + int(second_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Pranzo']['Secondo'].append(row.select('td:not([rowspan])')[0].text.lower())
for row in lunch_turn_rows:
lst = list(filter(lambda x: x.text in ['Contorno'], row.select('td[rowspan]')))
if len(lst)>0:
third_turn = lst[0]
break
selector = 'tr:nth-child(n+ ' + str(int(first_turn['rowspan']) + int(second_turn['rowspan']) + 1) + \
'):nth-child(-n+' + str(int(first_turn['rowspan']) + int(second_turn['rowspan']) + int(third_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Pranzo']['Contorno'].append(row.select('td:not([rowspan])')[0].text.lower())
selector = 'tr:nth-child(n+ ' + str(int(first_turn['rowspan']) + int(second_turn['rowspan']) + int(third_turn['rowspan']) + 1) + \
'):nth-child(-n+' + str(int(lunch_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Pranzo']['Frutta'].append(list(filter(lambda x: x.text != "Frutta", row.select('td:not([rowspan])')))[0].text.lower())
dinner_turn = list(filter(lambda x: x.text in ['Cena'], full_turn_list))[0]
selector = 'tr:nth-child(n+ ' + str(int(lunch_turn['rowspan']) + 2) + '):nth-child(-n+' + str(int(lunch_turn['rowspan']) + int(dinner_turn['rowspan']) + 1) + ')'
dinner_turn_rows = menu_table.select(selector)
for row in dinner_turn_rows:
lst = list(filter(lambda x: x.text in ['Primo'], row.select('td[rowspan]')))
if len(lst)>0:
first_turn = lst[0]
break
selector = 'tr:nth-child(n+ ' + str(int(lunch_turn['rowspan']) + 2) + \
'):nth-child(-n+ ' + str(int(lunch_turn['rowspan']) + int(first_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Cena']['Primo'].append(row.select('td:not([rowspan])')[0].text.lower())
for row in dinner_turn_rows:
lst = list(filter(lambda x: x.text in ['Secondo'], row.select('td[rowspan]')))
if len(lst)>0:
second_turn = lst[0]
break
selector = 'tr:nth-child(n+ ' + str(int(lunch_turn['rowspan']) + int(first_turn['rowspan']) + 2) + \
'):nth-child(-n+ ' + str( int(lunch_turn['rowspan']) + int(first_turn['rowspan']) + int(second_turn['rowspan']) + 1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Cena']['Secondo'].append(row.select('td:not([rowspan])')[0].text.lower())
for row in dinner_turn_rows:
lst = list(filter(lambda x: x.text in ['Contorno'], row.select('td[rowspan]')))
if len(lst)>0:
third_turn = lst[0]
break
selector = 'tr:nth-child(n+ ' + str(int(lunch_turn['rowspan'])+int(first_turn['rowspan']) + int(second_turn['rowspan']) + 2) + \
'):nth-child(-n+ ' + str(int(lunch_turn['rowspan']) + int(first_turn['rowspan']) + int(second_turn['rowspan'])+ int(third_turn['rowspan']) + 1) + ')'
turn_rows = menu_table.select(selector)[1:]
for row in turn_rows:
out['Cena']['Contorno'].append(row.select('td:not([rowspan])')[0].text.lower())
selector = 'tr:nth-child(n+ ' + str(int(lunch_turn['rowspan']) + int(first_turn['rowspan']) + int(second_turn['rowspan']) + int(third_turn['rowspan']) + 2) + \
'):nth-child(-n+' + str(int(lunch_turn['rowspan']) + int(dinner_turn['rowspan'])+1) + ')'
turn_rows = menu_table.select(selector)
for row in turn_rows:
out['Cena']['Frutta'].append(list(filter(lambda x: x.text != "Frutta", row.select('td:not([rowspan])')))[0].text.lower())
return out
def get_daily_time(time_table) -> Dict[str, str]:
"""
Gets the time of the daily menu
"""
out = {
'Pranzo': {
'IsOpen': False,
'OpenTime': '12:00',
'CloseTime': '14:00'
},
'Cena': {
'IsOpen': False,
'OpenTime': '19:00',
'CloseTime': '21:00'
}
}
time_table = time_table.select('tr')[1:]
for index, row in enumerate(time_table):
data = row.select('td')
out[data[0].text]['OpenTime'] = data[1].text
out[data[0].text]['CloseTime'] = data[2].text
if len(data)>3:
out[data[0].text]['IsOpen'] = data[3].text == 'NO'
try:
if int(data[3]['rowspan']) == 2 and index<len(time_table)-1:
tmp_data = time_table[index+1].select('td')
out[tmp_data[0].text]['IsOpen'] = data[3].text == 'NO'
except KeyError:
pass
return out
def parse_menu(html) -> Dict[str, Dict[str, List[str]]]:
"""
Parses the html of the daily menu and returns a dictionary with the menu
"""
html_content = sanitise(html)
soup = BeautifulSoup(html_content, 'html.parser')
tables = soup.select('table#menu')
if len(tables) < 2:
return None, None
time_table = tables[0]
menu_table = tables[1]
daily_time = get_daily_time(time_table)
daily_menu = get_daily_menu(menu_table)
return daily_menu, daily_time
def save_menu_to_db(menu: Dict[str, Dict[str, List[str]]], time: Dict[str, str], canteen: Canteen):
"""
Saves the menu and timetable to the database
"""
mongo_client = db.open_connection()
data_base = db.get_data_base(mongo_client=mongo_client)
collection = data_base[os.getenv('DB_MENU_COLLECTION')]
collection.find_one_and_delete({'canteen': canteen.value.lower()})
collection.insert_one({'canteen': canteen.value.lower(), 'menu': menu, 'time': time, 'date': datetime.date.today().isoformat()})
db.close_connection(mongo_client)
def init_menu():
"""
Module main function, does all the work
"""
for canteen in Canteen:
url = build_daily_url(datetime.date.today(), canteen)
headers = {'Accept-Encoding': 'identity'}
request = requests.get(url, headers=headers, timeout=60)
if request.status_code == 200:
from bot import logger
logger.info(f"Getting info on canteen: {canteen}")
menu, time = parse_menu(request.content.decode('utf-8'))
if menu is not None and time is not None:
save_menu_to_db(menu, time, canteen)
else:
menu = {
'Pranzo':{
'Primo':[],
'Secondo':[],
'Contorno':[],
'Frutta':[]
},
'Cena':{
'Primo':[],
'Secondo':[],
'Contorno':[],
'Frutta':[]
}
}
time = {
'Pranzo':{
'IsOpen': False,
'OpenTime': '-',
'CloseTime': '-'
},
'Cena':{
'IsOpen': False,
'OpenTime': '-',
'CloseTime': '-'
}
}
save_menu_to_db(menu, time, canteen)
else:
print("Error: " + str(request.status_code) + " for " + canteen)