changed tflbus command to only show next time for each bus, not every single bus it can find.
This commit is contained in:
parent
f5f756b39a
commit
145cb90c3d
1 changed files with 17 additions and 14 deletions
|
@ -1,4 +1,4 @@
|
||||||
import datetime
|
import collections, datetime
|
||||||
import Utils
|
import Utils
|
||||||
|
|
||||||
URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals"
|
URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals"
|
||||||
|
@ -17,9 +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
|
target_bus_route = None
|
||||||
if len(event["args_split"]) > 1:
|
if len(event["args_split"]) > 1:
|
||||||
bus_route = event["args_split"][1].lower()
|
target_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)
|
||||||
|
@ -40,19 +40,22 @@ 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])
|
||||||
busses_formatted = []
|
busses_formatted = collections.OrderedDict()
|
||||||
for bus in busses:
|
for bus_route, time_until in busses:
|
||||||
if bus[-1] == 0:
|
if bus_route in busses_formatted:
|
||||||
bus[-1] = "due"
|
continue
|
||||||
elif bus[-1] == 1:
|
if time_until == 0:
|
||||||
bus[-1] = "in 1 minute"
|
time_until = "due"
|
||||||
|
elif time_until == 1:
|
||||||
|
time_until = "in 1 minute"
|
||||||
else:
|
else:
|
||||||
bus[-1] = "in %d minutes" % bus[-1]
|
time_until = "in %d minutes" % time_until
|
||||||
if not bus_route or bus[0].lower() == bus_route:
|
if not target_bus_route or bus_route.lower() == target_bus_route:
|
||||||
busses_formatted.append(bus)
|
busses_formatted[bus_route] = time_until
|
||||||
|
busses_string = ", ".join(["%s (%s)" % (bus_route, time_until
|
||||||
|
) for bus_route, time_until in busses_formatted.items()])
|
||||||
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_string))
|
||||||
busses_formatted])))
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("%s: No busses due" % stop_id)
|
event["stderr"].write("%s: No busses due" % stop_id)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue