Reputation: 2041
I have the code below, which works for querying the Twitter API for screen names, based on a list of known ID's. I want to go the other way around: knowing the screen names, and getting the ID's.
from twython import Twython
# Paste your codes here
app_key = '...'
app_secret ='...'
oauth_token = '...-...'
oauth_token_secret= '...'
# Create twitter thing to query
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
# What to look up (Twitter id:s)
ids = ["259784090","136953436","1219150098"]
# Create a comma separated string from the previous list
comma_separated_string = ",".join(ids)
# Query twitter with the comma separated list
output = twitter.lookup_user(user_id=comma_separated_string)
username_list=[]
# Loop through the results (Twitter screen names)
for user in output:
print user['screen_name']
Upvotes: 0
Views: 3027
Reputation: 14324
It's the same API call, but with different parameters.
The documentation for GET users/lookup says
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters
Assuming your above use of Twython is correct (I don't use it myself) you should be able to call...
# What to look up (Twitter screen_name:s)
ids = ["john","paul","george","ringo"]
# Create a comma separated string from the previous list
comma_separated_string = ",".join(ids)
# Query twitter with the comma separated list
output = twitter.lookup_user(screen_name=comma_separated_string)
username_list=[]
# Loop through the results (Twitter screen names)
for user in output:
print user["id_str"]
Upvotes: 3