Created
September 17, 2011 01:31
-
-
Save blech/1223502 to your computer and use it in GitHub Desktop.
Find Muni bus 8192. Or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import re | |
import time | |
import urllib | |
from xml.dom.minidom import parse, parseString | |
# data structure | |
ids = dict() | |
muni = "http://webservices.nextbus.com/service/publicXMLFeed" | |
# get list of routes and examine non-tram lines | |
print "Getting routes" | |
url = muni + "?command=routeList&a=sf-muni" | |
d1 = parse(urllib.urlopen(url)) | |
for node in d1.getElementsByTagName('route'): | |
r = node.getAttribute('tag') | |
# skip anything that's not a bus route | |
# if not re.match("^[A-Z]", r): | |
# get all buses for route | |
print "Examining route %s" % r | |
# t = int(time.time()*1000) | |
url = muni+"?command=vehicleLocations&a=sf-muni&r=%s&t=%s" % (r, 0) | |
d2 = parse(urllib.urlopen(url)) | |
for v in d2.getElementsByTagName('vehicle'): | |
vehicle_info = dict() | |
for field in ('id', 'routeTag', 'dirTag', 'lat', 'lon', 'heading', 'speedKmHr'): | |
vehicle_info[field] = v.getAttribute(field) | |
ids[vehicle_info['id']] = vehicle_info | |
print ids.keys() | |
for route in sorted(ids.keys()): | |
print "%s: route %s at speed %02.f" % (route, ids[route]['routeTag'], float(ids[route]['speedKmHr'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment