twitterrest.SearchTweet.java Source code

Java tutorial

Introduction

Here is the source code for twitterrest.SearchTweet.java

Source

/*
 * Copyright 2007 Yusuke Yamamoto
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package twitterrest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

/*????*/
public class SearchTweet {
    //OAUTH?
    final static String CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    public static void main(String[] args) throws TwitterException {
        //?
        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
                .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
        Twitter twitter = new TwitterFactory(configuration).getInstance();
        Query query = new Query();
        try {
            File file = new File("./file/tweets.txt");
            PrintWriter pw;
            //???
            pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));

            // ???
            query.setQuery("?");
            query.setLang("ja");
            // 1????Tweet?100?
            query.setCount(100);
            //?????????
            //query.setSince("2013-06-13");
            //query.setUntil("2013-06-17");

            // 150015
            for (int i = 1; i <= 15; i++) {
                // 
                QueryResult result = twitter.search(query);
                System.out.println(": " + result.getTweets().size());
                System.out.println(" : " + new Integer(i).toString());

                // ???
                for (Status tweet : result.getTweets()) {
                    // 
                    String str = tweet.getText();
                    System.out.println(str);
                    //
                    System.out.println(tweet.getUser());
                    //
                    System.out.println(tweet.getCreatedAt());
                    // ??URL?   
                    StringTokenizer sta = new StringTokenizer(str, " ");
                    //?
                    while (sta.hasMoreTokens()) {
                        String wk = sta.nextToken();
                        if (wk.indexOf("#") == -1 && wk.indexOf("http") == -1 && wk.indexOf("RT") == -1
                                && wk.indexOf("@") == -1) {
                            pw.println(wk);
                        }
                    }
                    String u = tweet.getUser().getName();
                    pw.println(u);
                }
                if (result.hasNext()) {
                    query = result.nextQuery();
                } else {
                    break;
                }
            }
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}