コード例 #1
0
def process_friend_batch(user, page, api):
    """
    Create User object for each friend in batches of 100.
    Return batch as list of friend objects.

    Parameters:
    -----------
    User object
    A page of 100 friend ids (Twitter rate limit for user/lookup is 100 ids)
    API object

    Output:
    -------
    A list of 100 friend objects.

    """
    batch = []
    friend_objs = user.lookup_friends(f_ids=page)
    for f in friend_objs:
        friend = User(api, user_id=f.id, central_user=user.central_user)
        friend.num_followers = f.followers_count
        friend.screen_name = f.screen_name
        batch.append(friend)
    return batch
コード例 #2
0
def display_friends(screen_name):
    """
    For a given user, select top 50 most influential friends (by number of
    followers) and send JSON back to the front end for visualization.

    Parameters:
    ----------
    A user's screen name.

    Output:
    ------
    JSON of friends' screen names, number of followers, and scores.

    TODO: Refactor this route into 2 separate routes, in order to be able to
    update the front end more frequently.
    TODO: Add the searched user to friendlist, to be displayed in d3.

    """
    api = connect_to_API()

    # initialize the searched user as user object
    user = User(api, user_id=screen_name, central_user=screen_name)

    # will contain list of friend objects
    friendlist = []

    # store friends, followers and scores as objects inside "children" list
    friend_scores = {"name": user.user_id, "children": []}

    # unpickle classifier and vectorizer used for scoring timelines
    with open(PATH_TO_CLASSIFIER, "rb") as f:
        classifier = pickle.load(f)

    with open(PATH_TO_VECTORIZER, "rb") as f:
        vectorizer = pickle.load(f)

    try:
        friends_ids = user.get_friends_ids()

        for page in user.paginate_friends(friends_ids, 100):
            friends = process_friend_batch(user, page, api)
            friendlist.extend(friends)
            print friendlist

        if len(friendlist) > MAX_NUM_FRIENDS:
            friendlist = get_top_influencers(friendlist, MAX_NUM_FRIENDS)

        for friend in friendlist:
            timeline = friend.get_timeline(MAX_NUM_TWEETS)
            friend.score = friend.score_user(timeline, vectorizer, classifier)

            friend_scores["children"].append({"name": friend.screen_name,
                                             "size": friend.num_followers,
                                             "score": friend.score})

        json_scores = json.dumps(friend_scores)
        return json_scores

    except tweepy.TweepError as e:
        error_message = handle_error(e)
        flash(error_message)
        return redirect(url_for("index"))
コード例 #3
0
    """
    # initialize tweepy api object with auth, OAuth
    TWITTER_API_KEY = os.environ.get("TWITTER_API_KEY")
    TWITTER_SECRET_KEY = os.environ.get("TWITTER_SECRET_KEY")
    TWITTER_ACCESS_TOKEN = os.environ.get("TWITTER_ACCESS_TOKEN")
    TWITTER_SECRET_TOKEN = os.environ.get("TWITTER_SECRET_TOKEN")

    auth = tweepy.OAuthHandler(TWITTER_API_KEY, TWITTER_SECRET_KEY, secure=True)
    auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_SECRET_TOKEN)
    api = tweepy.API(auth, cache=None, retry_count=2)

    return api


api = connect_to_API()
user = User(api, "bookstein", "bookstein")

# get timeline
t0 = time.time()
timeline = user.get_timeline(20)
t1 = time.time()
print "TIME TO GET TIMELINE: ", t1 - t0

# get friends
t0 = time.time()
friends_ids = user.get_friends_ids()
t1 = time.time()
print "TIME TO GET FRIENDS:", t1 - t0

# paginate and lookup friends
t0 = time.time()