edu.umich.cse.pyongjoo.twittercrawl.GetUserTimeline.java Source code

Java tutorial

Introduction

Here is the source code for edu.umich.cse.pyongjoo.twittercrawl.GetUserTimeline.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 edu.umich.cse.pyongjoo.twittercrawl;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import edu.umich.cse.pyongjoo.twittercrawl.CustomConfig;
import edu.umich.cse.pyongjoo.twittercrawl.CustomConfig2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.1.7
 */
public class GetUserTimeline {
    /**
     * Usage: java twitter4j.examples.timeline.GetUserTimeline
     *
     * @param args String[]
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        OAuthTokenReader oauth = new OAuthTokenReader("oauth_tokens.csv");

        TwitterFactory tf = new TwitterFactory(oauth.getNextConfiguration());

        // gets Twitter instance with default credentials
        Twitter twitter = tf.getInstance();

        if (args.length < 2) {
            System.err.println("Usuage: command [username] [outputfile]");
            System.exit(-1);
        }
        String filename = args[1];

        FileWriter fstream = new FileWriter(filename, true);
        BufferedWriter out = new BufferedWriter(fstream);

        String user = "";
        if (args.length >= 1) {
            user = args[0];
        }
        //      out.write("#document starts with username: " + user + "\n");

        for (int i = 1; i <= 1; i++) {
            Paging pagingOption = new Paging(i, 200);

            try {
                List<Status> statuses;

                statuses = twitter.getUserTimeline(user, pagingOption);

                System.out.println("My Custom Showing @" + user + "'s user timeline.");

                for (Status status : statuses) {
                    out.write(status.toString() + '\n');
                    System.out.println(status.getUser().getScreenName() + "tweets written.");
                }
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to get timeline: " + te.getMessage());

                // close the file
                out.close();
                //              output.close();

                System.exit(-1);
            }
        }

        // close the file
        out.close();
        //      output.close();
    }
}