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
|
#!/usr/bin/env python3
""" usage: arafetch [options]
Options:
--db FILE path to db [Default: db.json]
--cantine NAME name of the cantine (matches mein.aramark.de/NAME/) [Default: thales-deutschland]
"""
import sys
from docopt import docopt
import requests
from bs4 import BeautifulSoup
import logging
import json
from datetime import datetime
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()
from itertools import tee
cantinedb = {
'thales-deutschland': 'aramark_menu2-2',
'buerocampus-wangen': 'aramark_menu2-3',
'ibm-boeblingen': 'aramark_menu2-3',
'metabo-nuertingen': 'aramark_menu2-3',
'vodafone-campus': 'aramark_menu2-2',
'continental-villingen': 'aramark_menu2-3'
}
def get_data(cantine):
headers = {
'User-Agent': 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Referer': f'http://mein.aramark.de/{cantine}/menuplan/',
'X-Requested-With': 'XMLHttpRequest',
'Cache-Control': 'max-age=0'
}
data = {
'action': 'aramark_menu2_content',
'id': cantinedb[cantine]
}
ret = requests.post(f'https://mein.aramark.de/{cantine}/wp-admin/admin-ajax.php',data=data)
if ret.status_code != 200:
log.error(ret)
raise ValueError('Return code is not 200')
return ret.content.decode()
def get_data_test():
return open("./test.html").read()
def get_things(row):
for item in row.select('.aramark_menu2-counter'):
try:
thing = {
"allergies": {},
"additives": {},
"type" : item.select_one('.aramark_menu2-countertitletop').find(text=True),
"description" : item.select_one('.aramark_menu2-menutext > p').find(text=True),
}
try:
price = float(item.select_one('.aramark_menu2-menuprice > .aramark_menu2-menuprice').find(text=True).replace(',','.').replace('EUR','').strip())
except:
price = float(thing['description'])
thing['price'] = price
try:
thing['title'] = item.select_one('.aramark_menu2-countermenu h4').find(text=True)
except:
thing['title'] = thing['type']
entries = item.select('.aramark_menu2-menuinfoallergenes li span')
for i in range(0,len(entries),2):
thing['allergies'] = { entries[i].find(text=True): entries[i+1].find(text=True) }
additives = { }
entries = item.select('.aramark_menu2-menuinfoadditives li span')
for i in range(0,len(entries),2):
thing['additives'] = { entries[i].find(text=True): entries[i+1].find(text=True) }
yield thing
except Exception as e:
log.error(f"problem with {e}")
log.debug(item.prettify())
def main():
args = docopt(__doc__)
dbname = args['--db']
cantine = args['--cantine']
try:
with open(dbname,'r+') as f:
db = json.load(f)
except:
log.info('creating new db')
db = {}
updated = datetime.now().isoformat().split('T')[0]
soup = BeautifulSoup(get_data(cantine), 'html.parser')
# soup = BeautifulSoup(get_data_test(), 'html.parser')
for row in soup.select('.aramark_menu2-menus > div'):
day = row.get('data-aramark_menu2-day')
things = list(get_things(row))
db[day] = things
log.info(f'writing db {dbname}')
with sys.stdout if dbname == '-' else open(dbname,'w+') as f:
json.dump(db,f,indent=2,sort_keys=True)
if __name__ == '__main__':
main()
|