6

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

Nathan Cheval
  • 103
  • 1
  • 7

1 Answers1

11

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')
TravisPooley
  • 429
  • 3
  • 10
  • Thanks a lot for your answer, I'll try this out :) But the "-filter:retweets" seems to be a little buggy. I tried a little and a lot of times I didn't find some recent tweets :/ – Nathan Cheval May 02 '18 at 07:47
  • Twitters free API's are pretty unreliable, they don't offer a for archival view really. I added the if statement to check if its a retweet to my response too, give that a go too if the other way is not working well enough. :) – TravisPooley May 02 '18 at 08:18
  • 2
    that's true ahah. I find another way to do it : "isRT = hasattr(tweet, 'retweeted_status')" – Nathan Cheval May 02 '18 at 12:58
  • 2
    There is a range of ways to tell if a tweet is a retweet I gave the methods I prefer to use. Another way you can check is even just check if the tweet contains the property called "retweeted_status". There's many ways to skin a cat :) – TravisPooley May 04 '18 at 00:35