tfl improvements
This commit is contained in:
parent
f8f011f69e
commit
4b4c5e8103
1 changed files with 46 additions and 45 deletions
|
@ -12,6 +12,8 @@ URL_STOP_SEARCH = "https://api.tfl.gov.uk/StopPoint/Search/%s"
|
||||||
|
|
||||||
URL_VEHICLE = "https://api.tfl.gov.uk/Vehicle/%s/Arrivals"
|
URL_VEHICLE = "https://api.tfl.gov.uk/Vehicle/%s/Arrivals"
|
||||||
|
|
||||||
|
PLATFORM_TYPES = ["Northbound", "Southbound", "Eastbound", "Westbound", "Inner Rail", "Outer Rail"]
|
||||||
|
|
||||||
class Module(object):
|
class Module(object):
|
||||||
_name = "TFL"
|
_name = "TFL"
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
|
@ -33,6 +35,30 @@ class Module(object):
|
||||||
help="Get information for a given vehicle",
|
help="Get information for a given vehicle",
|
||||||
usage="<ID>")
|
usage="<ID>")
|
||||||
|
|
||||||
|
def vehicle_span(self, arrival_time, human=True):
|
||||||
|
vehicle_due_iso8601 = arrival_time
|
||||||
|
if "." in vehicle_due_iso8601:
|
||||||
|
vehicle_due_iso8601 = vehicle_due_iso8601.split(".")[0]+"Z"
|
||||||
|
vehicle_due = datetime.datetime.strptime(vehicle_due_iso8601,
|
||||||
|
"%Y-%m-%dT%H:%M:%SZ")
|
||||||
|
time_until = vehicle_due-datetime.datetime.utcnow()
|
||||||
|
time_until = int(time_until.total_seconds()/60)
|
||||||
|
|
||||||
|
if time_until == 0: human_time = "due"
|
||||||
|
else: human_time = "in %s min" % time_until
|
||||||
|
|
||||||
|
if human: return human_time
|
||||||
|
else: return time_until
|
||||||
|
|
||||||
|
def platform(self, platform, short=False):
|
||||||
|
p = re.compile("(?:(.*) - Platform (\\d+)|(.*bound) Platform (\\d+))")
|
||||||
|
m = p.match(platform)
|
||||||
|
if m:
|
||||||
|
platform = "platform %s (%s)" % (m.group(2), m.group(1))
|
||||||
|
if short and m.group(1) in PLATFORM_TYPES:
|
||||||
|
platform = m.group(2)
|
||||||
|
return platform
|
||||||
|
|
||||||
def bus(self, event):
|
def bus(self, event):
|
||||||
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"]
|
||||||
|
@ -63,44 +89,39 @@ class Module(object):
|
||||||
busses = []
|
busses = []
|
||||||
for bus in bus_stop:
|
for bus in bus_stop:
|
||||||
bus_number = bus["lineName"]
|
bus_number = bus["lineName"]
|
||||||
bus_due_iso8601 = bus["expectedArrival"]
|
human_time = self.vehicle_span(bus["expectedArrival"])
|
||||||
if "." in bus_due_iso8601:
|
time_until = self.vehicle_span(bus["expectedArrival"], human=False)
|
||||||
bus_due_iso8601 = bus_due_iso8601.split(".")[0]+"Z"
|
|
||||||
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)
|
|
||||||
|
|
||||||
# Nice human friendly time (also consistent with how TfL display it)
|
|
||||||
if time_until == 0: human_time = "due"
|
|
||||||
elif time_until == 1: human_time = "in 1 minute"
|
|
||||||
else: human_time = "in %d minutes" % time_until
|
|
||||||
|
|
||||||
# If the mode is "tube", "Underground Station" is redundant
|
# If the mode is "tube", "Underground Station" is redundant
|
||||||
destination = bus.get("destinationName", "?")
|
destination = bus.get("destinationName", "?")
|
||||||
if (bus["modeName"] == "tube"): destination = destination.replace(" Underground Station", "")
|
if (bus["modeName"] == "tube"): destination = destination.replace(" Underground Station", "")
|
||||||
|
|
||||||
busses.append({"route": bus_number, "time": time_until, "id": bus["vehicleId"],
|
busses.append({"route": bus_number, "time": time_until, "id": bus["vehicleId"],
|
||||||
"destination": destination, "human_time": human_time,
|
"destination": destination, "human_time": human_time, "mode": bus["modeName"],
|
||||||
"mode" : bus["modeName"]})
|
"platform": bus["platformName"],
|
||||||
|
"platform_short" : self.platform(bus["platformName"], short=True)})
|
||||||
if busses:
|
if busses:
|
||||||
busses = sorted(busses, key=lambda b: b["time"])
|
busses = sorted(busses, key=lambda b: b["time"])
|
||||||
busses_filtered = []
|
busses_filtered = []
|
||||||
bus_routes = []
|
bus_route_dest = []
|
||||||
|
bus_route_plat = []
|
||||||
|
|
||||||
# dedup if target route isn't "*", filter if target route isn't None or "*"
|
# dedup if target route isn't "*", filter if target route isn't None or "*"
|
||||||
for b in busses:
|
for b in busses:
|
||||||
if target_bus_route != "*":
|
if target_bus_route != "*":
|
||||||
if b["route"] in bus_routes: continue
|
if (b["route"], b["destination"]) in bus_route_dest: continue
|
||||||
bus_routes.append(b["route"])
|
if bus_route_plat.count((b["route"], b["platform"])) >= 2: continue
|
||||||
|
bus_route_plat.append((b["route"], b["platform"]))
|
||||||
|
bus_route_dest.append((b["route"], b["destination"]))
|
||||||
if b["route"] == target_bus_route or not target_bus_route:
|
if b["route"] == target_bus_route or not target_bus_route:
|
||||||
busses_filtered.append(b)
|
busses_filtered.append(b)
|
||||||
else:
|
else:
|
||||||
busses_filtered.append(b)
|
busses_filtered.append(b)
|
||||||
|
|
||||||
# do the magic formatty things!
|
# do the magic formatty things!
|
||||||
busses_string = ", ".join(["%s (%s, %s)" % (b["destination"], b["route"], b["human_time"]
|
busses_string = ", ".join(["%s (%s, %s)" % (b["destination"], b["route"], b["human_time"],
|
||||||
) for b in busses_filtered])
|
) for b in busses_filtered])
|
||||||
|
|
||||||
event["stdout"].write("%s (%s): %s" % (stop_name, stop_id,
|
event["stdout"].write("%s (%s): %s" % (stop_name, stop_id,
|
||||||
busses_string))
|
busses_string))
|
||||||
else:
|
else:
|
||||||
|
@ -158,11 +179,9 @@ class Module(object):
|
||||||
"app_id": app_id, "app_key": app_key, "maxResults": "6", "faresOnly": "False"}, json=True)
|
"app_id": app_id, "app_key": app_key, "maxResults": "6", "faresOnly": "False"}, json=True)
|
||||||
if stop_search:
|
if stop_search:
|
||||||
for stop in stop_search["matches"]:
|
for stop in stop_search["matches"]:
|
||||||
print(stop)
|
pass
|
||||||
results = ["%s (%s): %s" % (stop["name"], ", ".join(stop["modes"]), stop["id"]) for stop in stop_search["matches"]]
|
results = ["%s (%s): %s" % (stop["name"], ", ".join(stop["modes"]), stop["id"]) for stop in stop_search["matches"]]
|
||||||
event["stdout"].write("; ".join(results))
|
event["stdout"].write("[%s results] %s" % (stop_search["total"], "; ".join(results)))
|
||||||
if not results:
|
|
||||||
event["stderr"].write("No results")
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("No results")
|
event["stderr"].write("No results")
|
||||||
|
|
||||||
|
@ -175,28 +194,10 @@ class Module(object):
|
||||||
vehicle = Utils.get_url(URL_VEHICLE % vehicle_id, get_params={
|
vehicle = Utils.get_url(URL_VEHICLE % vehicle_id, get_params={
|
||||||
"app_id": app_id, "app_key": app_key}, json=True)[0]
|
"app_id": app_id, "app_key": app_key}, json=True)[0]
|
||||||
|
|
||||||
#Stolen from bus
|
arrival_time = self.vehicle_span(vehicle["expectedArrival"], human=False)
|
||||||
vehicle_due_iso8601 = vehicle["expectedArrival"]
|
platform = self.platform(vehicle["platformName"])
|
||||||
if "." in vehicle_due_iso8601:
|
|
||||||
vehicle_due_iso8601 = vehicle_due_iso8601.split(".")[0]+"Z"
|
|
||||||
vehicle_due = datetime.datetime.strptime(vehicle_due_iso8601,
|
|
||||||
"%Y-%m-%dT%H:%M:%SZ")
|
|
||||||
time_until = vehicle_due-datetime.datetime.utcnow()
|
|
||||||
time_until = int(time_until.total_seconds()/60)
|
|
||||||
|
|
||||||
if time_until == 0: human_time = "due"
|
event["stdout"].write("%s (%s) to %s. %s. Arrival at %s (%s) in %s minutes on %s" % (
|
||||||
elif time_until == 1: human_time = "in 1 minute"
|
|
||||||
else: human_time = "in %d minutes" % time_until
|
|
||||||
|
|
||||||
platform = vehicle["platformName"]
|
|
||||||
|
|
||||||
p = re.compile("(.*) - Platform (\\d+)")
|
|
||||||
m = p.match(platform)
|
|
||||||
if m:
|
|
||||||
platform = "platform %s (%s)" % (m.group(2), m.group(1))
|
|
||||||
###
|
|
||||||
|
|
||||||
event["stdout"].write("%s (%s) to %s. %s. Arrival at %s (%s) %s on %s" % (
|
|
||||||
vehicle["vehicleId"], vehicle["lineName"], vehicle["destinationName"], vehicle["currentLocation"],
|
vehicle["vehicleId"], vehicle["lineName"], vehicle["destinationName"], vehicle["currentLocation"],
|
||||||
vehicle["stationName"], vehicle["naptanId"], human_time, platform))
|
vehicle["stationName"], vehicle["naptanId"], arrival_time, platform))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue