あるキーワードについて1週間分全てのツイートを保存するプログラム

89 views
Skip to first unread message

ask R

unread,
Jan 18, 2016, 5:20:45 AM1/18/16
to Twitter4J J
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;



public class SearchSave {
   
      static final String DEFAULT_WORD = "キーワード;
      static String word = DEFAULT_WORD;
      private static Object tweet;
   
   

  
    public static void main(String[] args) throws TwitterException, InterruptedException {
        // 初期化
        Twitter twitter = new TwitterFactory().getInstance();
        Query query = new Query();
                query.setLang("ja");
                int count = 0;
                int tweetcount = 0;
         
           
        // 検索ワードをセット
        query.setQuery(word);

        // 1度のリクエストで取得するTweetの数(100が最大)
        query.setCount(100);
                query.setSince("2016-01-12");
                query.setUntil("2016-01-17");
     
               
                // 書き込むファイルの名前
         String outputFileName = "C:\\karuizawa13152.txt";
        
          File outputFile = new File(outputFileName);
       try{          FileOutputStream fos = new FileOutputStream(outputFile);
                 OutputStreamWriter osw = new OutputStreamWriter(fos);
                   
                try (PrintWriter pw = new PrintWriter(osw)) {
                       
                   while(true) {
              
                       QueryResult result = twitter.search(query);
              
                           
                            // 検索結果を見てみる
                            for (Status tweet : result.getTweets()) {
                               
                                String str = tweet.getText();
                        
                                System.out.println("本文:"+str);
                                System.out.println("日時:"+tweet.getCreatedAt());
                                System.out.println("ID:"+tweet.getId());
                                System.out.println("RT数:"+tweet.getRetweetCount());
                            
                                pw.println("本文:"+str);
                                pw.println("日時:"+tweet.getCreatedAt());
                                pw.println("ID:"+tweet.getId());
                               
                                tweetcount++;
                       
                                 

                                count++;
            
   
                          
                          
                    if (result.hasNext()) {
                              query = result.nextQuery();
                           } else {
                               break;
                            }
                   if((count % 15000) == 0){
                     try{
                                     Thread.sleep(900000);
                  } catch(InterruptedException e){}
                   }
                   
                  
                  /
                   System.out.println("取得したツイート数: " + tweetcount);
                pw.println("取得したツイート数"+tweetcount);

                }
 
               
 
                }
            }  catch (IOException e){

                e.printStackTrace();
            }
      
    }
}
     
 

これで指定した日付の間のツイートを全て収集できると思うのですが、条件当てはまっていてもthread.sleepを無視したり、BuildSuccessとなりプログラムが終了してしまいます。改善方法を教えてください。

ask R

unread,
Jan 18, 2016, 6:10:12 AM1/18/16
to Twitter4J J
原因はわかりました。
if (result.hasNext())のところでelseにいきbreakしてしまっているようです
解決方法は未だわかりませんのでお願いいたします

Yamamoto Yusuke

unread,
Jan 18, 2016, 6:56:07 AM1/18/16
to twitt...@googlegroups.com
ここや
static final String DEFAULT_WORD = "キーワード;
ここで、
/
そもそもコンパイルが通らないですね。
hasNextでfalseが帰るということは、検索結果を全て洗い出してしまったということではないでしょうか。
“スターバックス”みたいに毎分のようにツイートされている単語を指定してレートリミットに達するかどうか試してみてください。
--
山本 裕介
@yusuke
http://samuraism.jp/

> On Jan 18, 2016, at 20:10, ask R <kyom...@gmail.com> wrote:
>
> 原因はわかりました。
> if (result.hasNext())のところでelseにいきbreakしてしまっているようです
> 解決方法は未だわかりませんのでお願いいたします
>
> --
> Twitter4J の最新情報をフォロー: http://twitter.com/t4j_news
> 現在のバージョン - 安定: 4.0.4, 開発中: 4.0.5-SNAPSHOT
> バグトラッキング: http://issue.twitter4j.org/youtrack/issues/TFJ
>
> このメールは次の Google グループの参加者に送られています: Twitter4J J
> このグループにメールで投稿: twitt...@googlegroups.com
> このグループから退会する: http://groups.google.com/group/twitter4j-j?hl=ja
> ---
> このメールは Google グループのグループ「Twitter4J J」に登録しているユーザーに送られています。
> このグループから退会し、グループからのメールの配信を停止するには twitter4j-j...@googlegroups.com にメールを送信してください。
> その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

Reply all
Reply to author
Forward
0 new messages