How to exclude retweets from my search query results Options

自作多情 提交于 2019-12-06 10:31:37

问题


I'm trying to search tweets using search method in twitter4j. My code is as follows,

    public List<Tweet> searchTweets(Query searchQuery) { 
        QueryResult queryResult = twitter.search(searchQuery); 

        return queryResult != null ? queryResult.getTweets() : new 
             ArrayList<Tweet>(0); 
    } 

How do I exclude retweets from my search query results


回答1:


I would rather comment on this but I can't yet so I'll post this as an answer. You need to edit the Query by appending parameters to it. The source code for Query.java can be found in the examples folder in the twitter4j folder.

public Query(String query) {
    this.query = query;
}

Check out Twitter search (atom) API - exclude retweets. The search basis is different but the concept is the same, which is appending +exclude:retweets to the end of your string query. Try it out and do let me know if it works!




回答2:


I was looking for how to exclude the replies on the query search, so I found this topic. To exclude retweets, this worked well for me:

Query query = new Query("from:"+twitterAccount + " +exclude:retweets");



回答3:


Query query = new Query(yourQuery + " -filter:retweets");

source

PS. I don't know the difference betweeen +exclude:retweets and -filter:retweets; both seem to do the job similarly.




回答4:


I used a different approach than above. See my code below:

List<Status> tweets = result.getTweets();
for (Status tweet : tweets )
{
      if( !tweet.isRetweet() )
      // processing .... 
} 

It works for me. However, there might be efficiency differences between these approaches.




回答5:


You can use set in the configuration builder the option setIncludeRTsEnabled to false, so is going to remove the retweets.

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setIncludeRTsEnabled(false);
Twitter twitter = new TwitterFactory(builder.build()).getInstance();


来源:https://stackoverflow.com/questions/7975866/how-to-exclude-retweets-from-my-search-query-results-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!