I have not being banned by Twitter yet. However, I would like to avoid it. I have simple method using StatusListener to pull the tweets according to the keywords array, then they will be filtered by the branches array. As I understood, StatusLintener gets only the new tweets and still running till the application be stopped.
I think this code will reach the rate limit after a while. So, is there any way to handle it? An authentication request can request 350 time in a hour, how does it work with StatusLintener?
public static void getTweetsKeywords(ConfigurationBuilder cb, String[] keywords, String[] branches,){
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
System.out.println(status.getCreatedAt()+" - "+"@" + status.getUser().getScreenName() + " - " + status.getText());
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
};
FilterQuery fq = new FilterQuery();
fq.track(keywords);
twitterStream.addListener(listener);
twitterStream.filter(fq);
}
thanks
StatusListener doesn't poll the Twitter API to obtain the tweets. It listens to the stream of tweets from Twitter Streaming API. It is therefore not subject to rate limitations.
来源:https://stackoverflow.com/questions/13273174/how-to-handle-rate-limit-using-twitter4j-to-avoid-being-banned