Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweepy check if a tweet is a retweet

I'm starting using Tweepy 3.6.0 with Python and I have some questions.

First, I want to get a list of tweets (with api.search method), but not retweets. I find something weird. Try to access to a tweet with his ID and the author_name. Automatically, it's redirect to the original tweet (different ID and author_name).

After some search I found people talking about a "retweeted_status" key. If the key exit, so it's a RT. But in my example below, there is no retweeted_status in my Tweet Object but the redirection to the original Tweet is here.

Did I understang something badly ?

Thanks

like image 543
Nathan Cheval Avatar asked Apr 26 '18 21:04

Nathan Cheval


1 Answers

You can chose to search for only retweets or exclude all retweets from your search query.

For searching for no retweets "-filter:retweets"

for tweet in tweepy.Cursor(api.search, q='github -filter:retweets',tweet_mode='extended').items(5):

For searching for only retweets "filter:retweets"

 for tweet in tweepy.Cursor(api.search, q='github filter:retweets',tweet_mode='extended').items(5):

Extra Information:

Whilst you can exclude retweets straight in the search query, it is also very easy to find if a tweet is a retweet because all retweets begin with "rt @UsernameOfAuthor". You can find if a tweet is a retweet by doing a basic if statement to see if the tweet begins with the rt.

first make a basic query and save the information into variables.

for tweet in tweepy.Cursor(api.search, q='github',tweet_mode='extended').items(5):
    # Defining Tweets Creators Name
    tweettext = str( tweet.full_text.lower().encode('ascii',errors='ignore')) #encoding to get rid of characters that may not be able to be displayed
    # Defining Tweets Id
    tweetid = tweet.id

Then print the info for demonstration purposes

    # printing the text of the tweet
    print('tweet text: '+str(tweettext))
    # printing the id of the tweet
    print('tweet id: '+str(tweetid))

Then there is the if statement to find if it is a retweet or not

# checking if the tweet is a retweet (this method is basic but it will work)
if tweettext.startswith("rt @") == True:
    print('This tweet is a retweet')
else:
    print('This tweet is not retweet')
like image 98
TravisPooley Avatar answered Oct 13 '22 14:10

TravisPooley