Twitter4j TwitterStream没有收到所有推文

时间:2011-09-25 18:06:07

标签: java twitter twitter4j

我试图通过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();

1 个答案:

答案 0 :(得分:10)

我对这方面的矛盾持开放态度,但我相信它的确如此......

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

.sample()给出了这个“花园软管”。您的Twitter帐户将无法访问firehose,但如果您确实有访问权限,我认为有一个用于firehose的twitterStream。

在此页面上搜索“状态/样本”,了解具体信息:https://dev.twitter.com/docs/streaming-api/methods