2019-10-11 22:40:28 +02:00
|
|
|
import logging
|
|
|
|
import requests
|
2019-10-16 19:59:13 +02:00
|
|
|
import re
|
2019-10-17 20:19:37 +02:00
|
|
|
from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler
|
2019-10-11 22:40:28 +02:00
|
|
|
|
2019-10-17 19:03:09 +02:00
|
|
|
from exceptions import *
|
|
|
|
|
2019-10-11 22:40:28 +02:00
|
|
|
token_file = open("token", "r")
|
|
|
|
updater = Updater(token=token_file.read(), use_context=True)
|
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
dispatcher = updater.dispatcher
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
|
|
|
|
|
|
|
|
2019-10-15 19:42:35 +02:00
|
|
|
class Query:
|
|
|
|
station_id = -1
|
|
|
|
departure_count = 4
|
2019-10-16 19:59:13 +02:00
|
|
|
line = ''
|
2019-10-15 19:42:35 +02:00
|
|
|
destination = ""
|
|
|
|
|
|
|
|
def __init__(self, args):
|
2019-10-16 19:59:13 +02:00
|
|
|
request_tmp = ' '.join(args)
|
|
|
|
argument_names = re.findall(r' to | in | times ', request_tmp)
|
|
|
|
arguments = re.split(r' to | in | times ', request_tmp)
|
2019-10-17 20:19:37 +02:00
|
|
|
if not arguments[0].isdigit():
|
|
|
|
reply = search_station(arguments[0])
|
|
|
|
if len(reply) == 1:
|
|
|
|
self.station_id = reply[0]['stationId']
|
|
|
|
else:
|
|
|
|
raise MultipleStationsFoundError(request_tmp, arguments[0], reply)
|
|
|
|
else:
|
|
|
|
self.station_id = arguments[0]
|
|
|
|
|
2019-10-16 19:59:13 +02:00
|
|
|
if ' to ' in argument_names:
|
2019-10-17 19:03:09 +02:00
|
|
|
self.destination = arguments[argument_names.index(' to ') + 1]
|
2019-10-16 19:59:13 +02:00
|
|
|
if ' in ' in argument_names:
|
2019-10-17 19:03:09 +02:00
|
|
|
self.line = arguments[argument_names.index(' in ') + 1]
|
2019-10-16 19:59:13 +02:00
|
|
|
if ' times ' in argument_names:
|
|
|
|
self.departure_count = int(arguments[argument_names.index(' times ') + 1])
|
2019-10-15 19:42:35 +02:00
|
|
|
|
|
|
|
|
2019-10-17 20:19:37 +02:00
|
|
|
def build_menu(buttons,
|
|
|
|
n_cols,
|
|
|
|
header_buttons=None,
|
|
|
|
footer_buttons=None):
|
|
|
|
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
|
|
|
|
if header_buttons:
|
|
|
|
menu.insert(0, [header_buttons])
|
|
|
|
if footer_buttons:
|
|
|
|
menu.append([footer_buttons])
|
|
|
|
return menu
|
|
|
|
|
|
|
|
|
|
|
|
def reply_multiple_stations(message, message_text, queried_station, station_list):
|
|
|
|
button_list = []
|
|
|
|
for station in station_list:
|
|
|
|
button_list.append(InlineKeyboardButton(station['fullName'],
|
|
|
|
callback_data="/vvs " + message_text
|
|
|
|
.replace(queried_station, station['stationId'])))
|
|
|
|
reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
|
|
|
|
message.reply_text("Multiple stations found:", reply_markup=reply_markup)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_multiple_stations_reply(update, context):
|
|
|
|
query = parse_station(update.callback_query.data.split(' ')[1:])
|
|
|
|
departures = get_vvs_departures(query)
|
|
|
|
for reply in departures:
|
|
|
|
context.bot.send_message(update.effective_chat['id'], reply)
|
|
|
|
|
|
|
|
|
2019-10-11 22:40:28 +02:00
|
|
|
def search_station(query):
|
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + query)
|
2019-10-17 20:19:37 +02:00
|
|
|
if request.status_code != 200:
|
|
|
|
raise ServerCommunicationError
|
|
|
|
if request.status_code == 200 and len(request.text) <= 2:
|
|
|
|
raise StationNotFoundError
|
2019-10-11 22:40:28 +02:00
|
|
|
else:
|
2019-10-17 20:19:37 +02:00
|
|
|
return request.json()
|
2019-10-11 22:40:28 +02:00
|
|
|
|
|
|
|
|
2019-10-17 16:42:14 +02:00
|
|
|
def handle_vvs(update, context):
|
2019-10-17 20:19:37 +02:00
|
|
|
try:
|
|
|
|
query = parse_station(context.args)
|
|
|
|
except MultipleStationsFoundError as error:
|
|
|
|
reply_multiple_stations(update.message, error.message_text, error.queried_station, error.station_list)
|
|
|
|
return
|
2019-10-17 19:03:09 +02:00
|
|
|
departures = get_vvs_departures(query)
|
|
|
|
|
|
|
|
for reply in departures:
|
2019-10-17 16:42:14 +02:00
|
|
|
update.message.reply_text(reply)
|
|
|
|
|
|
|
|
|
2019-10-17 19:03:09 +02:00
|
|
|
def parse_station(args):
|
2019-10-17 16:42:14 +02:00
|
|
|
if len(args) == 0:
|
2019-10-17 19:03:09 +02:00
|
|
|
raise NoArgError
|
2019-10-17 20:19:37 +02:00
|
|
|
try:
|
|
|
|
query = Query(args)
|
|
|
|
except StationNotFoundError:
|
|
|
|
raise
|
|
|
|
except MultipleStationsFoundError:
|
|
|
|
raise
|
2019-10-15 19:42:35 +02:00
|
|
|
if query.station_id == -1:
|
2019-10-17 19:03:09 +02:00
|
|
|
raise StationNotFoundError
|
|
|
|
return query
|
|
|
|
|
|
|
|
|
|
|
|
def get_vvs_departures(query):
|
|
|
|
reply = []
|
2019-10-15 19:42:35 +02:00
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/" + query.station_id + "/departures")
|
2019-10-11 22:40:28 +02:00
|
|
|
if request.status_code != 200:
|
2019-10-17 19:03:09 +02:00
|
|
|
raise ServerCommunicationError
|
2019-10-17 16:42:14 +02:00
|
|
|
reply.append("Next departures:")
|
2019-10-15 19:42:35 +02:00
|
|
|
printed_departures = 0
|
|
|
|
for station in request.json():
|
|
|
|
if station['direction'].casefold().find(query.destination) != -1 or station['direction'].find(
|
|
|
|
query.destination) != -1:
|
2019-10-16 19:59:13 +02:00
|
|
|
if query.line == '' or (query.line != -1 and station['number'] == query.line):
|
2019-10-17 16:42:14 +02:00
|
|
|
reply.append(
|
2019-10-16 19:59:13 +02:00
|
|
|
"Line " + station['number'] + " to \"" + station['direction'] + "\" at "
|
|
|
|
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2)
|
2019-10-18 12:33:11 +02:00
|
|
|
+ " (+" + str(station['delay']) + ")")
|
2019-10-16 19:59:13 +02:00
|
|
|
printed_departures += 1
|
2019-10-15 19:42:35 +02:00
|
|
|
if printed_departures >= query.departure_count:
|
|
|
|
break
|
2019-10-17 16:42:14 +02:00
|
|
|
return reply
|
2019-10-11 22:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def inline_station_search(update, context):
|
|
|
|
query = update.inline_query.query
|
|
|
|
if len(query) < 5 or not query:
|
|
|
|
return
|
|
|
|
results = list()
|
2019-10-15 19:17:57 +02:00
|
|
|
for station in get_station_id_list(query):
|
2019-10-11 22:40:28 +02:00
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
id=station['stationId'],
|
|
|
|
title=station['name'],
|
|
|
|
input_message_content=InputTextMessageContent("/vvs " + station['stationId'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
context.bot.answer_inline_query(update.inline_query.id, results)
|
|
|
|
|
|
|
|
|
2019-10-15 19:17:57 +02:00
|
|
|
def get_station_id_list(name):
|
2019-10-11 22:40:28 +02:00
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + name)
|
|
|
|
return request.json()
|
|
|
|
|
|
|
|
|
2019-10-17 19:03:09 +02:00
|
|
|
def error_callback(update, context):
|
|
|
|
try:
|
|
|
|
raise context.error
|
|
|
|
except NoArgError:
|
|
|
|
update.message.reply_text('No argument specified!')
|
|
|
|
return
|
|
|
|
except StationNotFoundError:
|
|
|
|
update.message.reply_text('No station matching this name found!')
|
|
|
|
return
|
|
|
|
except ServerCommunicationError:
|
|
|
|
update.message.reply_text('Error with server communication')
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2019-10-11 22:40:28 +02:00
|
|
|
inline_station_search_handler = InlineQueryHandler(inline_station_search)
|
|
|
|
|
|
|
|
dispatcher.add_handler(inline_station_search_handler)
|
2019-10-17 19:03:09 +02:00
|
|
|
dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
|
|
|
|
dispatcher.add_error_handler(error_callback)
|
2019-10-17 20:19:37 +02:00
|
|
|
dispatcher.add_handler(CallbackQueryHandler(handle_multiple_stations_reply))
|
2019-10-11 22:40:28 +02:00
|
|
|
|
|
|
|
updater.start_polling()
|
|
|
|
updater.idle()
|