def get_busses(): """ Get a list of routes currently operated by SF Muni. :return: A list of dicts with route and name attributes. """ res = rest.get('http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=sf-muni') routes = map(lambda route: {'route': route.attrib.get('tag'), 'name': route.attrib.get('title')}, res.route) return routes
def slurp_once(self): """ Download source data from sfgov and index it into ElasticSearch. """ data = rest.get("https://data.sfgov.org/api/views/rqzj-sfat/rows.json?accessType=DOWNLOAD") for truck in self.__parse(data): if not 'Latitude' in truck or not 'Longitude' in truck: logging.debug("Skipping %s", truck) continue if not truck['Latitude'] or not truck['Longitude']: logging.debug("Skipping %s", truck) continue doc = dict() doc['location'] = {"lat": truck['Latitude'], "lon": truck['Longitude']} doc['applicant'] = truck['Applicant'] doc['address'] = truck['Address'] doc['items'] = truck['FoodItems'] self.es.add(truck['id'], doc) logging.debug("Indexed %s", doc)
def get_locations(route, lastTime = None): """ Get the list of active vehicles for a specific route. :param lastTime: Either 0 or the timestamp returned by a previous call to this function. :return: A tuple of (locations, timestamp). Locations is a list of dicts with id, lat, lon, heading, route attributes. """ if not lastTime: lastTime = 0 query = urllib.urlencode({'command': 'vehicleLocations', 'a': 'sf-muni', 'r': route, 't': lastTime}) res = rest.get('http://webservices.nextbus.com/service/publicXMLFeed?{}'.format(query)) try: locations = map(lambda v: { 'id': v.attrib.get('id'), 'lat': v.attrib.get('lat'), 'lon': v.attrib.get('lon'), 'heading': v.attrib.get('heading'), 'route': v.attrib.get('routeTag') }, res.vehicle) return locations, res.lastTime.attrib.get('time') except AttributeError, e: # this happens when the response is empty return [], lastTime
def test_get_returns_parsed_json(self): res = rest.get('http://ip.jsontest.com/') print res self.assertTrue('ip' in res)
def test_get_returns_parsed_xml(self): res = rest.get('http://central.maven.org/maven2/junit/junit/4.8.1/junit-4.8.1.pom') self.assertEqual('4.0.0', res.modelVersion)