Twitter4j only collects a specific country
For a project I'm currently working on, I only need to collect tweets from a stream for one country. While Twitter4j's streaming API allows filtering by language, the results are not accurate enough. So I decided to put the filter on top of the filter by checking if the country attribute of the tweet is filled in. This works great when I check if the value is valid:
if(status.getPlace().getCountry() != null) {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
TwitterStream ts = new TwitterStreamFactory(cb.build()).getInstance();
ts.addListener(listener);
FilterQuery filter = new FilterQuery();
String[] language = {"country"};
String[] keywords = {"some keywords"};
filter.track(keywords);
filter.language(language);
ts.filter(filter);
But if I check a specific country for example. I am not receiving any tweets:
if(status.getPlace().getCountry() != "germany") {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
It would be great if someone could help me with this.
source to share
filter.track(keywords);
filter.language(language);
Be aware that the above code means track(keywords)
OR language(language)
. This is not logical.
If you only want tweets from one country and with specific keywords, delete filter.language(language)
and check the country after you get the status.
if(status.getPlace().getCountry().equalsIgnoreCase("germany")) {}
// Compare strings
You will not receive a tweet from Germany if no one from Germany tweets with the filters you specified.
source to share