added a way to filter what bus you're looking for at a stop in tfl.py.

This commit is contained in:
jesopo 2016-07-06 13:18:27 +01:00
parent 5c19b2ea18
commit f5f756b39a
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3

View file

@ -17,6 +17,9 @@ class Module(object):
app_id = self.bot.config["tfl-api-id"] app_id = self.bot.config["tfl-api-id"]
app_key = self.bot.config["tfl-api-key"] app_key = self.bot.config["tfl-api-key"]
stop_id = event["args_split"][0] stop_id = event["args_split"][0]
bus_route = None
if len(event["args_split"]) > 1:
bus_route = event["args_split"][1].lower()
if stop_id.isdigit(): if stop_id.isdigit():
bus_search = Utils.get_url(URL_BUS_SEARCH % stop_id, get_params={ bus_search = Utils.get_url(URL_BUS_SEARCH % stop_id, get_params={
"app_id": app_id, "app_key": app_key}, json=True) "app_id": app_id, "app_key": app_key}, json=True)
@ -37,16 +40,19 @@ class Module(object):
busses.append([bus_number, time_until]) busses.append([bus_number, time_until])
if busses: if busses:
busses = sorted(busses, key=lambda b: b[-1]) busses = sorted(busses, key=lambda b: b[-1])
for i, bus in enumerate(busses): busses_formatted = []
for bus in busses:
if bus[-1] == 0: if bus[-1] == 0:
bus[-1] = "due" bus[-1] = "due"
elif bus[-1] == 1: elif bus[-1] == 1:
bus[-1] = "in 1 minute" bus[-1] = "in 1 minute"
else: else:
bus[-1] = "in %d minutes" % bus[-1] bus[-1] = "in %d minutes" % bus[-1]
if not bus_route or bus[0].lower() == bus_route:
busses_formatted.append(bus)
event["stdout"].write("%s (%s): %s" % (stop_name, stop_id, event["stdout"].write("%s (%s): %s" % (stop_name, stop_id,
", ".join(["%s (%s)" % (number, due) for number, due in busses] ", ".join(["%s (%s)" % (number, due) for number, due in
))) busses_formatted])))
else: else:
event["stderr"].write("%s: No busses due" % stop_id) event["stderr"].write("%s: No busses due" % stop_id)
else: else: