I am trying to get all the tweets on twitter via the twitter4j TwitterStream object. I'm not sure I get all the tweets. To test the delay after which the streaming API returns a tweet, I sent a tweet from my twitter account. But I did not get this tweet even after a long time.
Does twitter4j catch every twitter post on Twitter, or does it lose a good percentage of tweets? Or am I doing something wrong? Here is the code I use to get tweets:
StatusListener listener = new StatusListener(){
int countTweets = 0;
public void onStatus(Status status) {
countTweets ++;
StatusDto statusDto = new StatusDto(status);
session.saveOrUpdate(statusDto);
if (countTweets == BATCH_SIZE) {
countTweets = 0;
session.flush();
session.clear();
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
public void onScrubGeo(long arg0, long arg1) {
}
};
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(Twitter4jProperties.CONSUMER_KEY)
.setOAuthConsumerSecret(Twitter4jProperties.CONSUMER_SECRET)
.setOAuthAccessToken(Twitter4jProperties.ACCESS_TOKEN)
.setOAuthAccessTokenSecret(Twitter4jProperties.ACCESS_TOKEN_SECRET);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
twitterStream.sample();
source
share