Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

getstopdata.py - SF Muni Google Map

By: asweigart on Feb 10th, 2014  |  syntax: Python  |  size: 1.18 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import pprint, requests, routedata
  2. import xml.etree.ElementTree as ET
  3.  
  4. allRoutesStopData = {} # this will be the big dictionary we store all data in
  5. for route in routedata.allRoutes:
  6.     allRoutesStopData[route] = []
  7.  
  8.     req = requests.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=%s' % (route))
  9.     if req.status_code != 200:
  10.         raise Exception('Could not download route data for %s' % (route))
  11.  
  12.     # load the downloaded XML text into an XML object we can navigate
  13.     root = ET.fromstring(req.text)
  14.     for child in root[0]: # iterate over all the elements in the <route> element
  15.         if child.tag != 'stop':
  16.             continue # skip the non-<stop> elements
  17.  
  18.         allRoutesStopData[route].append({'title': child.attrib['title'],
  19.                                          'lon': child.attrib['lon'],
  20.                                          'lat': child.attrib['lat'],
  21.                                          'stopId': child.attrib['stopId']}) # append the stop
  22.  
  23. # output all the data to a file named munistops.py:
  24. fo = open('munistops.py', 'w')
  25. fo.write('munistops = ' + pprint.pformat(allRoutesStopData))
  26. fo.close()