java - 如何在 Twitter 中检查流式 API 中的速率限制

标签 java twitter twitter4j

我已经设法获取了某个关键字的实时推文。到目前为止一切顺利,我尝试添加一些 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.

https://dev.twitter.com/discussions/2907

关于java - 如何在 Twitter 中检查流式 API 中的速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24607425/

相关文章:

java - 无法使用 Eclipse 上的最新更新修改属性

python - 使用 GAE Python 的 Twitter Streaming API

android - 推特。通过媒体发布状态

java - 使用 Twitter4j 我如何获得特定用户 Collection 的所有推文列表

java - android 新手 - 如何使用 SocialAuthAdapter 类中找到的 'uploadImage' 方法

java - 如何为 log4j 设置单独的日志记录流?

java - Google Guava 并发中 Futures.addCallBack() 和 Futures.transform() 的区别

java - 如何打印这个类的toString?

ios - 在 iOS Swift 上弃用后 Twitter.sharedInstance().session()?.userName 的替代方案

java - 将视频上传至 twitvid 并获取链接