This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
421 lines (348 loc) · 12.4 KB
/
bot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Authors: MaanuelMM
# Credits: eternnoir, atlink, CoreDumped-ETSISI, Eldinnie
# Created: 2019/02/14
# Last update: 2019/11/27
import os
import re
import logger
import telebot
import more_itertools
from bot_requests import get_token, get_arrive_stop, get_bicimad, get_parkings, get_generic_request
from data_loader import DataLoader
from flask import Flask, request
from flask_sslify import SSLify
from unidecode import unidecode
from bs4 import BeautifulSoup
from logger import get_logger
logger = get_logger("bot", True)
logger.info("Starting bot...")
try:
logger.info("Getting data and config vars...")
data = DataLoader()
logger.info("Creating TeleBot...")
bot = telebot.TeleBot(token=data.TOKEN)
logger.info("Creating Flask server...")
server = Flask(__name__)
if 'DYNO' in os.environ: # only trigger SSLify if the app is running on Heroku
sslify = SSLify(server)
except Exception as e:
logger.info("Error creating Bot. Shutting down...")
logger.error(e, exc_info=True)
exit()
def log_message(message):
logger.info("Received: \"" + message.text + "\" from " + message.from_user.first_name +
" (ID: " + str(message.from_user.id) + ") " + "[Chat ID: " + str(message.chat.id) + "].")
def log_api_error(response):
logger.info("Error in API request:\n" + response)
def get_text_with_no_command(text):
try:
return text.split(" ", 1)[1]
except:
return ""
def is_integer(string):
try:
return int(string)
except:
return False
def make_arrival_line(arrival):
return "\n\nLínea " + arrival["line"] + " (" + arrival["destination"] + "):\n " + arrival["estimateArrive"]
def process_time_left(time_left):
if(str(time_left).isdecimal()):
time_left = int(time_left)
if(time_left == 0):
return "En parada"
elif(time_left > 2700):
return ">45:00min"
else:
return " " + str(int(time_left / 60)).zfill(2) + ":" + str(int(time_left % 60)).zfill(2) + "min"
else:
return " --:--min"
# https://stackoverflow.com/questions/4391697/find-the-index-of-a-dict-within-a-list-by-matching-the-dicts-value
def sort_arrivals(arrivals):
arrivals_sorted = []
for arrival in arrivals:
index_list = list(more_itertools.locate(
arrivals_sorted, pred=lambda d: d["line"] == arrival["line"]))
arrival["estimateArrive"] = process_time_left(
arrival["estimateArrive"])
if(index_list): # False if empty
arrivals_sorted[index_list[0]
]["estimateArrive"] += " " + arrival["estimateArrive"]
else:
arrivals_sorted.append(arrival)
del index_list
return arrivals_sorted
def process_arrival_response(arrivals):
result = ""
arrivals = sort_arrivals(
sorted(arrivals, key=lambda d: (d["estimateArrive"])))
for arrival in arrivals:
result += make_arrival_line(arrival)
return result + "\n\n"
def map_link_maker(coord_x, coord_y):
return "https://google.com/maps/search/?api=1&query=" + str(coord_x) + "," + str(coord_y)
def bicimad_light(light):
switcher_light = {
0: "\U0001F7E9BAJA\U0001F7E9",
1: "\U0001F7E7MEDIA\U0001F7E7",
2: "\U0001F7E5ALTA\U0001F7E5"
}
return switcher_light.get(light, "Indefinido")
def process_bicimad_response(bicimad):
line = "\n"
line += "Estación BiciMAD: " + \
bicimad["name"] + " (#" + str(bicimad["id"]) + ")"
if bool(bicimad["activate"]):
line += " \U0001F7E2OPERATIVA\U0001F7E2"
line += "\n"
line += "\n Huecos disponibles: " + str(bicimad["free_bases"])
line += "\n Bicicletas disponibles: " + str(bicimad["dock_bikes"])
line += "\n Nivel de ocupación: " + bicimad_light(bicimad["light"])
else:
line += " \U0001F534INOPERATIVA\U0001F534"
line += "\n"
line += "\n\U0001F4CDUbicación: " + \
map_link_maker(bicimad["geometry"]["coordinates"]
[1], bicimad["geometry"]["coordinates"][0])
line += "\n"
return line
def make_parking_line(parking):
line = "\n"
line += parking["name"] + " (#" + str(parking["id"]) + ")"
line += "\n\tPlazas libres: "
if parking["freeParking"] is None:
line += "None"
else:
line += str(parking["freeParking"])
line += "\n"
return line
def process_parkings_response(parkings):
result = "\n"
for parking in parkings:
result += make_parking_line(parking)
return result
def generate_metro_data_response(metro_data):
response = {}
soup = BeautifulSoup(metro_data, "html.parser")
for img in soup.find_all("img"):
line = img.get('class')[0].replace("-", " ").upper()
estimations = re.sub(r'\s\s+', r'\n ',
img.parent.next_sibling.next_sibling.text).strip()
if line not in response:
response[line] = []
response[line].append(estimations)
return response
def process_metro_response(metro_data):
result = ""
for k, v in generate_metro_data_response(metro_data).items():
result += "\n\n"
result += k.replace("LINEA", "LÍNEA")
for item in v:
result += "\n "
result += item
return result
def message_sender(message, reply):
if(len(reply) > 3000):
splitted_reply = reply.split("\n\n")
new_reply = ""
for fragment in splitted_reply:
if not new_reply:
new_reply = fragment
elif(len(new_reply + "\n\n" + fragment) > 3000):
bot.reply_to(message, new_reply)
new_reply = fragment
else:
new_reply += "\n\n" + fragment
del new_reply
else:
bot.reply_to(message, reply)
def get_token_clean():
try:
token_response = get_token(
data.EMTMADRID_GETTOKENSESSIONURL, data.EMTMADRID_EMAIL, data.EMTMADRID_PASSWORD)
token = token_response["data"][0]["accessToken"]
del token_response
except:
try:
log_api_error(token_response)
except:
log_api_error("Unable to get API response.")
raise
return token
def get_arrive_stop_clean(stop_id):
try:
arrive_stop_response = get_arrive_stop(
data.EMTMADRID_GETARRIVESTOPURL, get_token_clean(), stop_id, data.EMTMADRID_GETARRIVESTOPJSON)
try:
arrive_stop = arrive_stop_response["data"][0]["Arrive"]
except:
arrive_stop = ""
del arrive_stop_response
except:
try:
log_api_error(arrive_stop_response)
except:
log_api_error("Unable to get API response.")
raise
return arrive_stop
def get_bicimad_clean(station_id):
try:
bicimad_response = get_bicimad(
data.EMTMADRID_GETBICIMADSTATIONSURL, get_token_clean(), station_id)
try:
bicimad = bicimad_response["data"]
except:
bicimad = ""
del bicimad_response
except:
try:
log_api_error(bicimad_response)
except:
log_api_error("Unable to get API response.")
raise
return bicimad
def get_parkings_clean():
try:
parkings_response = get_parkings(
data.EMTMADRID_GETPARKINGSSTATUSURL, get_token_clean())
try:
parkings = parkings_response["data"]
except:
parkings = ""
del parkings_response
except:
try:
log_api_error(parkings_response)
except:
log_api_error("Unable to get API response.")
raise
return parkings
def get_metro_arrival_clean(station_id):
try:
metro_response = get_generic_request(
data.METROMADRID_GETARRIVESTATIONURL + station_id).json()
try:
metro = metro_response[0]["data"]
except:
metro = ""
del metro_response
except:
log_api_error("Unable to get API response due to unauthorized access.")
raise
return metro
def get_metro_stations_clean():
try:
return get_generic_request(data.METROMADRID_GETSTATIONSLISTURL).json()
except:
log_api_error("Unable to get Metro Madrid stations from GitHub.")
raise
@bot.message_handler(commands=['start'])
def send_start(message):
log_message(message)
bot.reply_to(message, data.START)
@bot.message_handler(commands=['hola'])
def send_hola(message):
log_message(message)
bot.reply_to(message, data.HOLA + message.from_user.first_name)
@bot.message_handler(commands=['parada'])
def send_parada(message):
log_message(message)
text = get_text_with_no_command(message.text)
if(str(message.chat.id) in data.PARADA_CHAT_LIST and text.upper() in data.PARADA_CHAT_LIST[str(message.chat.id)]):
text = data.PARADA_CHAT_LIST[str(message.chat.id)][text.upper()]
if is_integer(text):
try:
arrive_stop = get_arrive_stop_clean(text)
if arrive_stop:
message_sender(message, data.PARADA_SUCCESSFUL.replace(
"<stopId>", text) + process_arrival_response(arrive_stop) + data.PARADA_SUCCESSFUL_DISCLAIMER)
else:
bot.reply_to(message, data.PARADA_NO_ESTIMATION)
del arrive_stop
except Exception as e:
logger.error(e, exc_info=True)
bot.reply_to(message, data.REQUEST_FAIL)
else:
bot.reply_to(message, data.PARADA_BAD_SPECIFIED)
del text
@bot.message_handler(commands=['bicimad'])
def send_bicimad(message):
log_message(message)
text = get_text_with_no_command(message.text)
if is_integer(text):
try:
bicimad = get_bicimad_clean(text)
if bicimad:
message_sender(message, process_bicimad_response(bicimad[0]))
else:
bot.reply_to(message, data.BICIMAD_NO_INFO)
del bicimad
except Exception as e:
logger.error(e, exc_info=True)
bot.reply_to(message, data.REQUEST_FAIL)
else:
bot.reply_to(message, data.BICIMAD_BAD_SPECIFIED)
del text
@bot.message_handler(commands=['parkings'])
def send_parkings(message):
log_message(message)
try:
message_sender(message, data.PARKINGS +
process_parkings_response(get_parkings_clean()))
except Exception as e:
logger.error(e, exc_info=True)
bot.reply_to(message, data.REQUEST_FAIL)
@bot.message_handler(commands=['metro'])
def send_metro(message):
log_message(message)
station = get_text_with_no_command(message.text)
if station:
station = re.sub(r'[^a-zA-Z0-9 ]', r'',
unidecode(station).replace("-", " ")).upper()
try:
stations = get_metro_stations_clean()
if station in stations:
response = get_metro_arrival_clean(stations[station])
if response:
message_sender(message, data.METRO_SUCCESSFUL +
process_metro_response(response))
else:
bot.reply_to(message, data.METRO_NO_ESTIMATION)
del response
else:
bot.reply_to(message, data.METRO_DOES_NOT_EXIST)
del stations
except Exception as e:
logger.error(e, exc_info=True)
bot.reply_to(message, data.REQUEST_FAIL)
else:
bot.reply_to(message, data.METRO_BAD_SPECIFIED)
del station
@bot.message_handler(commands=['help'])
def send_help(message):
log_message(message)
bot.reply_to(message, data.HELP)
@server.route("/" + data.TOKEN, methods=['POST'])
def get_message():
bot.process_new_updates(
[telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
logger.info("Removing current webhook...")
bot.remove_webhook()
logger.info("Successfully removed current webhook.")
logger.info("Creating new webhook...")
bot.set_webhook(url=data.URL+data.TOKEN)
logger.info("Successfully created new webhook.")
return "!", 200
if __name__ == "__main__":
try:
logger.info("Starting running server...")
server.run(host="0.0.0.0", port=int(data.PORT))
except Exception as e:
logger.info("Error starting server. Shutting down...")
logger.error(e, exc_info=True)
exit()