forked from GLUA-UA/meals-ua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meals-ua.py
executable file
·453 lines (359 loc) · 18.9 KB
/
meals-ua.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python3
# GLUA - Grupo de Linux da Universidade de Aveiro - 2019
# Visit https://glua.ua.pt or https://www.facebook.com/glua.ua/ for more information about us
# https://github.com/GLUA-UA/meals-ua
# Developed mainly by GLUA members:
# - Rodrigo Rosmaninho
# - Leonardo Costa
# - André Alves
# Helped by pull requests from:
# - Luís Silva
# Made possible by the University of Aveiro's public API
# VERSION 3
# Necessary imports
import urllib.request as urllib
import sys, os
import argparse
import json
from os.path import expanduser
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
# Get home directory. Works both on Linux and Windows
home = expanduser("~")
class bcolors: # Define colors
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m' # ENDC makes the color return to normal
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# See delete_last_prints()
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
# Instantiate the parser
parser = argparse.ArgumentParser(
description="Script simples em python que apresenta os menus do dia (ou semana) em todos os refeitórios da Universidade de Aveiro")
# Presents the week's menu
parser.add_argument('-w', action='store_true', default=False, dest='showWeek',
help='Apresenta os menus de toda a semana')
# Presents the week's menu
parser.add_argument('-t', action='store_true', default=False, dest='showTutorial',
help='Volta a apresentar o tutorial inicial')
# Specifies which zone to display results from
parser.add_argument('-l', type=int, nargs=1, dest='displayZone',
default=1,
help='Especifica os refeitórios a apresentar: 1 - Campus (Santiago, Crasto, Snack, e AFUAv), 2 - ESTGA, 3 - Restaurante Universitário, 4 - ESAN',
)
# Save JSON config file on the user's home directory
def save_config_file():
file = open(home + "/.ementasrc", "w")
json_data = json.loads("{}")
json_data["skip_tutorial"] = True
file.write(json.dumps(json_data))
# Show Tutorial
def show_tutorial():
print(bcolors.OKGREEN + '************************* Tutorial *************************' + bcolors.ENDC)
print("Bem vindo. Este tutorial apenas aparecerá uma vez.")
print("Este script serve para conseguir facilmente consultar as ementas dos vários refeitórios da Universidade de Aveiro.")
print("Por omissão, são apresentadas as ementas do dia atual nos refeitórios do campus da UA (Santiago, Crasto, e Snack)")
print("No entanto, pode utilizar o parâmetro " + bcolors.BOLD + "-w" + bcolors.ENDC + " para visualizar as ementas de toda a semana e o parâmetro " + bcolors.BOLD + "-l" + bcolors.ENDC + " para especificar o local a consultar, sendo que:")
print(" " + bcolors.BOLD + "-l 1" + bcolors.ENDC + " devolve as ementas no Campus\n " + bcolors.BOLD + "-l 2" + bcolors.ENDC + " devolve as ementas na ESTGA\n " + bcolors.BOLD + "-l 3" + bcolors.ENDC + " devolve as ementas no Restaurante Universitário\n " + bcolors.BOLD + "-l 4" + bcolors.ENDC + " devolve as ementas na ESAN")
print("É possível conjugar ambos os parâmetros. Por exemplo, " + bcolors.BOLD + "ementa -w -l 2" + bcolors.ENDC + " devolve todas as ementas da semana correnta na ESTGA")
print("\nSe ainda não o fez, deveria correr o script 'install.sh' incluído neste diretório.\nDesta forma, poderá executar o programa de forma fácil e sem ter de navegar até ao diretório para onde o descarregou.")
print("Para o fazer, execute os seguintes comandos:" + bcolors.WARNING + "\nchmod +x install.sh\n./install.sh" + bcolors.ENDC)
print("\nEste software é open-source e o código pode ser consultado em: " + bcolors.OKBLUE + "https://github.com/GLUA-UA/meals-ua" + bcolors.ENDC)
print("Se encontrar algum problema, por favor crie um issue nessa página, para que este possa ser rapidamente resolvido.")
print("\nPara consultar novamente este tutorial pode usar o parâmetro " + bcolors.BOLD + "-t" + bcolors.ENDC + "")
print("Para apresentar a página de ajuda pode usar o parâmetro " + bcolors.BOLD + "-h" + bcolors.ENDC + " ou " + bcolors.BOLD + "--help" + bcolors.ENDC + "")
print(bcolors.OKGREEN + '************************************************************\n' + bcolors.ENDC)
# Check if config file exists and if the tutorial has already been shown
def check_config():
if os.path.isfile(home + "/.ementasrc"):
file = open(home + "/.ementasrc")
data = json.loads(file.read())
if 'skip_tutorial' in data:
if not data['skip_tutorial']:
save_config_file()
show_tutorial()
else:
save_config_file()
show_tutorial()
else:
save_config_file()
show_tutorial()
# Determine whether there is internet access, return boolean
def internet_on():
try:
urllib.urlopen('http://www.google.pt', timeout=1)
return True
except urllib.URLError as err:
return False
# Deletes last x prints (x = number)
def delete_last_prints(number):
for _ in range(number):
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(ERASE_LINE)
# Handle an error that happens when there is no meal data for a given place (happens a lot with ESAN)
def handle_key_error(displayZone):
if displayZone == [2]:
place="no refeitório da ESTGA"
elif displayZone == [3]:
place = "no Restaurante Universitário"
elif displayZone == [4]:
place = "no refeitório da ESAN"
else:
place = "nos refeitórios do Campus"
print(bcolors.WARNING + "De momento não há informações sobre a ementa " + place + "!" + bcolors.ENDC + "\n")
sys.exit()
# Query UA API for meal information and print it on the CLI
def query_UA_API(place, date):
# Try to import xmltodict. If the module is not found, print an error and quit program
try:
import xmltodict
except:
print("\nErro! Módulo 'xmltodict' não encontrado! Siga as instruções no README.md para instalar e volte a correr o programa.")
print("Ou faça sh ./install.sh no diretório deste ficheiro.")
sys.exit()
try:
file = urllib.urlopen('http://services.web.ua.pt/sas/ementas?date=' + date + '&place=' + place) # open the result of the API as a file
response = file.read()
file.close()
response = xmltodict.parse(response) # Use xmltodict module to parse data from XML to a Python Dict
# Sample data: https://codebeautify.org/jsonviewer/cb6c4994
# try:
# response = response['result']['menus']['menu'] # Get to the relevent section of the data. 'menu' is an array of objects, each object matches a meal at a certain canteen
# except KeyError:
# handle_key_error(args.displayZone)
data = response
if not isinstance(response, list):
data = [1]
data[0] = response
last_date = ""
# Iterate through the array of meals
for i in range(len(data)):
if data[i]['@date'] != last_date and data[i]['@meal'] == 'Almoço':
last_date = data[i]['@date']
print(bcolors.WARNING + '****************** ' + bcolors.HEADER + bcolors.BOLD + '{: ^22}'.format(data[i]['@date'][:-15]) + bcolors.WARNING + ' ******************\n' + bcolors.ENDC) # Prints the day properly formatted. The space between the '=' must be 22 characters.
if data[i]['@meal'] == 'Almoço': # Each canteen can serve 2 meals (Lunch, Dinner). If the current meal is Lunch, print the name of the canteen
print(bcolors.OKBLUE + '================== ' + '{: ^22}'.format(data[i]['@canteen']) + ' ==================\n' + bcolors.ENDC) # Prints the name of the canteen properly formatted. The space between the '=' must be 22 characters.
print(bcolors.UNDERLINE + data[i]['@meal'] + '\n' + bcolors.ENDC) # Print what meal it is, i.e. Lunch or Dinner
if data[i]['@disabled'] == '0': # If the meal is not 'disabled', in other words, if the canteen is in fact serving this meal
# Iterate through all the diffent options on the menu
for x in range(len(data[i]['items']['item'])):
if '#text' in data[i]['items']['item'][x]: # If the option name has a corresponding value (food), print both the name and the value
print(bcolors.WARNING + data[i]['items']['item'][x]['@name'] + bcolors.ENDC + ': ' + bcolors.BOLD + data[i]['items']['item'][x]['#text'] + bcolors.ENDC)
else:
print(bcolors.FAIL + data[i]['@disabled'] + bcolors.ENDC) # If the meal is 'disabled', print the disable message included in the data
print()
except:
try:
query_CMS(place, date)
except:
# Error has occured, print error message
print(bcolors.FAIL + "Ocorreu um erro ao obter a informação das ementas na UA!\n Por favor tente novamente. Se o erro persistir contacte o GLUA." + bcolors.ENDC)
# Scrap CMS website for meal information and print it on the CLI
def query_CMS(place, date):
file = urllib.urlopen('https://cms.ua.pt/ementas/')
response = file.read()
file.close()
soup = BeautifulSoup(response, 'html.parser')
# Get the table with the meal data div class view-content
table = soup.find('div', {'class': 'view-content'})
if table is None:
handle_key_error(args.displayZone)
# Get all the canteens of the table
canteens = table.find_all('table', {'class': 'tabelahead views-table cols-3 tablesaw tablesaw-stack'})
menu = {}
# Loop through all the canteens
for canteen in canteens:
# Get the caption of the table (the name of the canteen)
caption = canteen.find('caption').text.split(': ')[1]
# Get the meals of the canteen
meals = canteen.find_all('tr')
# Filter the first meal (the header of the table)
meals = meals[1:]
menu[caption] = {}
# Loop through all the meals
for meal in meals:
# Get the meal header
header = meal.find('td', {'class': 'views-field views-field-title'}).text
header = header.strip().split(' ')
# Get the meal date and whether it is lunch or dinner
lunchOrDinner = header[0]
meal_date = header[-1]
if meal_date not in menu[caption]:
menu[caption][meal_date] = {}
# Get the meal options if it isn't specified whether it is lunch or dinner
if lunchOrDinner != 'Almoço' and lunchOrDinner != 'Jantar':
# Get the meal options
meal_name = meal.find('td', {'class': 'views-field views-field-body'})
items = meal_name.find_all('p')
if "Almoço" not in menu[caption][meal_date]:
menu[caption][meal_date]["Almoço"] = {}
if "Jantar" not in menu[caption][meal_date]:
menu[caption][meal_date]["Jantar"] = {}
if "Almoço" not in items[0].text:
continue
lunch = True
# Loop through all the meal options
for item in items:
if "Jantar" in item.text:
lunch = False
if "Almoço" in item.text or "Jantar" in item.text or item.text.rstrip() == "":
continue
plate = item.text.strip().replace(u'\xa0', u' ')
if plate == "":
continue
plate_name, plate_description = parse_plate(plate)
menu[caption][meal_date]["Almoço" if lunch else "Jantar"][plate_name] = plate_description
continue
# Get the meal options
meal_name = meal.find('td', {'class': 'views-field views-field-body'})
items = meal_name.find_all('p')
if lunchOrDinner not in menu[caption][meal_date]:
menu[caption][meal_date][lunchOrDinner] = {}
# Loop through all the meal options
for plate in items:
plate = plate.text.strip().replace(u'\xa0', u' ')
if plate == '':
continue
# Get the name and description of the meal option
plate_name, plate_description = parse_plate(plate)
# Add the meal option to the menu
menu[caption][meal_date][lunchOrDinner][plate_name] = plate_description
printCanteens = []
printDates = []
# Print the menu
if place == 'santiago':
printCanteens.append('Santiago')
printCanteens.append('Crasto')
printCanteens.append('Grelhados')
elif place == 'estga':
printCanteens.append('ESTGA')
elif place == 'rest':
printCanteens.append('Restaurante Universitário')
if date == 'day':
# Get the current date
now = datetime.now().strftime('%d/%m/%Y')
printDates.append(now)
elif date == 'week':
# Get all the dates of the current week
now = datetime.now()
start = now - timedelta(days=now.weekday())
end = start + timedelta(days=6)
for i in range((end - start).days + 1):
printDates.append((start + timedelta(days=i)).strftime('%d/%m/%Y'))
# Loop through all the canteens
for canteen in menu:
# Check if the canteen is one of the canteens to be printed
if canteen not in printCanteens:
continue
# Loop through all the dates
for meal_date in menu[canteen]:
# Check if the date is one of the dates to be printed
if meal_date not in printDates:
continue
print(bcolors.WARNING + '****************** ' + bcolors.HEADER + bcolors.BOLD + '{: ^22}'.format(meal_date) + bcolors.WARNING + ' ******************\n' + bcolors.ENDC)
print(bcolors.OKBLUE + '================== ' + '{: ^22}'.format(canteen) + ' ==================\n' + bcolors.ENDC)
# Loop through all the meals
for meal in menu[canteen][meal_date]:
print(bcolors.UNDERLINE + meal + '\n' + bcolors.ENDC) # Print what meal it is, i.e. Lunch or Dinner
# Loop through all the meal options
for plate in menu[canteen][meal_date][meal]:
print(bcolors.WARNING + plate + bcolors.ENDC + ': ' + bcolors.BOLD + menu[canteen][meal_date][meal][plate] + bcolors.ENDC)
print()
# Parse the meal option name and description
def parse_plate(plate):
if '-' in plate:
meal = plate.split(' - ')
name = meal[0]
description = meal[-1]
else:
name = plate.split(' ')[0]
description = plate
return name, description
# Scrap and print data from AFUAv's facebook page, where they regularly post the menu
def query_AFUAv(hour):
from lxml import etree, html
import requests
print(bcolors.OKBLUE + '================== ' + '{: ^22}'.format("AFUAv") + ' ==================\n' + bcolors.ENDC) # Prints the name of the canteen properly formatted. The space between the '=' must be 22 characters.
print(bcolors.WARNING + "A obter os dados da página do facebook. Por favor aguarde..." + bcolors.ENDC)
try:
f = requests.get('https://www.facebook.com/AFUAv-1411897009022037/', headers={'Connection':'close'})
# Parse etree from html string
doc = html.fromstring(f.content)
# Filter by post
posts = doc.xpath('//div[contains(@class, "userContentWrapper")]')
ignore = ['Boa', 'Bom', 'Hoje', 'Ficamos', 'Ver', '...', '🍽️']
for p in posts:
# Convert utc timestamp to datetime object
t = datetime.utcfromtimestamp(int(p.xpath('.//@data-utime')[0]))
view = None
if hour >= t.hour and ((t.hour in [11, 12] and hour < 14) or (t.hour in [18, 19] and hour < 22)):
show = True
else:
show = True
if show:
view = ''
# Get all text in the post
meal = p.xpath(
'.//div[@class="text_exposed_root"]//*[self::p or self::span]/text()')
for m in meal:
# Show only dishes
if not any(sub in m for sub in ignore) and m != ' ':
view += m.strip() + '\n'
# Delete standby print and print final result
delete_last_prints(1)
time_of_day = "Almoço" if t.hour in range(8, 15) else "Jantar"
print(bcolors.UNDERLINE + time_of_day + bcolors.ENDC)
print(f"(Post a {t.day}/{t.month} às {t.hour}h{t.minute})\n")
print(bcolors.WARNING + "Pratos disponíveis" + bcolors.ENDC + ":")
print(bcolors.BOLD + view + bcolors.ENDC, end="")
break
else:
continue
except:
# Error has occured, likely related to the request
# Delete standby print and print error message
delete_last_prints(1)
print(bcolors.FAIL + "Ocorreu um erro ao obter a informação da página do facebook da AFUAv!\n Por favor tente novamente. Se o erro persistir contacte o GLUA." + bcolors.ENDC)
def main():
print(bcolors.OKGREEN + '\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n' + bcolors.ENDC + bcolors.BOLD +
' Ementas na UA\n' + bcolors.ENDC + bcolors.OKGREEN + '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' + bcolors.ENDC)
print('By GLUA - Grupo de Linux da Universidade de Aveiro\n')
# Get arguments
global args
args = parser.parse_args()
if args.showTutorial:
show_tutorial()
sys.exit()
check_config()
if not internet_on(): # If there is no internet print errors
print(bcolors.FAIL +
'=========================== ERRO ===========================' + bcolors.ENDC)
print(bcolors.FAIL + 'Não existe conexão à internet.\nEste script necessita de uma ligação estável à internet para aceder ao API da UA.\n' + bcolors.ENDC)
else:
if args.showWeek:
date = "week"
else:
date = "day"
if args.displayZone == [2]:
place="ESTGA"
elif args.displayZone == [3]:
place = "rest"
elif args.displayZone == [4]:
place = "ESAN"
else:
# UA's Main Campus
place = "santiago"
# Query and print UA API data for selected location and time period
query_UA_API(place, date)
# Query and print AFUAv data (only for UA's main campus and for the same day)
if(place == "santiago" and date == "day"):
now = datetime.now().hour
query_AFUAv(now)
print(bcolors.UNDERLINE + '\nBom Apetite!\n' + bcolors.ENDC)
if __name__ == "__main__":
main()