简体   繁体   English

Twitter4j TwitterStream没有收到所有推文

[英]Twitter4j TwitterStream doesn't get all tweets

I am trying to get all the tweets on twitter through the twitter4j TwitterStream object. 我试图通过twitter4j TwitterStream对象获取Twitter上的所有推文。 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. 为了测试流API返回推文之后的延迟,我在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? twitter4j是否会抓住Twitter上发布的每条推文,或者它的推文丢失率很高? 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();

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. Streaming API仅提供非合作伙伴的推文样本。 It's the "garden hose" as opposed to the "firehose" which a few Twitter partners get. 它是“花园软管”,而不是一些Twitter合作伙伴得到的“firehose”。 But you can apply for full access. 但您可以申请完全访问权限。

.sample() gives this "garden hose". .sample()给出了这个“花园软管”。 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. 您的Twitter帐户将无法访问firehose,但我认为如果您确实有访问权限,则会有一个用于firehose的twitterStream。

Search for "statuses/sample" on this page for the specifics: https://dev.twitter.com/docs/streaming-api/methods 在此页面上搜索“状态/样本”以获取具体信息: https//dev.twitter.com/docs/streaming-api/methods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM