def profile_index(): currentUser = users.get_current_user() userProfile = UserRepository.get(currentUser) if userProfile == None: userProfile = UserRepository.new(currentUser) logging.info('userprofile %s' % userProfile) if(userProfile.services): isLatitudeInstalled = ServiceRepository.get('latitude', userProfile) != None isFoursquareInstalled = ServiceRepository.get('foursquare', userProfile) != None if(isLatitudeInstalled == False and isFoursquareInstalled == False): return render_template('setup_step1.html', user=currentUser, logout_url=users.create_logout_url("/")); if(isLatitudeInstalled == False and isFoursquareInstalled == True): return render_template('setup_step1.html', user=currentUser, logout_url=users.create_logout_url("/")); if(isLatitudeInstalled == True and isFoursquareInstalled == False): return render_template('setup_step2.html', user=currentUser, logout_url=users.create_logout_url("/")); if(userProfile.isPaused == True): return render_template('setup_off.html', user=currentUser, logout_url=users.create_logout_url("/")); return render_template('setup_on.html', user=currentUser, logout_url=users.create_logout_url("/"));
def service_auth(service): userProfile = UserRepository.get(users.get_current_user()) if(ServiceRepository.get(service, userProfile) != None): return redirect(url_for('profile.profile_index')) callback_url = 'http://autocheckin.appspot.com/profile/setup/' + service + '/authorized' if(settings.isDebug): callback_url = 'http://localhost:8080/profile/setup/' + service + '/authorized' if service == 'latitude': consumer = oauth.Consumer(settings.latitude_key, settings.latitude_secret) request_token_url = settings.latitude_request_token_url authorize_base_url = settings.latitude_authorize_url session_key = 'oauth_latitude_request_secret' if service == 'foursquare': consumer = oauth.Consumer(settings.foursquare_key, settings.foursquare_secret) request_token_url = settings.foursquare_request_token_url authorize_base_url = settings.foursquare_authorize_url session_key = 'oauth_foursquare_request_secret' helper = oauth_helper.OAuthHelper(consumer) request_token = helper.get_request_token(request_token_url, callback_url) authorize_url = helper.get_authorize_url(request_token, authorize_base_url) session[session_key] = request_token.secret return redirect(authorize_url)
def blah(): userProfile = UserRepository.get(users.get_current_user()) service = ServiceRepository.get('latitude', userProfile) userProfile = UserRepository.get(users.get_current_user()) client = clients.latitude.LatitudeClient(service.access_token, service.access_secret) location = client.current_location() return json.dumps(location)
def oauth_authorized(service): if service == 'latitude': consumer = oauth.Consumer(settings.latitude_key, settings.latitude_secret) request_secret = session['oauth_latitude_request_secret'] access_token_url = settings.latitude_access_token_url if service == 'foursquare': consumer = oauth.Consumer(settings.foursquare_key, settings.foursquare_secret) request_secret = session['oauth_foursquare_request_secret'] access_token_url = settings.foursquare_access_token_url userProfile = UserRepository.get(users.get_current_user()) helper = oauth_helper.OAuthHelper(consumer) token = oauth.Token(request.args.get('oauth_token'), request_secret) verifier = request.args.get('oauth_verifier') access_token = helper.get_access_token(token, verifier, access_token_url) ServiceRepository.new(userProfile, service, access_token.key, access_token.secret) return redirect(url_for('profile.profile_index'))
def fake(): lng = request.args.get('lng') lat = request.args.get('lat') accuracy = request.args.get('a') logging.warning('Faking location: %s)' % accuracy) userProfile = UserRepository.get(users.get_current_user()) service = ServiceRepository.get('latitude', userProfile) userProfile = UserRepository.get(users.get_current_user()) client = clients.latitude.LatitudeClient(service.access_token, service.access_secret) location = client.update_location(lat, lng, accuracy) return json.dumps(location)
def getNearByVenues(user, location): foursquareService = ServiceRepository.get('foursquare', user) logging.info('Trying to fecth data from FourSquare (user-key%s)' % user.key) client = foursquare.FoursquareClient(foursquareService.access_token, foursquareService.access_secret) # Get nearby Venues venues = client.venues(location.latitude, location.longitude) venueList = [] if venues and not 'groups' in venues: logging.warning('Skipping, couldnt get near by venues for user (userkey: %s)' % userKey) return None # Merge all Venues for group in venues['groups']: venueList.extend(group['venues']) if(len(venueList) == 0): logging.warning('Skipping, No near by venues found for user (userkey: %s)' % userKey) return None # Get venue stats for venue in venueList: #venue['stats'] = client.venue(venue['id'])['venue']['stats'] venue['rank'] = 1 venue['score'] = 0 # Category filter # Ranking for venue in venueList: # if venue['stats']['beenhere']['me'] == True: # venue['rank'] += 2 venue['score'] = int(venue['rank']) * (int(location.accuracy) - int(venue['distance'])) # 7. Sorting def distance_compare(x, y): return y['score'] - x['score'] sortedVenues = sorted(venueList, cmp = distance_compare) return sortedVenues
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 ""
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 ""