如何使用twitter4j处理速率限制以避免被禁止 [英] How to handle rate limit using twitter4j to avoid being banned

查看:32
本文介绍了如何使用twitter4j处理速率限制以避免被禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有被 Twitter 封禁.但是,我想避免它.我有一个简单的方法使用 StatusListener 根据关键字数组提取推文,然后它们将被分支数组过滤.据我了解,StatusLintener 仅获取新推文,并且仍在运行,直到应用程序停止.

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.

我认为这段代码会在一段时间后达到速率限制.那么,有没有办法处理呢?一个身份验证请求可以在一个小时内请求 350 次,它如何与 StatusLintener 一起工作?

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);      
}

谢谢

推荐答案

StatusListener 不会轮询 Twitter API 来获取推文.它侦听来自 Twitter Streaming API 的推文流.因此,它不受速率限制.

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.

这篇关于如何使用twitter4j处理速率限制以避免被禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆