2012-02-22 123 views
5

我的目標是收集包含單詞「France」和「Germany」的所有推文,並收集關聯的元數據(例如,附加到推文的地理座標)。我知道這個元數據是可用的,但我不知道如何使用我正在使用的Java庫訪問它:「twitter4j」。twitter4j - 從Streaming API訪問tweet信息

好吧,所以我到目前爲止取自twitter4j網站上的代碼示例。它會打印出包含我所選關鍵字的所有推文,因爲它們是由Twitter的Streaming API實時提供的。我在我的TwitterStream對象上調用過濾器方法,並提供流。但我需要更多的控制。即,我希望能夠:

1)將tweets寫入文件; 2)只打印出前1000條推文; 3)訪問附加到推文的其他元數據(篩選方法只是輸出用戶名和推文本身)。

這裏是我的代碼至今:

import twitter4j.FilterQuery; 
import twitter4j.Status; 
import twitter4j.StatusDeletionNotice; 
import twitter4j.StatusListener; 
import twitter4j.TwitterException; 
import twitter4j.TwitterStream; 
import twitter4j.TwitterStreamFactory; 
import twitter4j.conf.ConfigurationBuilder; 

public class Stream { 
    public static void main(String[] args) throws TwitterException { 

    ConfigurationBuilder cb = new ConfigurationBuilder(); 
    cb.setDebugEnabled(true); 
    cb.setOAuthConsumerKey("bbb"); 
    cb.setOAuthConsumerSecret("bbb"); 
    cb.setOAuthAccessToken("bbb"); 
    cb.setOAuthAccessTokenSecret("bbb"); 

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

     public void onStatus(Status status) { 
      System.out.println("@" + 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(); 
    String keywords[] = {"France", "Germany"}; 

    fq.track(keywords); 

    twitterStream.addListener(listener); 
    twitterStream.filter(fq);  
} 
} 
+0

需要幫助。我收到以下錯誤。 '新的StatusListener(){}類型必須實現繼承的抽象方法StatusListener.onStallWarning(StallWarning)' – 2016-04-15 09:22:24

回答

5

看着這個新鮮的眼睛,我意識到了解決方案(這是很明顯的)。編輯以下部分代碼:

public void onStatus(Status status) { 
     System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); 
    } 

允許我訪問其他元數據。例如,如果我想訪問鳴叫的日期,我只需要添加以下內容:

System.out.println(status.getCreatedAt()); 
0

的401錯誤時,如果本API試圖訪問一些信息,這些信息無法在目前獲取。所以你需要檢查twitter上允許的權限。將其更改爲READ,WRITE和...以獲得完整的API訪問權限。或者可能會有問題,因爲您可能正在使用代理服務器。因此使用以下命令提及代理細節。

System.getProperties().put("http.proxyHost", "10.3.100.211"); 
     System.getProperties().put("http.proxyPort", "8080"); 
0

爲了寫文件鳴叫:

FileWriter file = new FileWriter(....); 

public void onStatus(Status status) { 
    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " -> "+ status.getCreatedAt()); 
    try { 
     file.write(status.getUser().getScreenName() + " - " + status.getText() + " -> "+ status.getCreatedAt() +"\n"); 
     file.flush(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}