def syncworker(request): """ Okay, this is the real stuff. It's the worker process for the task queue. Gets a user name as a parameter. Calls Foursquare, Fire Eagle. Compares times of last check-in. If the Foursquare check-in is more recent, updates Fire Eagle. We catch all kinds of exceptions, so users with messed up OAuth permissions won't stop the worker. """ user = request.args.get('user') logging.warning('user is: %s' % str(user)) t = Token.gql( "WHERE user = :1", str(user) ).get() lat = 0 lon = 0 try: token = oauth.OAuthToken.from_string( t.fe_token ) fe = FireEagle( FE_CONSUMER_KEY, FE_CONSUMER_SECRET ) user = fe.user( token ) date_time = user[0]['location'][0]['located_at'] """ For some reasons that only Yahoo! knows, Fire Eagle logs its time and dates in local Californian time (including Daylight Saving Time), so we have to convert the thing to UTC. Crazy, huh? Share my frustration at http://blog.johl.io/post/393494632/what-time-is-it """ current_datetime = datetime.datetime.now(ustimezones.pacific()) # What time is it in Cali? fe_delta = str(current_datetime.replace(tzinfo=None) - date_time) credentials = foursquare.OAuthCredentials( FS_CONSUMER_KEY, FS_CONSUMER_SECRET ) fs = foursquare.Foursquare( credentials ) token = oauth.OAuthToken.from_string( t.fs_user_token ) credentials.set_access_token(token) history = fs.history() date_time = history['checkins'][0]['created'] lat = history['checkins'][0]['venue']['geolat'] lon = history['checkins'][0]['venue']['geolong'] current_datetime = datetime.datetime.now() date_time = datetime.datetime.fromtimestamp(time.mktime(rfc822.parsedate(date_time))) fs_delta = str(current_datetime.replace(tzinfo=None) - date_time) except: """ So yeah, something went wrong. Let's assume we should update Fire Eagle. """ logging.warning('Application error occurred with user %s', str(t.user)) fs_delta = 0 fe_delta = -1 if (fs_delta < fe_delta): try: fe.update( lat=lat, lon=lon, token=oauth.OAuthToken.from_string( t.fe_token ) ) except: """ Can't update Fire Eagle. Whatever. """ logging.warning('Application error occurred with user %s', str(t.user)) return render_to_response('firecheckin/index.html')
def fe_connect(request): """ Do the OAuth dance for Fire Eagle! """ CONSUMER_KEY = FE_CONSUMER_KEY CONSUMER_SECRET = FE_CONSUMER_SECRET CALLBACK_URL = FE_CALLBACK_URL fe = FireEagle( CONSUMER_KEY, CONSUMER_SECRET ) request_token = fe.request_token( oauth_callback=CALLBACK_URL ) if ( Token.gql( "WHERE user = :1", str(request.user) ).get() != None ): token = Token.gql( "WHERE user = :1", str(request.user) ).get() else: token = Token() token.user = str(request.user) token.fe_request_token = str(request_token) token.put() auth_url = fe.authorize( request_token, oauth_callback=CALLBACK_URL ) return redirect( auth_url )
def fe_request_token_ready(request): """ OAuth dance for Fire Eagle, callback URL """ query = request.environ['QUERY_STRING'] query_values = query.split("&") values = {} for qv in query_values: (key, value) = qv.split("=") values[key] = value oauth_verifier = values['oauth_verifier'] oauth_token = values['oauth_token'] CONSUMER_KEY = FE_CONSUMER_KEY CONSUMER_SECRET = FE_CONSUMER_SECRET fe = FireEagle( CONSUMER_KEY, CONSUMER_SECRET ) token = Token.gql( "WHERE user = :1", str(request.user) ).get() oauth_token = oauth.OAuthToken.from_string( token.fe_request_token ) fe = FireEagle( CONSUMER_KEY, CONSUMER_SECRET ) access_token = fe.access_token( oauth_verifier=oauth_verifier, token=oauth_token ) token.fe_token = str(access_token) token.put() return redirect( "/" )
#!/usr/bin/env python from os import path import pickle from sys import argv from fireeagle_api import FireEagle import settings # Die if we haven't authorized if not path.exists(settings.AUTH_FILE): print 'You need to authorize with Fire Eagle by running authorize.py.' exit() fe = FireEagle(settings.CONSUMER_KEY, settings.CONSUMER_SECRET) # Load the access token token_file = open(settings.AUTH_FILE, 'r') try: access_token = pickle.load(token_file) finally: token_file.close() # Get the hierarchy # NOTE: We're hacking past the user's token here to get straight to # the meat of location user_hierarchy = fe.user(access_token)[0]['location'] # Do some list jiggering to format the locations into a pretty state user_hierarchy_names = [ location['level_name'] + ': ' + location['name']
CONSUMER_SECRET='gQYRSEgCsEZSCDM2Pa6qr9aTVCmT80Mx' CONSUMER_KEY='3q3VkoxUjwP9' USER_TOKEN ='VhfSR2SzLkzM' USER_TOKEN_SECRET = '8gMH42725VgaLB4b3iahYLdxozVawLlM' from fireeagle_api import FireEagle from oauth import OAuthToken from threequarters.blog.models import Location import datetime fe = FireEagle(CONSUMER_KEY, CONSUMER_SECRET) token = OAuthToken(USER_TOKEN, USER_TOKEN_SECRET) locations = fe.user(token)[0]["location"] #from pprint import pprint #pprint(locations) lastloc = Location.objects.all()[0] for location in locations: if location["level"] == 3: # CITY name = location["name"] located_at = location["located_at"] #print "LOCATED_AT", located_at, "LAST", lastloc.located_at if name <> lastloc.name: print "ADDING LOCATION", name loc = Location(name=name, located_at=located_at) loc.save()
from os import path import pickle from fireeagle_api import FireEagle import settings # Die if we've already authorized if path.exists( settings.AUTH_FILE ): print "It looks like you already authorized Fire Eagle. Bye!" exit() def pause( prompt='hit to continue' ): return raw_input( '\n' + prompt + '\n' ) fe = FireEagle( settings.CONSUMER_KEY, settings.CONSUMER_SECRET ) ## Step 1 - Get a request token request_token = fe.request_token() ## Step 2 - Ask the user to authorize the application, using that request token auth_url = fe.authorize( request_token ) print auth_url pause( 'Please authorize the app at that URL' ) ## Step 3 - Convert the request token into an access token access_token = fe.access_token( request_token )
#!/usr/bin/env python from os import path import pickle from sys import argv from fireeagle_api import FireEagle import settings # Die if we haven't authorized if not path.exists( settings.AUTH_FILE ): print 'You need to authorize with Fire Eagle by running authorize.py.' exit() fe = FireEagle( settings.CONSUMER_KEY, settings.CONSUMER_SECRET ) # Load the access token token_file = open( settings.AUTH_FILE, 'r' ) try: access_token = pickle.load( token_file ) finally: token_file.close() # Get the hierarchy # NOTE: We're hacking past the user's token here to get straight to # the meat of location user_hierarchy = fe.user( access_token )[0]['location'] # Do some list jiggering to format the locations into a pretty state user_hierarchy_names = [ location['level_name'] + ': ' + location['name']
from os import path import pickle from sys import argv from fireeagle_api import FireEagle import settings # Die if we haven't authorized if not path.exists( settings.AUTH_FILE ): print 'You need to authorize with Fire Eagle by running authorize.py.' exit() def pause( prompt='hit to continue' ): return raw_input( '\n' + prompt + '\n' ) fe = FireEagle( settings.CONSUMER_KEY, settings.CONSUMER_SECRET ) # Load the access token token_file = open( settings.AUTH_FILE, 'r' ) try: access_token = pickle.load( token_file ) finally: token_file.close() # Disambiguate the input to a place_id that Fire Eagle can understand lookup_results = fe.lookup( access_token, q=argv[1] ) # If we have multiple locations, get the user to confirm. Otherwise, go ahead. if 1 < len(lookup_results): for i, location in enumerate( lookup_results ): print '%(#)d: %(name)s' % { '#': i, 'name': location['name'] }
from fireeagle_api import FireEagle fe = FireEagle( "MLxYXHDkLiA8", "r9qanNnpBvFX3mF3xmI98Hsjsg3zJ389" ) # dev application_token = fe.request_token() auth_url = fe.authorize( application_token ) print auth_url pause( 'Please authorize the app at that URL!' ) user_token = fe.access_token( application_token ) print fe.lookup( user_token, q='London, England' ) print fe.user( user_token )
from os import path import pickle from fireeagle_api import FireEagle import settings # Die if we've already authorized if path.exists( settings.AUTH_FILE ): print "It looks like you already authorized Fire Eagle. Bye!" exit() def pause( prompt='hit to continue' ): return raw_input( '\n' + prompt + '\n' ) fe = FireEagle( settings.CONSUMER_KEY, settings.CONSUMER_SECRET ) ## Step 1 - Get a request token request_token = fe.request_token() # Alternately, include a callback url when getting a request token # request_token = fe.request_token( oauth_callback="http://example.com/cb") ## Step 2 - Ask the user to authorize the application, using that request token auth_url = fe.authorize( request_token ) print 'Please authorize this application:' print auth_url oauth_verifier = pause( 'Please enter the verification code:' ) ## Step 3 - Convert the request token into an access token, using the verification code that was provided
from sys import argv from fireeagle_api import FireEagle import settings # Die if we haven't authorized if not path.exists(settings.AUTH_FILE): print 'You need to authorize with Fire Eagle by running authorize.py.' exit() def pause(prompt='hit to continue'): return raw_input('\n' + prompt + '\n') fe = FireEagle(settings.CONSUMER_KEY, settings.CONSUMER_SECRET) # Load the access token token_file = open(settings.AUTH_FILE, 'r') try: access_token = pickle.load(token_file) finally: token_file.close() # Disambiguate the input to a place_id that Fire Eagle can understand lookup_results = fe.lookup(access_token, q=argv[1]) # If we have multiple locations, get the user to confirm. Otherwise, go ahead. if 1 < len(lookup_results): for i, location in enumerate(lookup_results): print '%(#)d: %(name)s' % {'#': i, 'name': location['name']}