def stopRouteRequest(stopID, routeID, devStoreKey): logging.debug("Stop/Route Request started") # got fetch all of the data for this stop sid = stopID + str(devStoreKey) + str(time.time()) routes = asynch.aggregateBusesAsynch(sid,stopID,routeID) if routes is None: response_dict = {'status':'0', 'timestamp':api_utils.getLocalTimestamp(), 'info':'No routes found' } return response_dict response_dict = {'status':'0', 'timestamp':api_utils.getLocalTimestamp() } # there should only be results. we assume the results are sorted by time stop_dict = {'stopID':stopID,} route_results = [] for r in routes: if not api_utils.inthepast(r.arrivalTime): route_results.append(dict({'routeID':r.routeID, 'vehicleID':'unknown', 'minutes':str(api_utils.computeCountdownMinutes(r.arrivalTime)), 'arrivalTime':r.arrivalTime, 'destination':r.destination, })) # add the populated stop details to the response stop_dict.update({'route':route_results}) response_dict.update({'stop':stop_dict}) return response_dict
def getRoutes(refresh): if refresh is False: # do we already have it in the datastore? api = db.GqlQuery('select * from StaticAPIs where method = :1', api_utils.GETROUTES).get() if api is not None: logging.debug('---> datastore hit') return api.json logging.debug('---> datastore lookup starting!') offset = 0 q = RouteListing.all() routes = q.fetch(1000) hits = {} response_dict = {'status': 0, 'timestamp': api_utils.getLocalTimestamp()} while len(routes) > 0: offset += len(routes) ## stopped here trying to create a map of unique routes and endpoints ## for r in routes: # are we tracking this route/direction pair? key = r.route + ':' + r.direction hits[key] = hits.get(key, 0) + 1 # get more routes routes = q.fetch(1000, offset) routeMap = {} for k, v in hits.iteritems(): key = k.split(':') routeID = key[0] direction = key[1] directionLabel = api_utils.getDirectionLabel(direction) logging.debug('adding direction %s to route %s' % (directionLabel, routeID)) if routeID in routeMap: routeMap[routeID].append(directionLabel) else: routeMap[routeID] = list() routeMap[routeID].append(directionLabel) route_results = [] for k, v in routeMap.iteritems(): route_results.append(dict({'routeID': k, 'directions': routeMap[k]})) # add the populated route details to the response response_dict.update({'routes': route_results}) json_results = json.dumps(response_dict) static = StaticAPIs() static.method = api_utils.GETROUTES static.json = json_results static.put() return json_results
def stopRequest(stopID, devKey): response_dict = None # look for memcahced results results_cache_key = 'stopRequest::%s' % stopID response_dict = memcache.get(results_cache_key) if( response_dict is None ): logging.debug("getarrivals : cache miss") response_dict = {'status':'0', 'timestamp':api_utils.getLocalTimestamp() } # unique key to track this request t = str(time.time()).split('.')[0] sid = '%s::%s::%s' % (stopID,devKey,t) # fetch all of the data for this stop routes = asynch.aggregateBusesAsynch(sid,stopID) if routes is None or len(routes) == 0: response_dict['status'] = '-1' response_dict['description'] = 'No routes found for this stop' response_dict['stopID'] = stopID return response_dict # get the stop details stop_dict = {'stopID':stopID,} # take the first 10 results. we assume the results are sorted by time #route_results = sorted(route_results, key=attrgetter('time')) route_results = [] for r in routes: minutes = api_utils.computeCountdownMinutes(r.arrivalTime) if minutes > 0: route_results.append(dict({'routeID':r.routeID, 'vehicleID':'unknown', 'minutes':str(minutes), 'arrivalTime':r.arrivalTime, 'destination':r.destination, })) # add the populated stop details to the response stop_dict.update({'route':route_results}); response_dict.update({'stop':stop_dict}) # cleanup the results #asynch.clean(sid) # stash results in the cache memcache.set(results_cache_key,response_dict,60) else: logging.debug('getarrivals : cash hit') response_dict['cached'] = True return response_dict
def getRoutes(refresh): if refresh is False: # do we already have it in the datastore? api = db.GqlQuery('select * from StaticAPIs where method = :1', api_utils.GETROUTES).get() if api is not None: logging.debug('---> datastore hit'); return api.json logging.debug('---> datastore lookup starting!') offset = 0 q = RouteListing.all() routes = q.fetch(1000) hits = {} response_dict = {'status':0,'timestamp':api_utils.getLocalTimestamp()} while len(routes) > 0: offset += len(routes) ## stopped here trying to create a map of unique routes and endpoints ## for r in routes: # are we tracking this route/direction pair? key = r.route + ':' + r.direction hits[key] = hits.get(key,0) + 1 # get more routes routes = q.fetch(1000,offset) routeMap = {} for k,v in hits.iteritems(): key = k.split(':') routeID = key[0] direction = key[1] directionLabel = api_utils.getDirectionLabel(direction) logging.debug('adding direction %s to route %s' % (directionLabel,routeID)) if routeID in routeMap: routeMap[routeID].append(directionLabel) else: routeMap[routeID] = list() routeMap[routeID].append(directionLabel) route_results = [] for k,v in routeMap.iteritems(): route_results.append(dict({'routeID':k,'directions':routeMap[k]})) # add the populated route details to the response response_dict.update({'routes':route_results}) json_results = json.dumps(response_dict) static = StaticAPIs() static.method = api_utils.GETROUTES static.json = json_results static.put() return json_results
def routeRequest(routeID, destination): if destination is not None: destination_code = getDestinationCode(destination) if destination_code == -1: response_dict = { 'status': '-1', 'info': ('Unknown destination %s' % destination) } return response_dict else: logging.debug('route listing query for route %s and direction %s' % (routeID, destination_code)) q = db.GqlQuery( 'select * from RouteListing where route = :1 and direction = :2 order by route', routeID, destination_code) else: q = db.GqlQuery( 'select * from RouteListing where route = :1 order by route', routeID) routes = q.fetch(500) if routes is None: response_dict = {'status': '0', 'info': 'No stops found'} return response_dict response_dict = { 'status': '0', 'timestamp': api_utils.getLocalTimestamp(), 'routeID': routeID } stop_results = [] for r in routes: stop = r.stopLocation if stop is None: logging.error('API: ERROR, no location!?') continue stop_results.append( dict({ 'stopID': stop.stopID, 'intersection': stop.intersection, 'latitude': stop.location.lat, 'longitude': stop.location.lon, #'destination' : stop.direction, })) # add the populated stop details to the response response_dict.update({'stops': stop_results}) return response_dict
def stopRouteRequest(stopID, routeID, devStoreKey): logging.debug("Stop/Route Request started") # got fetch all of the data for this stop sid = stopID + str(devStoreKey) + str(time.time()) routes = asynch.aggregateBusesAsynch(sid, stopID, routeID) if routes is None: response_dict = { 'status': '0', 'timestamp': api_utils.getLocalTimestamp(), 'info': 'No routes found' } return response_dict response_dict = {'status': '0', 'timestamp': api_utils.getLocalTimestamp()} # there should only be results. we assume the results are sorted by time stop_dict = { 'stopID': stopID, } route_results = [] for r in routes: if not api_utils.inthepast(r.arrivalTime): route_results.append( dict({ 'routeID': r.routeID, 'vehicleID': 'unknown', 'minutes': str(api_utils.computeCountdownMinutes(r.arrivalTime)), 'arrivalTime': r.arrivalTime, 'destination': r.destination, })) # add the populated stop details to the response stop_dict.update({'route': route_results}) response_dict.update({'stop': stop_dict}) return response_dict
def routeRequest(routeID,destination): if destination is not None: destination_code = getDestinationCode(destination) if destination_code == -1 : response_dict = {'status' : '-1', 'info' : ('Unknown destination %s' % destination) } return response_dict else: logging.debug('route listing query for route %s and direction %s' % (routeID,destination_code)) q = db.GqlQuery('select * from RouteListing where route = :1 and direction = :2 order by route', routeID, destination_code) else: q = db.GqlQuery('select * from RouteListing where route = :1 order by route', routeID) routes = q.fetch(500) if routes is None: response_dict = {'status':'0', 'info':'No stops found' } return response_dict response_dict = {'status':'0', 'timestamp':api_utils.getLocalTimestamp(), 'routeID':routeID } stop_results = [] for r in routes: stop = r.stopLocation if stop is None: logging.error('API: ERROR, no location!?') continue stop_results.append(dict({'stopID' : stop.stopID, 'intersection' : stop.intersection, 'latitude' : stop.location.lat, 'longitude' : stop.location.lon, #'destination' : stop.direction, })) # add the populated stop details to the response response_dict.update({'stops':stop_results}) return response_dict
def routeRequest(routeID): # need to first translate the routeID into the # internal Metro code for that route. route = asynch.getRouteListing(routeID=routeID) if not route or len(route) is 0: logging.error("Exiting early: unable to locate route listing: " + routeID) return api_utils.buildErrorResponse('-1', 'Unknown routeID') else: metro_route_code = route[0].routeCode logging.debug('%s - %s' % (routeID, metro_route_code)) # now go fetch the vehicle details from Metro... loop = 0 done = False result = None while not done and loop < 3: try: payload = {} payload['routeID'] = metro_route_code headers = {'Content-Type': 'application/json'} result = urlfetch.fetch( url=VEHICLE_URL_BASE, payload=json.dumps(payload), method=urlfetch.POST, headers=headers) done = True except urlfetch.Error: logging.error("Error loading page (%s)... sleeping" % loop) if result: logging.debug("Error status: %s" % result.status_code) logging.debug("Error header: %s" % result.headers) logging.debug("Error content: %s" % result.content) time.sleep(6) loop = loop+1 if result is None or result.status_code != 200: logging.error("Exiting early: error fetching URL: " + result.status_code) return api_utils.buildErrorResponse('-1', 'Error reading live Metro feed') # vehicle results are in a JSON array # { "d" : [ {}, {} ] } json_result = json.loads(result.content) vehicles = json_result['d'] if vehicles: vehicle_count = len(vehicles) else: vehicle_count = 0 # setup the core response object results = dict({'status' : 0, 'routeID' : routeID, 'count' : vehicle_count, 'timestamp' : api_utils.getLocalTimestamp(), 'vehicles' : list()}) if vehicles: # loop through all vehicles on the route and parse # out details for the response for v in vehicles: if v == vehicles[-1]: break spot = dict({'lat':v['lat'], 'lon':v['lon'], 'direction':v['directionName'], 'vehicleID':v['propertyTag'], 'nextStop':v['nextStop'], 'bikeRack':v['bikeRack'], 'wifiAccess':v['wiFiAccess'], 'wheelChairAccessible':v['wheelChairAccessible'], 'wheelChairLift':v['wheelChairLift']}) results['vehicles'].append(spot) return results
def stopRequest(stopID, devKey): response_dict = None # look for memcahced results results_cache_key = 'stopRequest::%s' % stopID response_dict = memcache.get(results_cache_key) if (response_dict is None): logging.debug("getarrivals : cache miss") response_dict = { 'status': '0', 'timestamp': api_utils.getLocalTimestamp() } # unique key to track this request t = str(time.time()).split('.')[0] sid = '%s::%s::%s' % (stopID, devKey, t) # fetch all of the data for this stop routes = asynch.aggregateBusesAsynch(sid, stopID) if routes is None or len(routes) == 0: response_dict['status'] = '-1' response_dict['description'] = 'No routes found for this stop' response_dict['stopID'] = stopID return response_dict # get the stop details stop_dict = { 'stopID': stopID, } # take the first 10 results. we assume the results are sorted by time #route_results = sorted(route_results, key=attrgetter('time')) route_results = [] for r in routes: minutes = api_utils.computeCountdownMinutes(r.arrivalTime) if minutes > 0: route_results.append( dict({ 'routeID': r.routeID, 'vehicleID': 'unknown', 'minutes': str(minutes), 'arrivalTime': r.arrivalTime, 'destination': r.destination, })) # add the populated stop details to the response stop_dict.update({'route': route_results}) response_dict.update({'stop': stop_dict}) # cleanup the results #asynch.clean(sid) # stash results in the cache memcache.set(results_cache_key, response_dict, 60) else: logging.debug('getarrivals : cash hit') response_dict['cached'] = True return response_dict
def nearbyStops(lat, lon, radius, routeID, destination): route_stops = None # limit results to 200 max_results = 200 # limit the radius value to 500 if radius > 1000: radius = 1000 logging.debug('nearbyStops (%s,%s,%s,%s,%s)' % (lat, lon, radius, routeID, destination)) if routeID is None or routeID == "": results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat, lon), # Or db.GeoPt max_results=max_results, max_distance=radius) else: if destination is not None and destination is not "": destination_code = getDestinationCode(destination) if destination_code == -1: response_dict = { 'status': '-1', 'info': ('Unknown destination %s' % destination) } return response_dict else: # first filter stops by route and destination... logging.debug( '... filter by destination for route %s and destination %s' % (routeID, destination_code)) q = db.GqlQuery( 'select stopID from RouteListing where route = :1 and direction = :2 order by route', routeID, destination_code) routes = q.fetch(1000) route_stops = [] for route in routes: route_stops.append(route.stopID) results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat, lon), # Or db.GeoPt max_results=max_results, max_distance=radius) else: # first filter stops by route and destination... logging.debug('... filter by destination for route %s' % routeID) q = db.GqlQuery('select * from RouteListing where route = :1', routeID) routes = q.fetch(1000) route_stops = [] for route in routes: route_stops.append(route.stopID) results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat, lon), # Or db.GeoPt max_results=max_results, max_distance=radius) if results is None: response_dict = { 'status': '0', 'timestamp': api_utils.getLocalTimestamp(), 'info': 'No stops found', 'stops': [] } return response_dict response_dict = { 'status': '0', 'timestamp': api_utils.getLocalTimestamp(), } stop_results = [] stop_tracking = [] logging.info('loop through %s results' % len(results)) for stop in results: # manually apply the destination filter here because # the GCL query limits our ability to apply it in the # proximity query if route_stops is not None and stop.stopID not in route_stops: #logging.debug('filtered out %s' % stop.stopID) continue # kind of a hack, but limit the results to one per route. # the query will return multiple results for each stop if stop.stopID not in stop_tracking: stop_results.append( dict({ 'stopID': stop.stopID, 'intersection': stop.intersection, 'latitude': stop.location.lat, 'longitude': stop.location.lon, })) logging.debug('appending %s to route tracking list' % stop.stopID) stop_tracking.append(stop.stopID) response_dict.update({'stop': stop_results}) return response_dict
def nearbyStops(lat,lon,radius,routeID,destination): route_stops = None # limit results to 200 max_results = 200 # limit the radius value to 500 if radius > 1000: radius = 1000 logging.debug('nearbyStops (%s,%s,%s,%s,%s)' % (lat,lon,radius,routeID,destination)) if routeID is None or routeID == "": results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat,lon), # Or db.GeoPt max_results=max_results, max_distance=radius) else: if destination is not None and destination is not "": destination_code = getDestinationCode(destination) if destination_code == -1 : response_dict = {'status' : '-1', 'info' : ('Unknown destination %s' % destination) } return response_dict else: # first filter stops by route and destination... logging.debug('... filter by destination for route %s and destination %s' % (routeID,destination_code)) q = db.GqlQuery('select stopID from RouteListing where route = :1 and direction = :2 order by route', routeID, destination_code) routes = q.fetch(1000) route_stops = [] for route in routes: route_stops.append(route.stopID) results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat,lon), # Or db.GeoPt max_results=max_results, max_distance=radius) else: # first filter stops by route and destination... logging.debug('... filter by destination for route %s' % routeID) q = db.GqlQuery('select * from RouteListing where route = :1', routeID) routes = q.fetch(1000) route_stops = [] for route in routes: route_stops.append(route.stopID) results = StopLocation.proximity_fetch( StopLocation.all(), geotypes.Point(lat,lon), # Or db.GeoPt max_results=max_results, max_distance=radius) if results is None: response_dict = {'status':'0', 'timestamp':api_utils.getLocalTimestamp(), 'info':'No stops found', 'stops':[] } return response_dict response_dict = {'status':'0','timestamp':api_utils.getLocalTimestamp(),} stop_results = [] stop_tracking = [] logging.info('loop through %s results' % len(results)) for stop in results: # manually apply the destination filter here because # the GCL query limits our ability to apply it in the # proximity query if route_stops is not None and stop.stopID not in route_stops: #logging.debug('filtered out %s' % stop.stopID) continue # kind of a hack, but limit the results to one per route. # the query will return multiple results for each stop if stop.stopID not in stop_tracking: stop_results.append(dict({ 'stopID':stop.stopID, 'intersection':stop.intersection, 'latitude':stop.location.lat, 'longitude':stop.location.lon, })) logging.debug('appending %s to route tracking list' % stop.stopID) stop_tracking.append(stop.stopID) response_dict.update({'stop':stop_results}) return response_dict