Exemplo n.º 1
0
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')
Exemplo n.º 2
0
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'] }
    
    chosen_location = pause( "Pick the correct location:" )
    update_location = lookup_results[int( chosen_location )]
elif 1 == len(lookup_results):
    update_location = lookup_results[0]
else:
    print "Fire Eagle couldn't find anywhere matching that string, sorry."
    exit()

# Perform the update
fe.update( access_token, woeid=update_location['woeid'] )
Exemplo n.º 3
0
    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']}

    chosen_location = pause("Pick the correct location:")
    update_location = lookup_results[int(chosen_location)]
elif 1 == len(lookup_results):
    update_location = lookup_results[0]
else:
    print "Fire Eagle couldn't find anywhere matching that string, sorry."
    exit()

# Perform the update
fe.update(access_token, woeid=update_location['woeid'])