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"))
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() all_friends = [] for page in user.paginate_friends(friends_ids, 100): friend_objs = user.lookup_friends(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 all_friends.append(friend) t1 = time.time() print "TIME TO PAGINATE AND LOOKUP FRIENDS", t1 - t0 print all_friends # get timelines for friends