/
pyUniVeronaBot.py
193 lines (154 loc) · 5.2 KB
/
pyUniVeronaBot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import arrow
import requests
import sqlite3
import pickledb
from geopy.distance import vincenty
import datetime
from time import sleep
HEADERS = {
'content-type': 'application/json'
}
URL = 'http://188.166.145.27/api/univr/'
HOME = """
Seleziona un comando dalla tastiera!
"""
BOTINFO = """
I menù sono ottenuti dal [sito dell'ESU](https://esu.markas.info).
Domande? Contatta @mikexine qua su Telegram.
Vuoi dare una mano? `github.com/mikexine/univeronabot`
"""
SELEZIONA = "Seleziona una voce dalla tastiera!"
def home():
markup = [['/mensa', '/aulastudio'],
['/biblioteca', '/botinfo']]
return HOME, markup
def mensa():
markup = [["/sanfrancesco"],
["/legrazie"],
["/home"]]
return SELEZIONA, markup
def aulastudio():
markup = [["/campofiore", "/frinzi"],
["/maddalene", "/meneghetti"],
["/sanvitale", "/home"]]
return SELEZIONA, markup
def biblioteca():
markup = [["/bibliofrinzi", "/bibliomeneghetti"],
["/biblioriva", "/home"]]
return SELEZIONA, markup
def botInfo():
return BOTINFO, [['/home']]
def commandList():
# data = requests.get(URL + 'mensa/', headers=HEADERS).json()
mList = []
for key in requests.get(URL + 'mensa/', headers=HEADERS).json():
mList.append(key)
mList.remove('last_update')
for key in requests.get(URL + 'aulastudio/', headers=HEADERS).json():
mList.append(key)
for key in requests.get(URL + 'biblioteca/', headers=HEADERS).json():
mList.append(key)
print mList
return mList
def replier(command):
db = pickledb.load('db/univeronabot.pickledb', False)
reply = db.get(command)['text']
keyboard = db.get(command)['keyboard']
try:
coord = db.get(command)['coord']
lat = coord['lat']
lon = coord['lon']
except:
lat = None
lon = None
return reply, keyboard, lat, lon
def position(usrCoord):
markup = []
distDict = {'mensa': {}, 'aulastudio': {}, 'biblioteca': {}}
tmp = distDict
today = str(datetime.datetime.today().weekday())
mensa = requests.get(URL + 'mensa/', headers=HEADERS).json()
del mensa['last_update']
for key in mensa:
pranzo = mensa[key]['calendario']['pranzo']
cena = mensa[key]['calendario']['cena']
if cena == 1 or pranzo == 1:
distDict['mensa'][key] = mensa[key]['coord']
biblioteca = requests.get(URL + 'biblioteca/', headers=HEADERS).json()
for key in biblioteca:
if biblioteca[key]['orari'][today] != '':
distDict['biblioteca'][key] = biblioteca[key]['coord']
aulastudio = requests.get(URL + 'aulastudio/', headers=HEADERS).json()
for key in aulastudio:
if aulastudio[key]['orari'][today] != '':
distDict['aulastudio'][key] = aulastudio[key]['coord']
for key in distDict:
for i in distDict[key]:
lat = distDict[key][i]['lat']
lon = distDict[key][i]['lon']
tmp[key][i] = vincenty((usrCoord['latitude'],
usrCoord['longitude']),
(lat, lon)).kilometers
nearMensa = min(tmp['mensa'], key=tmp['mensa'].get)
nearMensaDist = str(round(float(tmp['mensa'][nearMensa]), 4))
line1 = "- *Mensa* più vicina, aperta oggi: %s, distante %s km.\n\n" %\
(mensa[nearMensa]['nome'].encode("utf-8"),
nearMensaDist.encode("utf-8"))
markup.append(['/'+nearMensa.encode("utf-8")])
nearAula = min(tmp['aulastudio'], key=tmp['aulastudio'].get)
nearAulaDist = str(round(float(tmp['aulastudio'][nearAula]), 4))
line2 = "- *Aula studio* più vicina, aperta oggi: %s, distante %s km.\n\n" %\
(aulastudio[nearAula]['nome'].encode("utf-8"),
nearAulaDist.encode("utf-8"))
markup.append(['/'+nearAula.encode("utf-8")])
try:
nearBiblio = min(tmp['biblioteca'], key=tmp['biblioteca'].get)
nearBiblioDist = str(round(float(tmp['biblioteca'][nearBiblio]), 4))
line3 = "- *Biblioteca* più vicina, aperta oggi: %s, distante %s km.\n\n" %\
(biblioteca[nearBiblio]['nome'].encode("utf-8"),
nearBiblioDist.encode("utf-8"))
markup.append(['/'+nearBiblio.encode("utf-8")])
except:
line3 = "- *Biblioteca:* oggi sono tutte chiuse."
reply = line1 + line2 + line3
return reply, markup
def writedb(mdict):
a, b, c, d, e, f, g, h = [0, 0, 0, 0, 0, 0, 0, 0]
con = sqlite3.connect("db/logs.db")
try:
a = mdict["message_id"]
except:
pass
try:
b = mdict["from"]["id"]
except:
pass
try:
c = mdict["from"]["username"]
except:
pass
try:
d = mdict["from"]["first_name"]
except:
pass
try:
e = mdict["from"]["last_name"]
except:
pass
try:
f = mdict["text"]
except:
pass
try:
g = mdict["chat"]["id"]
except:
pass
try:
h = arrow.utcnow().format('YYYY-MM-DD HH:mm:ss:SSS ZZ')
except:
pass
with con:
cur = con.cursor()
cur.execute("INSERT INTO log VALUES (?,?,?,?,?,?,?,?)", (a, b, c, d, e, f, g, h))