asweigart

getstopdata.py - SF Muni Google Map

Feb 10th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  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()
Add Comment
Please, Sign In to add comment