Search code examples
pythonpython-twitter

Get usernames for a retweet?


How can I view the usernames for each retweet from my timeline using Python-Twitter? Here is what I have so far which brings back the timeline without the re-tweet usernames:

import twitter
api = twitter.Api(xxxxxx)
statuses = api.GetUserTimeline('username', include_rts=True)
for tweet in statuses:
    print tweet

The method that I think you would use is GetTweets which requires a statusID, I am not sure how you would pass the status ID from the timeline call to the GetRetweets call?

Thanks in advance!


Solution

  • Try this:

    import twitter
    
    t = twitter.Api(...)
    
    statuses = t.GetUserTimeline('username', include_rts=True)
    
    for tweet in statuses:
        retweets = t.GetRetweets(tweet.GetId())
        users = [retweet.GetUser().GetScreenName() for retweet in retweets]
        print tweet.GetId(), users
    

    Hope that helps.