aboutsummaryrefslogtreecommitdiff
path: root/modules/tfl.py
diff options
context:
space:
mode:
authorGravatar jesopo2016-06-28 00:09:33 +0100
committerGravatar jesopo2016-06-28 00:09:33 +0100
commitcbec3657f429f5dcccd177ff4fa45fc1d2b6f9d4 (patch)
treef0f07b525a1509bd66bd641768c26727e9b0ec1f /modules/tfl.py
parentfixed reconnection logic, using the internal timer logic. (diff)
added tfl.py.
Diffstat (limited to 'modules/tfl.py')
-rw-r--r--modules/tfl.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/modules/tfl.py b/modules/tfl.py
new file mode 100644
index 00000000..59c8bc71
--- /dev/null
+++ b/modules/tfl.py
@@ -0,0 +1,45 @@
+import datetime
+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):
+ def __init__(self, bot):
+ self.bot = bot
+ bot.events.on("received").on("command").on("tflbus"
+ ).hook(self.bus, min_args=1)
+
+ 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]
+ 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)
+ print(bus_search)
+ if bus_search["matches"]:
+ bus_stop = bus_search["matches"][0]
+ real_stop_id = bus_stop["id"]
+ 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])
+ event["stdout"].write("%s: %s" % (stop_id, ", ".join(
+ ["%s (in %d minutes)" % (line, due
+ ) for line, due in busses])))
+ 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")