Пример #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')
Пример #2
0
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']
    for location in user_hierarchy
]
print "\n".join(user_hierarchy_names)
Пример #3
0
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()
Пример #4
0
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']
        for location in user_hierarchy
    ]
print "\n".join( user_hierarchy_names )
Пример #5
0
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 )