24

How to Exclude retweets and replies in a search api?

I am trying to fetch the feeds from twitter using search api, in the result I am getting replies and retweets also.

So I want to exclude replies and retweets.

How to do it anybody help me.

This is my url:

https://api.twitter.com/1.1/search/tweets.json?q=from:rioferdy5&count=20&result_type=recent

Mo Zaatar
  • 925
  • 8
  • 12
rakesh r
  • 465
  • 2
  • 4
  • 12

11 Answers11

62

I beleive the above is incorrect, you can use filters in the search API but the documentation is very poor (non-existent?).

Your query would become:

?q=from:rioferdy AND -filter:retweets AND -filter:replies&count=20&result_type=recent

More tips for filtering were obtained here: How to Master Twitter Search: Basic Boolean Operators and Filters

Paul Thomas
  • 2,756
  • 2
  • 16
  • 28
16

Old post, but people might still stumble upon it.

Most query operators are documented here: https://dev.twitter.com/rest/public/search

But for the search/tweets method, you can also specify exclude:replies and/or exclude:retweets to filter out replies and retweets from the result.

Just test it in the API Console Tool and see for yourself.

Bonus: Another undocumented query operator is filter:verified to get tweets from verified users.

Example query: cats filter:vine filter:verified exclude:replies exclude:retweets

phuc77
  • 6,717
  • 2
  • 15
  • 11
  • is any of this still valid? i can't get the excludes: retweets to work at all – John Rogerson Jan 25 '18 at 02:27
  • 1
    Since the introduction of the premium API, they have changed their operators it seems: Standard search operators: https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators Premium search operators: https://developer.twitter.com/en/docs/tweets/search/guides/premium-operators – phuc77 Jan 30 '18 at 10:16
  • Had some time to test a bit more and seems to be working for me using the https://api.twitter.com/1.1/search/tweets.json end point. -filter:replies and -filter:retweets also works. – phuc77 Feb 06 '18 at 17:21
  • Fast forward to today, I can confirm that `exclude:retweets` still works, but it will exclude tweets containing has tag `#RT` as well. – Viktor Brešan Mar 28 '22 at 13:06
15

In the new Search Tweets API, including the following parameters will remove different flavors of retweets:
-is:retweet Excludes retweets
-is:quote Excludes quote tweets
-is:reply Excludes replies

Please see the API documentation here: Search Tweets - Build a Query

Only Mike Chen's response (which oddly had 0 upvotes until I upvoted it) is correct. The rest of the answers here are out of date due to the launch of the Twitter API v2. I would comment directly on Mike's answer, but I don't have enough reputation.

K. Thorspear
  • 473
  • 3
  • 12
7

Very late reply, like everyone else but I feel the second answer here by Paul should be the "correct" one. I wish twitter would document this better, or make it more well known but there are a TON of search filters you can do, even with their standard API in 2018.

https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators

Here's a rather comphrensive list of examples :) and retweets is somewhere in the middle.

-filter:retweets
Kehran
  • 83
  • 2
  • 6
6

yes, you can exclude the retweets during search API by adding -RT in the search string (q). Ex: search?q="#demo -RT"

Mert S. Kaplan
  • 1,045
  • 11
  • 16
6

This is allowed as documented in the official documentation

puppy -filter:retweets  containing “puppy”, filtering out retweets

https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators.html

Michael C
  • 99
  • 1
  • 2
5

Found this while searching how to do this in the new v2 API, and it's now puppy -is:retweet -is:reply (no longer need the ANDs either)

https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query#list

Mike Chen
  • 88
  • 1
  • 5
  • Now that I have enough reputation -- at the time of this comment, this is the only correct answer besides the one I provided, which is the same with a little more detail on the parameters to use. All others are out of date post the release of the v2 API. – K. Thorspear May 01 '22 at 18:20
3

According to the official documentation

Pass the following parameter exclude_replies=true

ex0b1t
  • 1,292
  • 1
  • 17
  • 25
2

Sorry I'm late to the party here. I agree with Hitesh in that they do not provide a way to exclude retweets natively, but every tweet that is a retweet has a retweet object in the json returned. So you could loop through your tweets and exclude any that have a retweeted_status typeof 'object' (meaning they are a retweet from someone else) or keep those that have a typeof 'undefined' (meaning they are original). The issue with retweet_count=0 is that someone like @pattonoswalt will have retweets on all of his tweets. So the count will never be zero even though they are all originals.

You could use something like so in a loop:

if(typeof tweets[i].retweeted_status === 'object') {tweets.splice(i,1);}

or

if(typeof tweets[i].retweeted_status !== 'undefined') {tweets.splice(i,1);}

JeremyS
  • 427
  • 2
  • 7
1

There is no direct way to exclude retweets and replies from the api. However, you can filter out the results you have got.

For replies, you can check if the in_reply_to_status_id field you get from api is null, that means its not a reply else if it contains a id, then its a reply.

For retweet, if you want posts that have not been retweeted ever, you can check for retweet_count = 0 or if you want posts that have not been retweeted by your authenticated user, you can check for retweeted = false

Hitesh
  • 3,449
  • 8
  • 39
  • 57
0

Just use nitter.net it allows you to exclude things from the search results (via advanced search options on the right end of its search bar), and it even provides its own RSS feed. On top it expands these t.co short URLs and replaces the youtube URL with the invidio.us URL

In the end you may use your RSS feed as a trigger for other web-applets through the self-hostable interface called Huginn

gloschtla
  • 41
  • 3
  • 10