2011-09-25 80 views
5

我想通過twitter4j TwitterStream對象獲取twitter上的所有推文。我不確定我是否收到了所有的推文。爲了測試流媒體API返回推文之後的延遲,我在Twitter上發佈了一條來自我的賬戶的推文。但是,即使經過很長時間,我仍然沒有收到推文。Twitter4j TwitterStream不會獲得所有推文

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(); 

回答

10

我願意就這一矛盾,但我相信它的工作原理是這樣的...

流API只給出樣本推特給非合作伙伴。這是「花園軟管」,而不是幾個Twitter合作伙伴獲得的「firehose」。但你可以申請完全訪問。

.sample()給出了這個「花園軟管」。您的Twitter帳戶將無法訪問firehose,但我認爲如果您有權訪問,則有一個用於firehose的twitterStream。

在此頁面上搜索「狀態/樣本」:https://dev.twitter.com/docs/streaming-api/methods