Example #1
0
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':utils.getLocalTimestamp(),
                         'info':'No routes found'
                        }
        return response_dict
    
    response_dict = {'status':'0',
                     'timestamp':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 utils.inthepast(r.arrivalTime):
            route_results.append(dict({'routeID':r.routeID,
                          'vehicleID':'unknown',
                          'minutes':str(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
Example #2
0
def getRoutes(refresh):

    if refresh is False:
        # do we already have it in the datastore?
        api = db.GqlQuery('select * from StaticAPIs where method = :1', 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':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 = 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 = simplejson.dumps(response_dict)
    
    static = StaticAPIs()
    static.method = utils.GETROUTES
    static.json = json
    static.put()

    return json
Example #3
0
def stopRequest(stopID, devStoreKey):

    logging.debug("Stop Request started")
    response_dict = {'status':'0',
                     'timestamp':utils.getLocalTimestamp()
                     }    
                     
    # unique key to track this request
    t = str(time.time()).split('.')[0]
    sid = stopID + str(devStoreKey) + t

    # got 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 = 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)
    
    return response_dict
Example #4
0
def routeRequest(routeID,destination):
    
    # @fixme memcache these results!
    if destination is not None:
        q = db.GqlQuery('select * from StopLocation where routeID = :1 and direction = :2 order by routeID', routeID, destination)
    else:
        q = db.GqlQuery('select * from StopLocation where routeID = :1 order by routeID', routeID)
    stops = q.fetch(200)
    if stops is None:
        response_dict = {'status':'0',
                         'info':'No stops found'
                        }
        return response_dict
        
    response_dict = {'status':'0',
                     'timestamp':utils.getLocalTimestamp(),
                     'routeID':routeID
                    }    
    
    stop_results = []
    for s in stops:
        if s.location is None:
            logging.error('API: ERROR, no location!?')
        else:
            logging.debug('API: latitude is %s' % s.location.lat)
            
        stop_results.append(dict({'stopID':s.stopID,
                          'intersection':s.intersection,
                          'latitude':'0.0' if s.location is None else s.location.lat,
                          'longitude':'0.0' if s.location is None else s.location.lon,
                          'destination':s.direction,                          
                          }))
    
    # add the populated stop details to the response
    response_dict.update({'stops':stop_results})
        
    return response_dict