1 # Make a "famous ratio" for a given user which I'll define as 'number
2 # of followers a person has divided by number of people they
3 # follow. Try out @makoshark, and @pontifex (the Pope). Who is higher?
5 # This works for all users in my follower list.
9 from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
11 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
12 auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
14 api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
16 user = api.get_user("makoshark")
18 # I found the list of functions in Tweepy here:
19 # https://tweepy.readthedocs.org/en/v3.2.0/api.html
21 # I found the idea of how to the user the Cursor here:
22 # https://tweepy.readthedocs.org/en/v3.2.0/cursor_tutorial.html
25 for page in tweepy.Cursor(api.followers_ids, screen_name="makoshark").pages():
27 follower_ids.append(follower)
30 # the answer is using the api.lookup_users() code. unfortunately, this
31 # seems to only work with 100 users at a time. the following code makes that
36 for follower in follower_ids:
37 tmp_ids.append(follower)
40 # if we've hit 100, we grab data and then reset things and keep going
42 tmp_users = api.lookup_users(user_ids=tmp_ids)
43 users = users + tmp_users
48 # run once more when we're done
49 tmp_users = api.lookup_users(user_ids=tmp_ids)
50 users = users + tmp_users
52 # print out the famous ratios for users
55 famous_ratios[user.screen_name] = user.followers_count / user.friends_count
57 for user in sorted(famous_ratios, key=famous_ratios.get, reverse=True):
58 print(user, famous_ratios[user])