How to Exclude retweets and replies in a search api?
Asked Answered
L

11

25

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

Linet answered 14/1, 2015 at 11:33 Comment(0)
S
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

Sparry answered 14/1, 2015 at 12:7 Comment(1)
I don't have "retweeted" in my results, even if the authenticated account retweeted it.Frum
B
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

Bedfast answered 18/3, 2015 at 17:45 Comment(3)
I believe this gets you only replies. remove - and use "filter:replies" to exclude repliesAmbassador
Tks for your answer.This is exactly.Alisha
Though it is the most upvoted, this answer is out of date post the release of the v2 API.Flimflam
B
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

Boscage answered 8/3, 2016 at 17:46 Comment(4)
is any of this still valid? i can't get the excludes: retweets to work at allSelftaught
Since the introduction of the premium API, they have changed their operators it seems: Standard search operators: developer.twitter.com/en/docs/tweets/search/guides/… Premium search operators: developer.twitter.com/en/docs/tweets/search/guides/…Boscage
Had some time to test a bit more and seems to be working for me using the api.twitter.com/1.1/search/tweets.json end point. -filter:replies and -filter:retweets also works.Boscage
Fast forward to today, I can confirm that exclude:retweets still works, but it will exclude tweets containing has tag #RT as well.Lascivious
F
16

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.

Flimflam answered 9/2, 2022 at 20:2 Comment(3)
Sorry, I know it's a nuisance sometimes, but the only option is to wait until you can comment.Joscelin
@GertArnold 💯%!Flimflam
I wrote a query using all three of these filter params but I am still getting duplicate tweets but with different author_ids.Machuca
J
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
Jannjanna answered 31/7, 2018 at 8:56 Comment(0)
A
6

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

Antlia answered 11/8, 2017 at 11:44 Comment(3)
I think you have an error in your example -RT or _RT?Filth
@Canis, its -RTAntlia
This one fixed it for me!Polygyny
P
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

Prosthodontics answered 4/1, 2019 at 23:35 Comment(0)
R
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

Recently answered 13/11, 2021 at 14:55 Comment(1)
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.Flimflam
D
3

According to the official documentation

Pass the following parameter exclude_replies=true

Deeannadeeanne answered 6/8, 2015 at 20:49 Comment(1)
This is for a user's timeline, not the search API.Shrine
S
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);}

Solitary answered 22/9, 2015 at 17:49 Comment(0)
S
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

Sparry answered 14/1, 2015 at 12:7 Comment(1)
I don't have "retweeted" in my results, even if the authenticated account retweeted it.Frum
K
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

Kindly answered 30/1, 2020 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.