Skip to content

Instantly share code, notes, and snippets.

@corbanmailloux
Created October 27, 2015 04:32
Show Gist options
  • Save corbanmailloux/2fb405332cb2739d6a49 to your computer and use it in GitHub Desktop.
Save corbanmailloux/2fb405332cb2739d6a49 to your computer and use it in GitHub Desktop.
[OUTDATED] Uses RIT's mobile REST API to get bus information. This API is no longer available. :(
import requests
from datetime import datetime, timezone
def main():
"""
r = requests.get("https://m.rit.edu/rest/transit/routes")
for route in r.json()["response"]:
print(route["title"] + " -- " + str(route["running"]))
"""
#time_format = '%Y-%m-%d %I:%M %p'
time_format = "%I:%M %p"
perkins = "rgrta__3384"
gleason = "rgrta__3385"
route_info = "rgrta__28"
r = requests.get("https://m.rit.edu/rest/transit/route",
params={"id": route_info})
print(r.url)
response = r.json()["response"]
for route in response["directions"]:
if route["running"] and len(route["segments"]) > 0:
#? Force only Perkins, testing
if route["id"] != "Perkins":
continue
print("Running now: {0}".format(route["name"]))
stop_lookup = {}
# Make stop lookup
for stop_loc in route["stops"]:
stop_lookup[stop_loc["id"]] = stop_loc["title"]
clean_stops = []
for segment in route["segments"]:
print("Segment:")
for stop_json in segment["stops"]:
if stop_json["arrives"] is not None:
stop = Stop(stop_json, stop_lookup[stop_json["id"]])
#print(" - {0:<32} -- {1}".format(stop_lookup[stop["id"]], arrival.strftime(time_format)))
print(str(stop))
class Stop:
def __init__(self, stop_json, stop_name):
self.arrival = datetime.utcfromtimestamp(int(stop_json["arrives"])).replace(tzinfo=timezone.utc).astimezone(tz=None)
self.id = stop_json["id"]
self.name = stop_name
def __str__(self):
return " - {0:<32} -- {1}".format(self.name, self.arrival.strftime("%I:%M %p"))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment