Skip to content

Method: Follows lookup

Matthew R. DeVerna edited this page Mar 13, 2021 · 2 revisions

Follows lookup endpoints allow querying the relationship between users by pulling who an account follows (following) as well as who follows them (followers).

Pull who follows a specific user_id - get_followers()

import osometweet

# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)

# Call the function to get "@jack"'s 100 most recent followers
# The endpoint only supports user id, we pass the id of @jack to the method
response = ot.get_followers('12')

which returns a list of 100 user objects representing the 100 most recent people who follow the account with user_id '12'.

Pull who a user_id follows - get_following()

import osometweet

# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)

# Call the function to get "jack"'s 100 most recent followers
response = ot.get_following('12')

which returns a list of 100 user objects represent the 100 most recent people who user_id '12' followed.

Specifying the result numbers

100 users are returned by default, however, you can request up to 1000 users per query - simply include the parameter max_results = 1000. Here is an example:

# Call the function to get "jack"'s 1000 most recent followers
response = ot.get_followers('12', max_results = 1000)

Pagination

If you need more than 1,000 results, you will have to use pagination. Here is an example for get 2,000 followers of @jack 's

# Call the function to get "jack"'s 1,000 most recent followers
response = ot.get_followers('12', max_results = 1000)

# Call the function again to get another 1,000 followers:
response_2 = ot.get_followers('12', pagination_token=response['meta']['next_token'], max_results = 1000)

The same technique works for get_following() too. For more information, see the offical document.


Fields & expansions

V2 API, by default, only returns limited information. To fetch more, you will need to specify the fields and expansions parameters in the requests. OSoMeTweet contains several classes to handle them. See Method: Specifying fields and expansions for examples of how to work with them.