Search code examples
pythonjsontwittertweepytwython

python - Twitter API - get tweets from a list of ids


I am trying to use Twython (or tweepy if you think its possible) to generate the text (and other info) from a list of tweet ids I have.

I can pull it off with one id, I am not sure of a) how to do it with multiple and b) how to pull out other info such as user description, location etc.

Here is my code

list_of_tweets = ["928549518659989505", "928635241677324288" ]

twitter = Twython(consumer_key, consumer_secret,access_token, access_token_secret)

tweet = twitter.show_status(id=__notsure__)
print(tweet['text'])

I also found another similar question (for user names, but its simple to get the text) which provides the following solution:

from twython import Twython

#Auth steps
twitter = Twython(AUTH)
samplefollower = ["259784090","136953436","1219150098"]

#We create a comma separated string from the previous list
comma_separated_string = ",".join(samplefollower)

#Now, we query twitter with the comma separated list
output = twitter.lookup_user(user_id=comma_separated_string)
username_list=[]

#Loop through the results
for user in output:
    username_list.append(user['screen_name'])
    print(username_list)

Which however gives me the error:

TwythonError: Twitter API returned a 404 (Not Found), No user matches for specified terms.

Solution

  • If you are not wedded to twython or tweepy, here is how you can do this using another library - TwitterAPI

    from TwitterAPI import TwitterAPI
    
    api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEY_KEY, ACCESS_TOKEN_SECRE)
    
    ids = "259784090","136953436","1219150098"
    
    for item in api.request('users/lookup', {'user_id':ids}):
            if 'screen_name' in item:
                    print(item['screen_name'])