From ddd2a0a463deffc7b65d33802d133410934bba77 Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sun, 10 Jul 2016 10:19:29 +0100 Subject: [PATCH] TfL tube line status --- modules/tfl.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/modules/tfl.py b/modules/tfl.py index 5c50548e..f2036bca 100644 --- a/modules/tfl.py +++ b/modules/tfl.py @@ -4,6 +4,9 @@ import Utils URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals" URL_BUS_SEARCH = "https://api.tfl.gov.uk/StopPoint/Search/%s" +URL_LINE = "https://api.tfl.gov.uk/Line/Mode/tube/Status" +LINE_NAMES = ["bakerloo", "central", "circle", "district", "hammersmith and city", "jubilee", "metropolitan", "piccadilly", "victoria", "waterloo and city"] + class Module(object): _name = "TFL" def __init__(self, bot): @@ -12,6 +15,10 @@ class Module(object): ).hook(self.bus, min_args=1, help="Get bus due times for a TfL bus stop", usage="") + bot.events.on("received").on("command").on("tflline" + ).hook(self.line, + help="Get line status for TfL underground lines", + usage="") def bus(self, event): app_id = self.bot.config["tfl-api-id"] @@ -62,3 +69,42 @@ class Module(object): event["stderr"].write("Bus ID '%s' unknown" % stop_id) else: event["stderr"].write("Please provide a numeric bus stop ID") + + def line(self, event): + app_id = self.bot.config["tfl-api-id"] + app_key = self.bot.config["tfl-api-key"] + + lines = Utils.get_url(URL_LINE, get_params={ + "app_id": app_id, "app_key": app_key}, json=True) + statuses = [] + for line in lines: + for status in line["lineStatuses"]: + entry = { + "id": line["id"], + "name": line["name"], + "severity": status["statusSeverity"], + "description": status["statusSeverityDescription"], + "reason": status.get("reason") + } + statuses.append(entry) + statuses = sorted(statuses, key=lambda line: line["severity"]) + combined = collections.OrderedDict() + for status in statuses: + if not status["description"] in combined: + combined[status["description"]] = [] + combined[status["description"]].append(status) + result = "" + for k, v in combined.items(): + result += k + ": " + result += ", ".join(status["name"] for status in v) + result += "; " + if event["args"]: + result = "" + for status in statuses: + for arg in event["args_split"]: + if arg.lower() in status["name"].lower(): + result += "%s: %s (%d) '%s'; " % (status["name"], status["description"], status["severity"], status["reason"]) + if result: + event["stdout"].write(result[:-2]) + else: + event["stderr"].write("No results")