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
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