I am working on a web application that gets all tweets within a time period for a specific keyword. The Twitter4J API does not return all tweets but the most relevant. Is there a way to get all tweets for a specific keyword? I have tried setting the query result type to RECENT
, but that is still unsuccessful at getting everything.
I know about Twitters streaming API but I am not able to collect data for a period of time. The search keyword can be for anything so collecting and storing so much data is not reasonable.
public static void main(String[] args) throws TwitterException {
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("(amazon) AND (stock)");
query.setCount(100);
query.setLang("en");
query.setResultType(Query.RECENT);
query.setSince("2017-02-06");
QueryResult result = twitter.search(query);
System.out.println("PRINTING:");
int numberOfTweets = 0;
for (Status status : result.getTweets()) {
numberOfTweets++;
System.out.println("-----------------------------------");
System.out.println("@" + status.getUser().getScreenName() + "-" + status.getCreatedAt() + ":");
System.out.println(status.getText());
}
System.out.println(numberOfTweets);
}
From the documentation on the Search API
Please note that Twitter’s search service and, by extension, the Search API is not meant to be an exhaustive source of Tweets. Not all Tweets will be indexed or made available via the search interface.
You cannot get everything.