2016-07-09 13:51:39 +00:00
|
|
|
import collections, datetime
|
2016-06-27 23:09:33 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals"
|
|
|
|
URL_BUS_SEARCH = "https://api.tfl.gov.uk/StopPoint/Search/%s"
|
|
|
|
|
|
|
|
class Module(object):
|
2016-07-05 11:16:40 +00:00
|
|
|
_name = "TFL"
|
2016-06-27 23:09:33 +00:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
bot.events.on("received").on("command").on("tflbus"
|
2016-07-05 11:16:40 +00:00
|
|
|
).hook(self.bus, min_args=1,
|
|
|
|
help="Get bus due times for a TfL bus stop",
|
|
|
|
usage="<stop_id>")
|
2016-06-27 23:09:33 +00:00
|
|
|
|
|
|
|
def bus(self, event):
|
|
|
|
app_id = self.bot.config["tfl-api-id"]
|
|
|
|
app_key = self.bot.config["tfl-api-key"]
|
|
|
|
stop_id = event["args_split"][0]
|
2016-07-09 13:51:39 +00:00
|
|
|
target_bus_route = None
|
2016-07-06 12:18:27 +00:00
|
|
|
if len(event["args_split"]) > 1:
|
2016-07-09 13:51:39 +00:00
|
|
|
target_bus_route = event["args_split"][1].lower()
|
2016-06-27 23:09:33 +00:00
|
|
|
if stop_id.isdigit():
|
|
|
|
bus_search = Utils.get_url(URL_BUS_SEARCH % stop_id, get_params={
|
|
|
|
"app_id": app_id, "app_key": app_key}, json=True)
|
|
|
|
if bus_search["matches"]:
|
|
|
|
bus_stop = bus_search["matches"][0]
|
|
|
|
real_stop_id = bus_stop["id"]
|
2016-07-05 11:16:40 +00:00
|
|
|
stop_name = bus_stop["name"]
|
2016-06-27 23:09:33 +00:00
|
|
|
bus_stop = Utils.get_url(URL_BUS % real_stop_id, get_params={
|
|
|
|
"app_id": app_id, "app_key": app_key}, json=True)
|
|
|
|
busses = []
|
|
|
|
for bus in bus_stop:
|
|
|
|
bus_number = bus["lineName"]
|
|
|
|
bus_due_iso8601 = bus["expectedArrival"]
|
|
|
|
bus_due = datetime.datetime.strptime(bus_due_iso8601,
|
|
|
|
"%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
time_until = bus_due-datetime.datetime.utcnow()
|
|
|
|
time_until = int(time_until.total_seconds()/60)
|
|
|
|
busses.append([bus_number, time_until])
|
|
|
|
if busses:
|
|
|
|
busses = sorted(busses, key=lambda b: b[-1])
|
2016-07-09 13:51:39 +00:00
|
|
|
busses_formatted = collections.OrderedDict()
|
|
|
|
for bus_route, time_until in busses:
|
|
|
|
if bus_route in busses_formatted:
|
|
|
|
continue
|
|
|
|
if time_until == 0:
|
|
|
|
time_until = "due"
|
|
|
|
elif time_until == 1:
|
|
|
|
time_until = "in 1 minute"
|
2016-06-27 23:18:09 +00:00
|
|
|
else:
|
2016-07-09 13:51:39 +00:00
|
|
|
time_until = "in %d minutes" % time_until
|
|
|
|
if not target_bus_route or bus_route.lower() == target_bus_route:
|
|
|
|
busses_formatted[bus_route] = time_until
|
|
|
|
busses_string = ", ".join(["%s (%s)" % (bus_route, time_until
|
|
|
|
) for bus_route, time_until in busses_formatted.items()])
|
2016-07-05 11:16:40 +00:00
|
|
|
event["stdout"].write("%s (%s): %s" % (stop_name, stop_id,
|
2016-07-09 13:51:39 +00:00
|
|
|
busses_string))
|
2016-06-27 23:09:33 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("%s: No busses due" % stop_id)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Bus ID '%s' unknown" % stop_id)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Please provide a numeric bus stop ID")
|