Twitter4j TwitterStream 无法获取所有推文

发布于 2024-12-06 04:47:37 字数 1919 浏览 0 评论 0原文

我试图通过 twitter4j TwitterStream 对象获取 twitter 上的所有推文。我不确定我是否收到了所有推文。为了测试流 API 返回推文的延迟,我从我的 Twitter 帐户中发布了一条推文。但过了很长一段时间我都没有收到那条推文。

twitter4j 是否捕获 Twitter 上发布的每一条推文,还是丢失了很大比例的推文?或者我在这里做错了什么? 这是我用来获取推文的代码:

        StatusListener listener = new StatusListener(){
        int countTweets = 0;    // Count to implement batch processing

        public void onStatus(Status status) {
            countTweets ++;
            StatusDto statusDto = new StatusDto(status);
            session.saveOrUpdate(statusDto);

            // Save 1 round of tweets to the database
            if (countTweets == BATCH_SIZE) {
                countTweets = 0;
                session.flush();
                session.clear();
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub
        }           
    };

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey(Twitter4jProperties.CONSUMER_KEY)
      .setOAuthConsumerSecret(Twitter4jProperties.CONSUMER_SECRET)
      .setOAuthAccessToken(Twitter4jProperties.ACCESS_TOKEN)
      .setOAuthAccessTokenSecret(Twitter4jProperties.ACCESS_TOKEN_SECRET);

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    twitterStream.addListener(listener);

    session = HibernateUtil.getSessionFactory().getCurrentSession();
    transaction = session.beginTransaction();

    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.sample();

I am trying to get all the tweets on twitter through the twitter4j TwitterStream object. I'm not sure that I am getting all the tweets. For testing the delay after which the streaming API returns the tweet, I posted a tweet from my account on twitter. But I didn't receive that tweet even after a long time.

Does the twitter4j catch each and every tweet posted on twitter or it loses a good percentage of the tweets? Or am I doing something wrong here?
Here's the code that I am using to get the tweets:

        StatusListener listener = new StatusListener(){
        int countTweets = 0;    // Count to implement batch processing

        public void onStatus(Status status) {
            countTweets ++;
            StatusDto statusDto = new StatusDto(status);
            session.saveOrUpdate(statusDto);

            // Save 1 round of tweets to the database
            if (countTweets == BATCH_SIZE) {
                countTweets = 0;
                session.flush();
                session.clear();
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub
        }           
    };

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey(Twitter4jProperties.CONSUMER_KEY)
      .setOAuthConsumerSecret(Twitter4jProperties.CONSUMER_SECRET)
      .setOAuthAccessToken(Twitter4jProperties.ACCESS_TOKEN)
      .setOAuthAccessTokenSecret(Twitter4jProperties.ACCESS_TOKEN_SECRET);

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    twitterStream.addListener(listener);

    session = HibernateUtil.getSessionFactory().getCurrentSession();
    transaction = session.beginTransaction();

    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.sample();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2024-12-13 04:47:37

我对此持开放态度,但我相信它的工作原理是这样的......

Streaming API 只为非合作伙伴提供推文样本。它是“花园软管”,而不是一些 Twitter 合作伙伴所获得的“消防软管”。但您可以申请完全访问权限。

.sample() 给出了这个“花园软管”。你的 Twitter 帐户将无法访问 Firehose,尽管我认为如果你确实有访问权限的话,Firehose 会有一个 twitterStream。

在此页面上搜索“statuses/sample”以了解具体信息:https://dev.twitter.com/docs/流式API/方法

I'm open to contradiction on this, but I believe it works like this...

Streaming API only gives a sample of tweets for non-partners. It's the "garden hose" as opposed to the "firehose" which a few Twitter partners get. But you can apply for full access.

.sample() gives this "garden hose". Your twitter account won't have access to the firehose, although I think there is a twitterStream for the firehose if you did have access.

Search for "statuses/sample" on this page for the specifics: https://dev.twitter.com/docs/streaming-api/methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文