Pawel Klimek Pawel Klimek - 2 months ago 32
Java Question

Twitter4j: show all tweets with searching word on specific user account

I want to find all the tweets where there is a specific word on specific user account.

String searchWord = "cat";
String userAccount = "....";

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxx")
.setOAuthConsumerSecret("xxxxx")
.setOAuthAccessToken("xxxxx")
.setOAuthAccessTokenSecret("xxxxx");

TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();


This method give me all tweet of account in
String userAccount
:

List<Status> statusList = twitter.getUserTimeline(userAccount);
for (Status status : statusList) {
System.out.println(status.getUser().getName() + " : " + status.getText());
}


This method give me all tweets with include searching word, but random users:

Query query = new Query(searchWord);
QueryResult result = twitter.search(query);
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
}


How to join this two method to find in account
userAccount
all tweets with word in
searchWord
.

Answer

If you want to get all the tweets with an specific word for an specific user you should use the first method and show only the tweets with the word that you want. In other words:

String yourstring = "your text";
List<Status> statusList = twitter.getUserTimeline(userAccount);
 for (Status status : statusList) {
  if(status.getText().toLowerCase().contains(yourstring)){
    System.out.println(status.getUser().getName() + " : " + status.getText());
  }
 } 
Comments