我已经设法获取了某个关键字的实时推文。到目前为止一切顺利,我尝试添加一些 RateLimitStatusListener
以防达到速率限制。
问题是,永远不会被调用。即使我达到速率限制也不会被调用。我想不通。 我的意图是,如果我收到速率限制错误,请进行一些错误处理。尽管如此,还有其他方法可以检查是否已达到速率限制吗?
public final class PrintSampleStream {
public static void main(String[] args) throws TwitterException {
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
};
twitterStream.addRateLimitStatusListener( new RateLimitStatusListener() {
@Override
public void onRateLimitStatus( RateLimitStatusEvent event ) {
System.out.println("Limit["+event.getRateLimitStatus().getLimit() + "], Remaining[" +event.getRateLimitStatus().getRemaining()+"]");
}
@Override
public void onRateLimitReached( RateLimitStatusEvent event ) {
System.out.println("Limit["+event.getRateLimitStatus().getLimit() + "], Remaining[" +event.getRateLimitStatus().getRemaining()+"]");
}
} );
FilterQuery filterQuery = new FilterQuery();
String[] query = {"#WorldCup2014"};
filterQuery.track(query);
twitterStream.addListener(listener);
twitterStream.filter(filterQuery);
}
}
最佳答案
您提到的速率限制仅适用于 twitter rest api,不适用于流式 api。在流式传输中,不需要添加那样的代码,因为限制是以不同的方式处理的:
You will be streamed 1% of total volume
这意味着您将获得您在流媒体 API 中跟踪或关注的内容的 1%。
How are rate limits determined on the Streaming API?
The public streaming APIs cap the number of messages sent to your client to a small fraction of the total volume of Tweets at any given moment.
The sample hose, as documented in https://stream.twitter.com/1/statuses/sample.json, delivers a random sampling of all Tweets at a volume equal to the public streaming cap.
Filtered streams return all matching Tweets up to a volume equal to the streaming cap. If there are more tweets that would match your criteria, you'll be streamed a rate limit message indicating how many tweets were not delivered.
关于java - 如何在 Twitter 中检查流式 API 中的速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24607425/