Parsing args now based on regex; implemented filter by line

Implementing new arguments is now much easier!
Also user can now filter by line
This commit is contained in:
JuliusFreudenberger 2019-10-16 19:59:13 +02:00
parent ce24dec0c2
commit 21a9da3684

30
bot.py
View file

@ -1,5 +1,6 @@
import logging import logging
import requests import requests
import re
from telegram import InlineQueryResultArticle, InputTextMessageContent from telegram import InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import Updater, CommandHandler, InlineQueryHandler from telegram.ext import Updater, CommandHandler, InlineQueryHandler
@ -13,28 +14,22 @@ logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s
class Query: class Query:
input = ""
station_id = -1 station_id = -1
departure_count = 4 departure_count = 4
line_filter = -1 line = ''
destination = "" destination = ""
def __init__(self, args): def __init__(self, args):
last_index_string = len(args) request_tmp = ' '.join(args)
if args[-1].isdigit(): argument_names = re.findall(r' to | in | times ', request_tmp)
self.departure_count = int(args[-1]) arguments = re.split(r' to | in | times ', request_tmp)
last_index_string -= 1 self.station_id = search_station(arguments[0])
request_tmp = ' '.join(args[0:last_index_string]).strip() if ' to ' in argument_names:
separator_index = request_tmp.find(" to ") self.destination = arguments[argument_names.index(' to ')+1]
if separator_index != -1: if ' in ' in argument_names:
self.input = request_tmp[:separator_index - 1].strip() self.line = arguments[argument_names.index(' in ')+1]
self.destination = request_tmp[separator_index + 4:].strip() if ' times ' in argument_names:
else: self.departure_count = int(arguments[argument_names.index(' times ') + 1])
self.input = request_tmp.strip()
if args[0].isdigit():
self.station_id = args[0].strip()
else:
self.station_id = search_station(self.input)
def start(update): def start(update):
@ -65,6 +60,7 @@ def get_vvs_departures(update, context):
for station in request.json(): for station in request.json():
if station['direction'].casefold().find(query.destination) != -1 or station['direction'].find( if station['direction'].casefold().find(query.destination) != -1 or station['direction'].find(
query.destination) != -1: query.destination) != -1:
if query.line == '' or (query.line != -1 and station['number'] == query.line):
update.message.reply_text( update.message.reply_text(
"Line " + station['number'] + " to \"" + station['direction'] + "\" at " "Line " + station['number'] + " to \"" + station['direction'] + "\" at "
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2) + station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2)