TfL tube line status

This commit is contained in:
Evelyn 2016-07-10 10:19:29 +01:00
parent 436b82146f
commit ddd2a0a463

View file

@ -4,6 +4,9 @@ import Utils
URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals" URL_BUS = "https://api.tfl.gov.uk/StopPoint/%s/Arrivals"
URL_BUS_SEARCH = "https://api.tfl.gov.uk/StopPoint/Search/%s" 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): class Module(object):
_name = "TFL" _name = "TFL"
def __init__(self, bot): def __init__(self, bot):
@ -12,6 +15,10 @@ class Module(object):
).hook(self.bus, min_args=1, ).hook(self.bus, min_args=1,
help="Get bus due times for a TfL bus stop", help="Get bus due times for a TfL bus stop",
usage="<stop_id>") usage="<stop_id>")
bot.events.on("received").on("command").on("tflline"
).hook(self.line,
help="Get line status for TfL underground lines",
usage="<line_name>")
def bus(self, event): def bus(self, event):
app_id = self.bot.config["tfl-api-id"] app_id = self.bot.config["tfl-api-id"]
@ -62,3 +69,42 @@ class Module(object):
event["stderr"].write("Bus ID '%s' unknown" % stop_id) event["stderr"].write("Bus ID '%s' unknown" % stop_id)
else: else:
event["stderr"].write("Please provide a numeric bus stop ID") 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")