Skip to content

Instantly share code, notes, and snippets.

@slingamn
Created November 20, 2011 09:45
Show Gist options
  • Save slingamn/1380087 to your computer and use it in GitHub Desktop.
Save slingamn/1380087 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import optparse
from xml.dom import minidom
from xml.dom.minidom import parse, parseString
import urllib2
import pprint
BART_DATA_URL = "http://www.bart.gov/dev/eta/bart_eta.xml"
abbr_to_name = \
{u'12TH': u'12th St. Oakland City Center',
u'16TH': u'16th St. Mission',
u'19TH': u'19th St. Oakland',
u'24TH': u'24th St. Mission',
u'ASHB': u'Ashby',
u'BALB': u'Balboa Park',
u'BAYF': u'Bay Fair',
u'CAST': u'Castro Valley',
u'CIVC': u'Civic Center',
u'COLM': u'Colma',
u'COLS': u'Coliseum/Oakland Airport',
u'CONC': u'Concord',
u'DALY': u'Daly City',
u'DBRK': u'Downtown Berkeley',
u'DELN': u'El Cerrito del Norte',
u'DUBL': u'Dublin/Pleasanton',
u'EMBR': u'Embarcadero',
u'FRMT': u'Fremont',
u'FTVL': u'Fruitvale',
u'GLEN': u'Glen Park',
u'HAYW': u'Hayward',
u'LAFY': u'Lafayette',
u'LAKE': u'Lake Merritt',
u'MCAR': u'MacArthur',
u'MLBR': u'Millbrae',
u'MONT': u'Montgomery St.',
u'NBRK': u'North Berkeley',
u'NCON': u'North Concord/Martinez',
u'ORIN': u'Orinda',
u'PHIL': u'Pleasant Hill',
u'PITT': u'Pittsburg/Bay Point',
u'PLZA': u'El Cerrito Plaza',
u'POWL': u'Powell St.',
u'RICH': u'Richmond',
u'ROCK': u'Rockridge',
u'SANL': u'San Leandro',
u'SBRN': u'San Bruno',
u'SFIA': u"San Francisco Int'l Airport",
u'SHAY': u'South Hayward',
u'SSAN': u'South San Francisco',
u'UCTY': u'Union City',
u'WCRK': u'Walnut Creek',
u'WOAK': u'West Oakland'}
name_to_abbr = dict((name, abbr) for abbr, name in abbr_to_name.iteritems())
doc = urllib2.urlopen(BART_DATA_URL)
dom = minidom.parse(doc)
def unpack_text(element, tagname):
return element.getElementsByTagName(tagname)[0].firstChild.data
max_dest_len = -1
station_to_status = {}
for elem in dom.documentElement.getElementsByTagName('station'):
#station = elem.getElementsByTagName('abbr')[0].data
try:
station_abbr, station_name, station_time = [unpack_text(elem, name) for name in ('abbr', 'name', 'time')]
except AttributeError:
#XXX sometimes the API returns a borked station, ignore
continue
record = {'station_abbr': station_abbr, 'time': station_time, 'eta': {}}
for status in elem.getElementsByTagName('eta'):
dest = unpack_text(status, 'destination')
max_dest_len = max(max_dest_len, len(dest))
estimate = unpack_text(status, 'estimate')
estimate_pieces = [piece.split(" ")[0] for piece in estimate.split(", ")]
estimate_times = [int(piece) if piece != "Leaving" else 0 for piece in estimate_pieces]
#print station_name, dest, estimate_times
#record['eta'][name_to_abbr[dest]] = estimate_times
record['eta'][dest] = estimate_times
station_to_status[station_abbr] = record
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
origin = args[0].upper() if len(args) > 0 else 'POWL'
humanreadable_origin = abbr_to_name.get(origin)
if not humanreadable_origin:
print >>sys.stderr, "Unrecognized abbreviation %s." % (origin,)
sys.exit(1)
status = station_to_status.get(origin)
if status is None:
print "No departure times available for %s." % (humanreadable_origin,)
sys.exit(0)
print "Departure times for %s (as of %s)" % (humanreadable_origin, status['time'])
for dest, times in status['eta'].iteritems():
print "%-*s %s" % (max_dest_len, dest, " mins, ".join(str(time) for time in times) + " mins")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment