【问题标题】:How to handle rate limit using twitter4j to avoid being banned如何使用 twitter4j 处理速率限制以避免被禁止
【发布时间】:2014-09-05 12:54:28
【问题描述】:

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

我认为这段代码会在一段时间后达到速率限制。那么,有没有办法处理呢? 一个身份验证请求可以在一小时内请求 350 次,它如何与 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);      
}

谢谢

【问题讨论】:

  • @PulkitGoyal 当然...我只是搞砸了概念...

标签: twitter4j


【解决方案1】:

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

【讨论】:

  • 太好了...谢谢 Sapan....我可以让它运行(听)几个小时吗?直到现在我才意识到它可以获得“实时”推文。
  • 是的,你可以让它运行多久就运行多久。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2020-11-22
  • 1970-01-01
  • 2016-02-22
  • 2022-01-14
  • 2020-02-03
  • 2021-10-12
相关资源
最近更新 更多