5

I'm trying to exclude retweets and replies in my Twython search.

Here is my code:

from twython import Twython, TwythonError

app_key = "xxxx"
app_secret = "xxxx"
oauth_token = "xxxx"
oauth_token_secret = "xxxx"   

naughty_words = [" -RT"]
good_words = ["search phrase", "another search phrase"]
filter = " OR ".join(good_words)
blacklist = " -".join(naughty_words)
keywords = filter + blacklist

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) 
search_results = twitter.search(q=keywords, count=100)

The problem is that the -RT function isn't really working.

EDIT:

I've tried @forge suggestion, and while it does print the if tweets are not retweets or replies, when I incorporate them into the code below, the bot still finds tweets, retweets, quotes and replies.

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) query = 'beer OR wine AND -filter:retweets AND -filter:replies' 
response = twitter.search(q=query, count=100) 
statuses = response['statuses'] 
try: 
for tweet in statuses: 
try: 
twitter.retweet(id = tweet["id_str"]) 
except TwythonError as e: 
print e 
except TwythonError as e: 
print e

Any ideas? Is there a filter:quotes?

Forge
  • 6,538
  • 6
  • 44
  • 64
B. Chas
  • 51
  • 1
  • 6
  • 2
    i have tried including "-filter:retweets" and "-filter:replies" in my bad words field but not getting any luck. is it really this difficult just to have the api return just tweets and not replies quotes or retweets? – B. Chas Mar 14 '16 at 09:16
  • You can also use as the tutorial given by [Craig Addyman] - http://www.craigaddyman.com/mining-all-tweets-with-python/ – Dinesh Dec 19 '16 at 10:06

1 Answers1

11

The correct syntax is -filter:retweets.

If you would like to search on terms "search phrase" or "another search phrase" and exclude retweets, then the query should be:

query = "search_phrase OR another_search_phrase -filter:retweets"

To exclude replies as well, add -filter:replies like this:

query = "search_phrase OR another_search_phrase -filter:retweets AND -filter:replies"

This should be working, you can verify it by checking the status fields in_reply_to_status_id and retweeted_status:

  • Status is not a reply if in_reply_to_status_id is empty
  • Status is not a retweet if it doesn't have the field retweeted_status

With Twython:

import twython

twitter = twython.Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 

query = 'wine OR beer -filter:retweets AND -filter:replies' 
response = twitter.search(q=query, count=100)
statuses = response['statuses']
for status in statuses:
    print status['in_reply_to_status_id'], status.has_key('retweeted_status')

# Output should be (None, False) to any status
Forge
  • 6,538
  • 6
  • 44
  • 64
  • 1
    Beautiful! Also, if anyone wants the API doc for various filters: https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators – jtubre Mar 09 '20 at 23:11