-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyUniPd.py
204 lines (182 loc) · 7.06 KB
/
pyUniPd.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import telegram
import sqlite3
import pickledb
from geopy.distance import vincenty
import arrow
con = sqlite3.connect("db/logs.db")
class pyUniPd:
def __init__(self):
pass
@classmethod
def commandlist(self, db):
mydb = pickledb.load(db, False)
commands = []
for command in mydb.getall():
commands.append(command)
return commands
@classmethod
def dict_factory(self, curs, row):
d = {}
for idx, col in enumerate(curs.description):
d[col[0]] = row[idx]
return d
@classmethod
def writedb(self, mdict):
a, b, c, d, e, f, g, h = [0, 0, 0, 0, 0, 0, 0, 0]
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))
def sendNearPOI(self, bot, chat_id, pos):
io = (pos['latitude'], pos['longitude'])
distDict = {}
db = pickledb.load('db/mensaDB.db', False)
for key in db.getall():
a = db.get(key)
mensaCoord = (a['coord']['lat'], a['coord']['lon'])
distDict[key] = vincenty(io, mensaCoord).kilometers
MensaNearPOI = min(distDict, key=distDict.get)
km = str(round(float(distDict[MensaNearPOI]), 4))
prettyNearPOI = str(MensaNearPOI).title()
if prettyNearPOI == 'Sanfrancesco':
prettyNearPOI = 'San Francesco'
textMensa = 'Mensa più vicina: ' + str(prettyNearPOI) + \
', distanza: ' + str(km) + ' km' + \
'. \nPer maggiori informazioni: /' + str(MensaNearPOI)
io = (pos['latitude'], pos['longitude'])
distDict = {}
db = pickledb.load('db/aulastudioDB.db', False)
for key in db.getall():
a = db.get(key)
asCoord = (a['coord']['lat'], a['coord']['lon'])
distDict[key] = vincenty(io, asCoord).kilometers
AsNearPOI = min(distDict, key=distDict.get)
km = str(round(float(distDict[AsNearPOI]), 4))
prettyNearPOI = str(AsNearPOI).title()
if prettyNearPOI == 'Viavenezia':
prettyNearPOI = 'Via Venezia'
elif prettyNearPOI == 'Titolivio':
prettyNearPOI = 'Tito Livio'
elif prettyNearPOI == 'Vbranca':
prettyNearPOI = 'Vittore Branca'
elif prettyNearPOI == 'Reset':
prettyNearPOI = 'Circolo Reset'
textAS = '\n\nAula studio più vicina: ' + str(prettyNearPOI) + \
', distanza: ' + str(km) + ' km' + \
'. \nPer maggiori informazioni: /' + str(AsNearPOI)
distDict = {}
db = pickledb.load('db/biblioDB.db', False)
for key in db.getall():
a = db.get(key)
biblioCoord = (a['coord']['lat'], a['coord']['lon'])
distDict[key] = vincenty(io, biblioCoord).kilometers
biblioNearPOI = min(distDict, key=distDict.get)
km = str(round(float(distDict[biblioNearPOI]), 4))
prettyNearPOI = db.get(biblioNearPOI)['nome']
textBiblio = '\n\nBiblioteca più vicina: ' + str(prettyNearPOI) + \
', distanza: ' + str(km) + ' km' + \
'. \nPer maggiori informazioni: /' + str(biblioNearPOI)
text = textMensa + textAS + textBiblio
markup = [['/' + MensaNearPOI, '/' + AsNearPOI],
['/' + biblioNearPOI, '/home']]
reply_markup = telegram.ReplyKeyboardMarkup(markup)
bot.sendMessage(chat_id=chat_id, text=text, reply_markup=reply_markup)
def reply(self, bot, update, message, command, chat_id):
commandsDB = pickledb.load('db/commandsDB.db', False)
pyUniPd.writedb(update.message.to_dict())
bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
reply = commandsDB.get(command)['text']
markup = commandsDB.get(command)['keyboard']
reply_markup = telegram.ReplyKeyboardMarkup(markup)
bot.sendMessage(chat_id=chat_id, text=reply,
reply_markup=reply_markup)
if commandsDB.get(command)['coord'] is not None:
lat = commandsDB.get(command)['coord']['lat']
lon = commandsDB.get(command)['coord']['lon']
bot.sendLocation(chat_id=chat_id, latitude=lat, longitude=lon)
else:
pass
def adminStats(self, bot, update, message, command, chat_id):
pyUniPd.writedb(update.message.to_dict())
connection = sqlite3.connect("db/logs.db")
connection.row_factory = pyUniPd.dict_factory
curs = connection.cursor()
curs.execute("DELETE FROM log where username='UnipdBot'")
curs.execute("DELETE FROM log where username='mikexine'")
curs.execute("DELETE FROM log where username='mikexine_1234'")
connection.commit()
curs.execute("select * from log")
results = curs.fetchall()
connection.close()
userList = []
nMsg = 0
for i in range(len(results)):
nMsg += 1
user = results[i]['first_name'] + '-' + \
results[i]['last_name'] + '-' + results[i]['username']
if user not in userList:
userList.append(user)
uIDList = []
for i in range(len(results)):
chat = results[i]['u_id']
if chat not in uIDList:
uIDList.append(chat)
nMsgUser = {}
for us in uIDList:
nMsgUser[us] = 0
for i in range(len(results)):
nMsgUser[results[i]['u_id']] += 1
uDict = {}
for key in nMsgUser:
uDict[key] = {
'nMsg': nMsgUser[key],
'name': ''}
for i in range(len(results)):
uDict[results[i]['u_id']]['name'] = results[i]['first_name'] + \
'~' + results[i]['last_name'] + ' | @' + results[i]['username']
out = ''
out = out + 'Number of single users: ' + str(len(userList) - 1) + '\n'
out = out + 'Number of single messages sent: ' + str(nMsg) + '\n'
out = out + '\n -- TOP 20 USERS -- \n'
top = []
for w in sorted(uDict, key=uDict.get, reverse=True):
tx = uDict[w]['name'] + ' | nMsg: ' + str(uDict[w]['nMsg'])
top.append(tx)
for i in range(20):
out = out + str(i + 1) + ' ' + top[i] + '\n'
reply = out
bot.sendMessage(chat_id=chat_id, text=reply)