-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_mensa.py
112 lines (98 loc) · 3.24 KB
/
update_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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib2
import requests
import json
import arrow
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('settings.ini')
auth = config.get('main', 'auth')
headers = {
'Content-Type': 'application/json',
'mikexine': auth
}
mensaList = ['sanfrancesco', 'piovego', 'agripolis', 'acli',
'belzoni', 'forcellini', 'murialdo']
rep = '<span style="visibility:hidden">:</span>'
nomenu = 'Menu non pubblicato su www.esupd.gov.it/'
errmenu = ['Niente menu, errore su www.esupd.gov.it/']
def getcal():
soup = BeautifulSoup(urllib2.urlopen("http://www.esupd.gov.it/it").read())
listFull = []
listPart = []
listCal = []
p = 0
for i in soup.find_all("td"):
listFull.append(str(i))
for x in range(7):
listPart.append(listFull[p])
listPart.append(listFull[p + 1])
p += 4
for x in range(len(listPart)):
if 'open' in listPart[x]:
listCal.append('Aperto')
else:
listCal.append('Chiuso')
return listCal
def getmenu(mid):
mensaid = '0' + str(mid)
completo = {"primo": [], "secondo": [], "contorno": [], "dessert": []}
try:
url = "http://www.esupd.gov.it/it/Pagine/Menu.aspx?idmenu=ME_"
soup = BeautifulSoup(urllib2.urlopen(url + mensaid).read())
menu = []
for i in soup.find_all("h2"):
portata = i.text.split()[0].lower()
for j in i.next_siblings:
if j.name == "h2":
break
if j.name == "ul":
a = str(j)
menu += (a.split("<li>"))
for piatto in range(len(menu)):
if "h3" in menu[piatto]:
menu[piatto] = menu[piatto].replace(rep, ' ')
txt = menu[piatto][4:].split("<")[0]
completo[portata].append(txt)
menu = []
for key in completo:
if completo[key] == []:
completo[key] = [nomenu]
except:
for key in completo:
completo[key] = errmenu
return completo
r = requests.get("http://unipd.xyz/mensa", timeout=30)
data = r.json()
mensaDict = data[1]['mensa']
mensaID = data[1]['id']
try:
cal = getcal()
except:
cal = None
a = 0
if cal is not None:
for x in range(7):
mensaDict[mensaList[x]]['calendario']['pranzo'] = cal[a]
mensaDict[mensaList[x]]['calendario']['cena'] = cal[a + 1]
a += 2
else:
for x in range(7):
mensaDict[mensaList[x]]['calendario'][
'pranzo'] = "errore su www.esupd.gov.it/"
mensaDict[mensaList[x]]['calendario'][
'cena'] = "errore su www.esupd.gov.it/"
a += 2
for x in range(7):
mensaDict[mensaList[x]]['menu'] = getmenu(x + 1)
print x
data[1]['mensa'] = mensaDict
r = requests.put("http://unipd.xyz/mensa/" + mensaID,
data=json.dumps(data[1]), headers=headers, timeout=30)
updateID = data[0]['id']
now = arrow.now('CET').format('HH:mm - DD-MM-YYYY')
data[0]['mensa']['last_update'] = now
r = requests.put("http://unipd.xyz/mensa/" + updateID,
data=json.dumps(data[0]), headers=headers, timeout=30)