예제 #1
0
 def get(self):
   user = users.get_current_user()
   if user:
     auth_token = self.request.get("oauth_token")
     if auth_token:
       credentials = constants.get_client().get_credentials(auth_token)
       old_userinfos = UserInfo.all().filter('user ='******'token'], secret = credentials['secret'], is_ready=False, is_authorized=True, last_checkin=0, last_updated=datetime.now(), color_scheme='fire', level_max=int(constants.level_const), checkin_count=0, venue_count=0)
       fetch_foursquare_data.update_user_info(userinfo)
       fetch_foursquare_data.fetch_and_store_checkins(userinfo, limit=10)
       taskqueue.add(url='/fetch_foursquare_data/all_for_user/%s' % userinfo.key())#, queue_name='initial-checkin-fetching')
       self.redirect("/")
     else:
       self.redirect(constants.get_client().get_authorization_url())
   else:
     self.redirect(users.create_login_url(self.request.uri))
def fetch_and_store_checkins(userinfo, limit=50):
  num_added = 0
  params = {'l':limit, 'sinceid':userinfo.last_checkin}

  try:
    response = constants.get_client().make_request("http://api.foursquare.com/v1/history.json",
                                            token = userinfo.token,
                                            secret = userinfo.secret,
                                            additional_params = params,
                                            protected = True)
  except DownloadError, err:
    logging.error("Checkins not fetched for %s with error %s" % (userinfo.user, err.args))
    #TODO i should maybe fail more gracefully here and try again?
    return num_added
def update_user_info(userinfo):
  response = constants.get_client().make_request("http://api.foursquare.com/v1/user.json", token = userinfo.token, secret = userinfo.secret)
  current_info = json.loads(response.content)
  if 'user' in current_info:
    userinfo.real_name = current_info['user']['firstname']
    if 'photo' in current_info['user'] and not current_info['user']['photo'] == '' :
      userinfo.photo_url = current_info['user']['photo']
    else:
      userinfo.photo_url = constants.default_photo
    if 'checkin' in current_info['user'] and 'venue' in current_info['user']['checkin']:
      userinfo.citylat = current_info['user']['checkin']['venue']['geolat']
      userinfo.citylng = current_info['user']['checkin']['venue']['geolong']
    else:
      userinfo.citylat = constants.default_lat
      userinfo.citylng = constants.default_lng
    userinfo.put()
  else:
    logging.error('no "user" key in json: %s' % current_info)