How to handle rate limit using twitter4j to avoid being banned

强颜欢笑 提交于 2019-12-06 12:35:37

问题


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


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!