def summary(screen_name, r):
    t = login()
    #some wrapper around _getFriendsOrFollowersUsingFunc
    #that bind the first two arguments
    getFriendIds = functools.partial(tu._getFriendsOrFollowersUsingFunc,
                                     t.friends.ids,
                                     'friend_ids',
                                     t,
                                     r)
    getFollowerIds = functools.partial(tu._getFriendsOrFollowersUsingFunc,
                                       t.followers.ids,
                                       'follower_ids',
                                       t,
                                       r)

    #get the data
    getFriendIds(screen_name, limit=sys.maxint)
    getFollowerIds(screen_name, limit=sys.maxint)

    #using Redis to compute the numbers
    n_friends = r.scard(tu.getRedisIdByScreenName(screen_name, 'friend_ids'))
    n_followers = r.scard(tu.getRedisIdByScreenName(screen_name,
                                                    'follower_ids'))

    n_friends_diff_followers = r.sdiffstore(
        'temp', [tu.getRedisIdByScreenName(screen_name,
        'friend_ids'),
        tu.getRedisIdByScreenName(screen_name,
        'follower_ids')])
    r.delete('temp')

    n_followers_diff_friends = r.sdiffstore(
        'temp',
        [tu.getRedisIdByScreenName(screen_name,
        'follower_ids'),
        tu.getRedisIdByScreenName(screen_name,
        'friend_ids')])

    r.delete('temp')

    n_friends_inter_followers = r.sinterstore(
        'temp',
        [tu.getRedisIdByScreenName(screen_name,
        'follower_id'),
        tu.getRedisIdByScreenName(screen_name,
        'friend_id')])

    r.delete('temp')

    print '%s is following %s' % (screen_name, n_friends)
    print '%s is being followerd by %s' % \
        (screen_name, n_followers)
    print '%s of %s is not following back' % \
        (n_friends_diff_followers, n_friends)
    print '%s of %s is not following back by %s' % \
        (n_followers_diff_friends, n_followers, screen_name)
    print '%s has %s mutual friends ' % \
        (screen_name, n_friends_inter_followers)
def search_tweets(t, r, keywords, level="country"):
 #   redisId = tu.getRedisIdByScreenName(keywords, 'index')
    if level == "country":
        index_name = keywords
    else:
        index_name = "%s %s" % (level, keywords)
    redisTweetId = tu.getRedisIdByScreenName(index_name, 'search')
    params = {"q": keywords,
              "count": 100}
    search_results = tu.makeTwitterRequest(t.search.tweets, **params)
    tweets = search_results['statuses']

    for i in range(MAX_PAGES - 1):
        print "page %d" % (i + 1)
        next_results = search_results['search_metadata'].get('next_results')
        if next_results is None:
            break
        kwargs = dict([kv.split('=') for kv in next_results[1:].split('&')])
        max_id = str(long(kwargs['max_id']) - 1)
        kwargs['max_id'] = max_id
        search_results = tu.makeTwitterRequest(t.search.tweets,
                                               **kwargs)
        tweets += search_results['statuses']

        if len(search_results['statuses']) == 0:
            break
        print "Fetched %d tweets so far" % len(tweets)

    for t in tweets:
        r.sadd(redisTweetId, t)
    return tweets
def filter_already_download(friend_ids, r):
    try:
        keys = r.keys()
        new_friend_ids = [_id for _id in friend_ids if tu.getRedisIdByScreenName(_id, 'info.json') not in keys]
        exist_ids = [_id for _id in friend_ids if _id not in new_friend_ids]
    except:
        print "---", friend_ids
    return new_friend_ids, exist_ids
def filter_already_download(friend_ids, r):
    try:
        keys = r.keys()
        new_friend_ids = [
            _id for _id in friend_ids
            if tu.getRedisIdByScreenName(_id, 'info.json') not in keys
        ]
        exist_ids = [_id for _id in friend_ids if _id not in new_friend_ids]
    except:
        print "---", friend_ids
    return new_friend_ids, exist_ids
def summary(screen_name, r):
    t = login()
    # some wrapper around _getFriendsOrFollowersUsingFunc
    # that bind the first two arguments
    getFriendIds = functools.partial(tu._getFriendsOrFollowersUsingFunc, t.friends.ids, "friend_ids", t, r)
    getFollowerIds = functools.partial(tu._getFriendsOrFollowersUsingFunc, t.followers.ids, "follower_ids", t, r)

    # get the data
    getFriendIds(screen_name, limit=sys.maxint)
    getFollowerIds(screen_name, limit=sys.maxint)

    # using Redis to compute the numbers
    n_friends = r.scard(tu.getRedisIdByScreenName(screen_name, "friend_ids"))
    n_followers = r.scard(tu.getRedisIdByScreenName(screen_name, "follower_ids"))

    n_friends_diff_followers = r.sdiffstore(
        "temp",
        [tu.getRedisIdByScreenName(screen_name, "friend_ids"), tu.getRedisIdByScreenName(screen_name, "follower_ids")],
    )
    r.delete("temp")

    n_followers_diff_friends = r.sdiffstore(
        "temp",
        [tu.getRedisIdByScreenName(screen_name, "follower_ids"), tu.getRedisIdByScreenName(screen_name, "friend_ids")],
    )

    r.delete("temp")

    n_friends_inter_followers = r.sinterstore(
        "temp",
        [tu.getRedisIdByScreenName(screen_name, "follower_id"), tu.getRedisIdByScreenName(screen_name, "friend_id")],
    )

    r.delete("temp")

    print "%s is following %s" % (screen_name, n_friends)
    print "%s is being followerd by %s" % (screen_name, n_followers)
    print "%s of %s is not following back" % (n_friends_diff_followers, n_friends)
    print "%s of %s is not following back by %s" % (n_followers_diff_friends, n_followers, screen_name)
    print "%s has %s mutual friends " % (screen_name, n_friends_inter_followers)
def loadUserInfo(friend_ids, r):
    return [json.loads(r.get(tu.getRedisIdByScreenName(_id, 'info.json'))) for _id in friend_ids]
Example #7
0
def loadUserInfo(friend_ids, r):
    return [
        json.loads(r.get(tu.getRedisIdByScreenName(_id, 'info.json')))
        for _id in friend_ids
    ]
getFollowers = functools.partial(_getFriendsOrFollowersUsingFunc,
                                 t.followers.ids, 'follower_ids', t, r)

screen_name = SCREEN_NAME

# get the data

print >> sys.stderr, 'Getting friends for %s...' % (screen_name, )
getFriends(screen_name, limit=MAXINT)

print >> sys.stderr, 'Getting followers for %s...' % (screen_name, )
getFollowers(screen_name, limit=MAXINT)

# use redis to compute the numbers

n_friends = r.scard(getRedisIdByScreenName(screen_name, 'friend_ids'))

n_followers = r.scard(getRedisIdByScreenName(screen_name, 'follower_ids'))

n_friends_diff_followers = r.sdiffstore('temp',
                                        [getRedisIdByScreenName(screen_name,
                                        'friend_ids'),
                                        getRedisIdByScreenName(screen_name,
                                        'follower_ids')])
r.delete('temp')

n_followers_diff_friends = r.sdiffstore('temp',
                                        [getRedisIdByScreenName(screen_name,
                                        'follower_ids'),
                                        getRedisIdByScreenName(screen_name,
                                        'friend_ids')])