Question

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!

Was it helpful?

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.

OTHER TIPS

I wanted to add to @alecxe's answer but with a comment I can't paste much code. In my application I found it useful to check if the tweet had retweets prior to getting those retweets. This saves a little when it comes to the API quota. I've modified @alecxe's code to fit my needs:

import twitter, json

t = twitter.Api(...)

statuses = t.GetUserTimeline('username', include_rts=True)

for tweet in statuses:
    json_tweet = json.loads(str(tweet))

    rts = 0
    try:
        rts = json_tweet['retweet_count']
    except:
        rts = 0

        if rts > 0:
            retweets = t.GetRetweets(tweet.GetId())
            users = [retweet.GetUser().GetScreenName() for retweet in retweets]
            print tweet.GetId(), users

Or you can try this too.

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
    print tweet.GetRetweets()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top