Added object for query; added option to filter by destination

Parsing and getting arguments now is much easier with object
if user additionally sends "to {destination}" the departures are
filtered to match only this destination (case-insensitive)
This commit is contained in:
JuliusFreudenberger 2019-10-15 19:42:35 +02:00
parent 9338990491
commit 78c337a8eb

59
bot.py
View file

@ -12,6 +12,31 @@ dispatcher = updater.dispatcher
logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
class Query:
input = ""
station_id = -1
departure_count = 4
line_filter = -1
destination = ""
def __init__(self, args):
last_index_string = len(args)
if args[-1].isdigit():
self.departure_count = int(args[-1])
last_index_string -= 1
request_tmp = ' '.join(args[0:last_index_string]).strip()
separator_index = request_tmp.find("to")
if separator_index != -1:
self.input = request_tmp[:separator_index - 1].strip()
self.destination = request_tmp[separator_index + 2:].strip()
else:
self.input = request_tmp
if args[0].isdigit():
self.station_id = args[0].strip()
else:
self.station_id = search_station(request_tmp[:separator_index].strip())
def start(update): def start(update):
update.message.reply_text("Hello") update.message.reply_text("Hello")
@ -25,27 +50,28 @@ def search_station(query):
def get_vvs_departures(update, context): def get_vvs_departures(update, context):
number_departures = 4
last_index_string = len(context.args)
if len(context.args) == 0: if len(context.args) == 0:
update.message.reply_text("No argument specified") update.message.reply_text("No argument specified")
return return
if context.args[-1].isdigit(): query = Query(context.args)
number_departures = context.args[-1] if query.station_id == -1:
last_index_string -= 1 update.message.reply_text("No station matching this name found!")
if context.args[0].isdigit(): return
station_id = context.args[0] request = requests.get("https://efa-api.asw.io/api/v1/station/" + query.station_id + "/departures")
else:
station_id = search_station(' '.join(context.args[0:last_index_string]))
request = requests.get("https://efa-api.asw.io/api/v1/station/" + station_id + "/departures")
if request.status_code != 200: if request.status_code != 200:
update.message.reply_text("Error with server communication") update.message.reply_text("Error with server communication")
update.message.reply_text("Next departures:") update.message.reply_text("Next departures:")
for station in request.json()[:int(number_departures)]: printed_departures = 0
update.message.reply_text( for station in request.json():
"Line " + station['number'] + " to \"" + station['direction'] + "\" at " if station['direction'].casefold().find(query.destination) != -1 or station['direction'].find(
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2) query.destination) != -1:
+ " +" + str(station['delay'])) update.message.reply_text(
"Line " + station['number'] + " to \"" + station['direction'] + "\" at "
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2)
+ " +" + str(station['delay']))
printed_departures += 1
if printed_departures >= query.departure_count:
break
def inline_station_search(update, context): def inline_station_search(update, context):
@ -77,6 +103,3 @@ updater.dispatcher.add_handler(CommandHandler('vvs', get_vvs_departures))
updater.start_polling() updater.start_polling()
updater.idle() updater.idle()
if query.station_id == -1:
update.message.reply_text("No station matching this name found!")
return