Example #1
0
def daily_user_update_connections():
    app_access_token = facebook.get_app_access_token(settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET)
    graph = facebook.GraphAPI(app_access_token)
    #Get active users
    actives = User.objects.filter(type=User.ACTIVE)
    errors = 0
    
    for active in actives:
        try:
            _id = active._id
            friends = graph.get_connections(_id, "friends").get('data')
            logger.warning("daily_user_update_connections user_id:%s 2" % _id)
            social.ensure_exists(_id, active.network)
            logger.warning("daily_user_update_connections user_id:%s 3" % _id)
            new_friends = {f['id'] : f for f in friends}
            r = update_friends_async(_id, [friend['id'] for friend in friends])
            logger.warning("daily_user_update_connections user_id:%s 4" % _id)
            #Get the people who are on keen around me and figure out their mutual relations
            users = col_get(USER_, {'network' : active.network})
            logger.warning("daily_user_update_connections user_id:%s 5" % _id)
            user_ids = [u.get('_id') for u in users]
            #make sure the update_friends has finished

            dr = DummyRequest()
            dr.session = {}
            user = User.get_by_id(_id)  
            rms = social.get_relation_mutuals_cache_v2(dr, user, user_ids, force_reload=True, check_cache_only=False)
            logger.warning("daily_user_update_connections user_id:%s 6" % _id)

        #try to get friends then run check_friends alg
#        try:
#            friends = graph.get_connections(active._id, "friends").get('data')
##            print friends['data']
#            new_friends = {}
#            for friend in friends:
#                id_ = friend['id']
#                f = {'name' : friend.get('name', ''), 'id' : id_, 'city_id' : '', 'city_name' : ''}
#                new_friends[id_] = f
#            new_friend_ids = active.check_friends(new_friends)          
        except facebook.GraphAPIError:
            #Maybe the user has uninstalled us frorm fb
            errors += 1
    return "Completed daily user update connections for %s users, with %s facebook graph errors" % (str(len(actives)), str(errors))
Example #2
0
def update_user_connections(user_id, access_token):
    logger.warning('update_user_connections user_id:%s access_token:%s' % (user_id, access_token))
    graph = facebook.GraphAPI(access_token)
    try:
        user = User.get_by_id(user_id)
        friends = graph.get_connections("me", "friends").get('data')
        #We check straight away that the friend & network cards are there
        f_ids = [friend['id'] for friend in friends]
        check_friends.apply_async((user_id, friends), countdown=10)
#        if _id in settings.GOD_IDS:
        #Get neo up to date
        
        logger.warning("update_user_connections user_id:%s 2" % user_id)
        b=social.ensure_exists(user_id, user.get('network'))
        logger.warning("update_user_connections user_id:%s 3" % user_id)
        new_friends = {f['id'] : f for f in friends}
        r = update_friends_async(user_id, [friend['id'] for friend in friends])
        logger.warning("update_user_connections user_id:%s 4" % user_id)
        #Get the people who are on keen around me and figure out their mutual relations
        users = col_get(USER_, {'network' : user.get('network')})
        logger.warning("update_user_connections user_id:%s 5" % user_id)
        user_ids = [u.get('_id') for u in users]
        #make sure the update_friends has finished
        dr = DummyRequest()
        dr.session = {}
        rms = social.get_relation_mutuals_cache_v2(dr, user, user_ids, force_reload=True, check_cache_only=False)
        logger.warning("update_user_connections user_id:%s 6" % user_id)
#        set_friends_cache(request, new_friends)
        logger.warning("update_user_connections user_id:%s 7" % user_id)
#        request.session['fb_friends'] = new_friends
        return {'result' : True, 'data' : rms}
    except facebook.GraphAPIError, e:
        logger.warning("update_user_connections _id:%s ERROR:facebook:%s" % (user_id, str(e)))
        response_data = {
            'result' : False,
            'data'   : 'Facebook friend request failed'
        }