#create connection to twitter api auth_api = tf.twitter_connect(consumer_key, consumer_secret, access_token, access_token_secret) #get Hadley's account id hadley = tf.get_user_object("@hadleywickham", auth_api) hadleyid = hadley[0] #%% find all of Hadley's followers level1 = tf.get_follower_ids(hadleyid, auth_api) #returns 74,820 followers #filter out followers who follow >100 or <25 accounts followers = [] #split into batches of 100 for Twitter API requests = [level1[i:i + 100] for i in range(0, len(level1), 100)] for request in requests: users = tf.get_users(request, auth_api) for user in users: if user[3] <= 100 and user[3] >= 25: followers.append(user[0]) # returns 13,416 followers #randomly select 5000 followers to explore network rand_followers = random.sample(followers, 5000) #%% for each of Hadley's followers, find all accounts they follow # and tally up how many of Hadley's followers also follow each of these accounts level2 = defaultdict(int) for follower in rand_followers: try: for friend in tf.get_friend_ids(follower, auth_api): level2[friend] += 1 except: