-
Notifications
You must be signed in to change notification settings - Fork 3
Method: User lookup
User lookup endpoints return the account information. It supports two different types of queries:
- Query using user ID numbers
- Query using the account's
username
Note: A user can change their
username
but they can't change theirid
number. It is therefore recommended that you gather data usingid
numbers because they are more reliable.
We can query Twitter with up to 100 user IDs or usernames per query. Here's what it looks like to pull two user IDs with the .user_lookup_ids()
method, which requires we pass it a list
(or tuple
) of account user IDs.
import osometweet
# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)
# Set some test IDs (these are Twitter's own accounts)
ids2find = ["2244994945", "6253282"]
# Call the function with these ids
response = ot.user_lookup_ids(ids2find)
print(response["data"])
which returns a list of dictionaries, where each dictionary contains the requested information for an individual user.
[
{'id': '2244994945', 'name': 'Twitter Dev', 'username': 'TwitterDev'},
{'id': '6253282', 'name': 'Twitter API', 'username': 'TwitterAPI'}
]
The process for pulling account information using an account's user names is exactly the same as using user_id
s except we now use the user_lookup_usernames()
method and provide usernames. So, after initializing the OsomeTweet
with your Twitter tokens/keys, you can do the following:
# Set some test IDs (these are Twitter's own accounts)
usernames2find = ["TwitterDev", "TwitterAPI"]
# Call the function with these ids
response = ot.user_lookup_usernames(usernames2find)
print(response["data"])
which returns the same results as before.
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.