17
votes

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 excludes.

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

9

9 Answers

3
votes

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

59
votes

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

15
votes

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

6
votes

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
6
votes

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

4
votes

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

3
votes

According to the official documentation

Pass the following parameter exclude_replies=true

2
votes

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);}

0
votes

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