Skip to content

Instantly share code, notes, and snippets.

@gpaciga
Created December 30, 2020 22:39
Show Gist options
  • Save gpaciga/810747cc29ccf7960bf3097f1a65f5f2 to your computer and use it in GitHub Desktop.
Save gpaciga/810747cc29ccf7960bf3097f1a65f5f2 to your computer and use it in GitHub Desktop.
Print next bus arrival times for a TTC stop
import requests
import xml.etree.ElementTree as ET
import time
AGENCY="ttc"
STOP_ID=12345
PREDICTIONS = f"http://webservices.nextbus.com/service/publicXMLFeed?a={AGENCY}&command=predictions&stopId={STOP_ID}"
def print_next():
xml_predictions = requests.get(PREDICTIONS)
root = ET.fromstring(xml_predictions.text)
upcoming = []
for route in root:
routeNumber = route.attrib['routeTag']
for direction in route:
for prediction in direction:
upcoming.append((routeNumber, int(prediction.attrib['minutes'])))
upcoming.sort(key=lambda x: x[1])
print(", ".join([b[1].__str__() + "m" for b in upcoming]))
while True:
print_next()
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment