Ejemplo n.º 1
0
def fs():
    userProfile = UserRepository.get(users.get_current_user()) 
    currentLocation = LocationRepository.getLatestForUser(userProfile)
    # 5. Get Near by venues            
    venues = VenueRepository.getNearByVenues(userProfile, currentLocation)

    return json.dumps(venues)
Ejemplo n.º 2
0
def checkin_dummy():
    userProfile = UserRepository.get(users.get_current_user())
    userKey = userProfile.key()

    currentLocation = LocationRepository.getLatestForUser(userProfile)
     
    # 5. Add Foursquare task
    task = taskqueue.Task(url='/task/checkin', params={ 'userKey': userKey, 'locationKey' : currentLocation.key() }) 
    task.add('checkin')
    
    return "Dummy check in task"
Ejemplo n.º 3
0
def profile_test():
    currentUser = users.get_current_user()
    userProfile = UserRepository.get(users.get_current_user()) 
    logging.error('userProfile %s' % userProfile.services.fetch(1))
    
    currentLocation = LocationRepository.getLatestForUser(userProfile)
    timeDelta = datetime.datetime.utcnow() - currentLocation.importDate
    
    logging.info('Skipping, Location changed, but user hasn been there for time limit (timeDelta: %s)' % timeDelta) 
    
    if timeDelta < datetime.timedelta(minutes=5):
         logging.info('6min (userkey: %s)' % timeDelta) 
         return ""
         
    return 'none'
Ejemplo n.º 4
0
    def post(self):
        # extract the parameters for this task   
        userKey = self.request.get('userKey')
        locationKey = self.request.get('locationKey')
        
        if(userKey == None):
            logging.error('Aborting, didnt receive a userKey')
            return ""
        if(locationKey == None):
            logging.error('Aborting, didnt receive a locationKey')
            return ""
                
        logging.info('started handling checkin-task with user-key: %s' % userKey)

        # 1. get User
        userProfile = UserRepository.getByKey(userKey)
        if(userProfile == None):
            logging.error('Aborting. Didnt find the user (user-key: %s)' % userKey)
            return ""
        
        # 2. get Location
        location = LocationRepository.getByKey(locationKey)
        if(location == None):
            logging.error('Aborting. Didnt find location for user (user-key: %s, location : %s)', userKey, location)
            return ""
        
        # 3. foursquare client    
        foursquareService = ServiceRepository.get('foursquare', userProfile)
        if(foursquareService == None):
            logging.warning('Skipping. User didnt have a foursquare-service installed (user-key: %s)' % userKey) 
            return ""

        logging.info('Trying to fecth data from FourSquare (user-key%s)' % userKey)    
        client = foursquare.FoursquareClient(foursquareService.access_token, foursquareService.access_secret) 
        
        # 4. Get history
        logging.info('Trying to get history from FourSquare (user-key%s)' % userKey) 
        history = client.history(1)
        
        if(history and 'unauthorized' in history):
            logging.warning('Skipping, unauthorized from foursquare (userkey: %s)' % userKey)
            logging.warning('Removing service, since its unauthorized (userkey: %s, serviceKey: %s)', userKey, foursquareService.key())
            ServiceRepository.delete(foursquareService.key())
            return ""
            
            
        if history and not 'checkins' in history:
            logging.warning('Skipping, couldnt get history for user (userkey: %s)' % userKey)
            lastVenueId = 0
        else:
            logging.info('Got history for user (userkey: %s, history: %s)', userKey, history)
            lastVenueId = history['checkins'][0]['venue']['id']
            
        logging.info('Foursquare lastVenueId: %s' % lastVenueId)

        # 5. Get Near by venues        
        venues = VenueRepository.getNearByVenues(userProfile, location)
        
        if(venues == None):
            return ""
            
        theVenue = venues[0]    
        
        if(lastVenueId == theVenue['id']):
            logging.warning('Skipping, User already checked into this venue (userkey: %s, venueId: %s)', userKey, theVenue['id'])
            return ""
          
        # 8. Check in
        logging.info('Foursquare checking user into new venue (id= %s)' % theVenue['id']) 
        client.checkin(theVenue['id'], None, '(autocheckin.appspot.com)', location.latitude, location.longitude)
            
        return ""
Ejemplo n.º 5
0
    def post(self):
        # extract the parameters for this task   
        userKey = self.request.get('userKey')

        logging.info('started handling location-task with user-key: %s' % userKey)

        # 1. get User
        userProfile = UserRepository.getByKey(userKey)
        if(userProfile == None):
            logging.warning('Skipping. Didnt find the user (user-key: %s)' % userKey)
            return ""
            
        logging.info('User Found for with user-key: %s' % userKey)
        # 2. get current location
        currentLocation = LocationRepository.getLatestForUser(userProfile)
        latitudeService = ServiceRepository.get('latitude', userProfile)
        
        if(latitudeService == None):
            logging.warning('Skipping. User didnt have a latitude-service installed (user-key: %s)' % userKey)  
            return ""
            
        # 3. get Location from Latitude
        client = latitude.LatitudeClient(latitudeService.access_token, latitudeService.access_secret)
        latitudeLocation = client.current_location()

        # Check if authorized
        if(latitudeLocation and 'error' in latitudeLocation and latitudeLocation['error']['message'] == 'Unknown authorization header'):
            logging.warning('Skipping, unauthorized from latitude (userkey: %s)' % userKey)
            logging.warning('Removing service, since its unauthorized (userkey: %s, serviceKey: %s)', userKey, latitudeService.key())
            ServiceRepository.delete(latitudeService.key())
            return ""
            
        if not latitudeLocation or not 'data' in latitudeLocation:
             logging.warning('Aborting, couldnt get location form Google Latitude (userkey: %s, location: %s)', userKey, latitudeLocation)   
             return ""
             
        logging.info('Location received Google Latitude (userkey: %s, location: %s) ', userKey, latitudeLocation)  
        location = latitudeLocation['data']       

        if not location or not 'latitude' in location:
            logging.warning('Aborting, Google Latitude did not return a valid location (userkey: %s, location: %s)', userKey, location)
            return ""
            
        #userLocationAccuracyThreshold = SettingsRepository.getValue(userProfile, 'location-accuracy-threshold')
        #if(userLocationAccuracyThreshold == None):
        #    userLocationAccuracyThreshold = 100
        #else:
        #    userLocationAccuracyThreshold = int(userLocationAccuracyThreshold)
        userLocationAccuracyThreshold = 80
                    
        if(location['accuracy'] > userLocationAccuracyThreshold):
            logging.warning('Skipping, Accuracy for location isnt good enough (more than %s meters) (userkey: %s, location: %s)', userLocationAccuracyThreshold, userKey, location)
            return ""     
             
        # 4. Has the location changed?
        if(currentLocation != None and currentLocation.latitude == location['latitude'] and currentLocation.longitude == location['longitude']):            
            timeDelta = datetime.datetime.utcnow() - currentLocation.importDate
            
            userLocationTimeThreshold = SettingsRepository.getValue(userProfile, 'location-time-threshold')
            if(userLocationTimeThreshold == None):
                userLocationTimeThreshold = 10
            else:
                userLocationTimeThreshold = int(userLocationTimeThreshold)
                                
            if timeDelta < datetime.timedelta(minutes=userLocationTimeThreshold):
                logging.warning('Skipping, Location changed, but user hasn been there for %s minutes (userkey: %s)', userLocationTimeThreshold, userKey) 
                return ""
            
            userLocationDistanceThreshold = SettingsRepository.getValue(userProfile, 'location-distance-threshold')
            if(userLocationDistanceThreshold == None):
                userLocationDistanceThreshold = 10      
            else:
                userLocationDistanceThreshold = int(userLocationDistanceThreshold)     
                                      
            #distance = geocode_distance( (currentLocation.latitude, currentLocation.longitude), (location['latitude'], location['longitude']) )
            
            #if( (distance) < userLocationDistanceThreshold):
            #    logging.warning('Skipping, Location changed %s meters, but not more than the location treshhold (%s meters) (userkey: %s)', distance, userLocationDistanceThreshold, userKey) 
            #    return ""
                 
            logging.info('Continue, user hasn been there for time limit (userkey: %s)' % userKey) 
            # 5. Add Foursquare task
            logging.info('Adding task "/task/checkin" to checkin-queue. (userkey: %s, locationKey: %s)', userKey, currentLocation.key())
            task = taskqueue.Task(url='/task/checkin', params={ 'userKey': userKey, 'locationKey' : currentLocation.key() })   
            task.add('checkin')  
        elif currentLocation == None:
            # 5. Store new Location  
            logging.info('Storing location for the first time (userkey: %s, location: %s) ', userKey, location)
            LocationRepository.new(location['latitude'], location['longitude'], location['accuracy'], latitudeService, userProfile)            
        else:    
            # 5. Store new Location  
            logging.info('Storing new location (userkey: %s, location: %s) ', userKey, location)
            LocationRepository.new(location['latitude'], location['longitude'], location['accuracy'], latitudeService, userProfile)
 
        return ""