cc.twittertools.index.ExtractTweetidsFromCollection.java Source code

Java tutorial

Introduction

Here is the source code for cc.twittertools.index.ExtractTweetidsFromCollection.java

Source

/**
 * Twitter Tools
 *
 * 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 cc.twittertools.index;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import cc.twittertools.corpus.data.JsonStatusCorpusReader;
import cc.twittertools.corpus.data.StatusStream;
import twitter4j.Status;

public class ExtractTweetidsFromCollection {
    private static final String COLLECTION_OPTION = "collection";

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws Exception {
        Options options = new Options();

        options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory")
                .create(COLLECTION_OPTION));

        CommandLine cmdline = null;
        CommandLineParser parser = new GnuParser();
        try {
            cmdline = parser.parse(options, args);
        } catch (ParseException exp) {
            System.err.println("Error parsing command line: " + exp.getMessage());
            System.exit(-1);
        }

        if (!cmdline.hasOption(COLLECTION_OPTION)) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ExtractTweetidsFromCollection.class.getName(), options);
            System.exit(-1);
        }

        String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);

        File file = new File(collectionPath);
        if (!file.exists()) {
            System.err.println("Error: " + file + " does not exist!");
            System.exit(-1);
        }

        StatusStream stream = new JsonStatusCorpusReader(file);

        Status status;
        while ((status = stream.next()) != null) {
            System.out.println(status.getId() + "\t" + status.getUser().getScreenName());
        }
    }
}