javaでtwitter4jを使ってツイートを収集しています。searchAPIを使って検索をしているのですが、検索されたツイートに含まれる画像のURLを取得する方法がわかりません。以下がツイートを検索するプログラムの全文です。
java
1import java.io.File; 2import java.io.FileWriter; 3import java.io.IOException; 4 5import twitter4j.Query; 6import twitter4j.QueryResult; 7import twitter4j.Status; 8import twitter4j.Twitter; 9import twitter4j.TwitterException; 10import twitter4j.TwitterFactory; 11 12 13 /** 14 * @author katoy 15 */ 16 public class TwitterMediaInformation { 17 18 19 static final String DEFAULT_WORD = "アベノミクス exclude:retweets"; 20 static final int MAX_PAGE = 15; // 1; 21 22 static String word = DEFAULT_WORD; 23 static int page = MAX_PAGE; 24 static String from_date = null; // "2014-11-01"; 25 static String to_date = null; // "2014-11-09"; 26 27 28 29 30 /** 31 * コマンドメイン 32 * 33 * @param args コマンドラインパラメータ 34 */ 35 public static void main(String[] args) { 36 parse_args(args); 37 try { 38 new TwitterReader().reader(TwitterMediaInformation.word, 39 TwitterMediaInformation.from_date, TwitterMediaInformation.to_date, 40 TwitterMediaInformation.page); 41 } catch (TwitterException ex) { 42 // TODO Auto-generated catch block 43 ex.printStackTrace(); 44 } 45 } 46 47 48 static void parse_args(String[] args) { 49 if (args.length > 4) { 50 throw new IllegalArgumentException(); 51 } 52 if (args.length > 3) { 53 TwitterMediaInformation.to_date = args[3]; 54 } 55 if (args.length > 2) { 56 TwitterMediaInformation.from_date = args[2]; 57 } 58 if (args.length > 1) { 59 TwitterMediaInformation.page = Integer.parseInt(args[1]); 60 } 61 if (args.length > 0) { 62 TwitterMediaInformation.word = args[0]; 63 } 64 } 65 } 66 67 68 class TwitterReader { 69 70 71 /** 72 * 73 * @param word 検索ワード 74 * @param from_date 期間の開始 YYYY-MM-DD 75 * @param to_date 期間の収容 YYYY-MM-DD 76 * @param page 検索するページ数 77 * @return int 検索した tweet 数。 78 * @throws TwitterException 79 */ 80 int reader(final String word, final String from_date, final String to_date, final int page) throws TwitterException { 81 // 初期化 82 int count = 0; // 取得した総件数 83 final Twitter twitter = new TwitterFactory().getInstance(); 84 Query query = new Query(); 85 86 87 // 検索ワードをセット 88 query.setQuery(word); 89 90 91 // 1 度のリクエストで取得する Tweet の数(100が最大) 92 query.setCount(100); 93 query.resultType(Query.RECENT); 94 if (from_date != null) { 95 query.since(from_date); 96 } 97 if (to_date != null) { 98 query.until(to_date); 99 } 100 int index = word.indexOf(" "); 101 try{ 102 File file = new File("C:\\work\\研究\\" + word.substring(0,index) + ".txt"); 103 FileWriter filewriter = new FileWriter(file); 104 for (int i = 0; i < page; i++) { 105 QueryResult result = twitter.search(query); 106 System.out.println("ヒット数 : " + result.getTweets().size()); 107 System.out.println("ページ数 : " + i); 108 109 // 検索結果を見てみる 110 for (Status tweet : result.getTweets()) { 111 // 本文 112 String str = tweet.getText(); 113 java.util.Date hiduke = tweet.getCreatedAt(); 114 count += 1; 115 System.out.println("" + count + "\t" + hiduke + str); 116 filewriter.write("" + count + "\t" + hiduke + str + "\r\n"); 117 // ハッシュタグ と URL の削除 118 } 119 if (result.hasNext()) { 120 query = result.nextQuery(); 121 } else { 122 break; 123 } 124 } 125 filewriter.close(); 126 }catch(IOException e){ 127 System.out.println(e); 128 } 129 return count; 130 } 131 } 132 133
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。