com.happy_coding.viralo.coordinator.ViraloRunner.java Source code

Java tutorial

Introduction

Here is the source code for com.happy_coding.viralo.coordinator.ViraloRunner.java

Source

/**
 * Copyright (C) 2012 Happy Coding <contact@happy-coding.com>
 *
 * 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 com.happy_coding.viralo.coordinator;

import com.happy_coding.viralo.twitter.Contact;
import com.happy_coding.viralo.twitter.FriendDiscoverer;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

/**
 * CLI.
 */
public class ViraloRunner {

    public static final int WAIT_MS_BETWEEN_ACTIONS = 5000;

    /**
     * Main task.
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // Parser
        CommandLineParser parser = new PosixParser();

        // Options
        Options options = new Options();

        Option createFriends = OptionBuilder.withDescription("Create friends by using the given keywords.").hasArg()
                .withArgName("keywords").create("createFriends");

        options.addOption(createFriends);
        options.addOption("R", "refreshFollowers", false, "refresh followers for current user.");
        options.addOption("C", "cleanup", false, "delete friends which don't follow");
        options.addOption("S", "smartTweet", false, "Posts a smart tweet.");
        options.addOption("F", "createTrendFriends", false, "Create friends to a random trend.");
        options.addOption("T", "answerTopQuestion", false, "answer a top question and post it.");
        options.addOption("RC", "robotConversation", false, "starts a task which answers mentions automatically.");

        if (args.length < 1) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar ...jar", options);
            System.exit(0);
        }

        CommandLine line = parser.parse(options, args);

        if (line.hasOption("createFriends")) {

            String keywords = line.getOptionValue("createFriends");

            Viralo viralo = new Viralo();

            FriendDiscoverer friendDiscoverer = new FriendDiscoverer();
            Contact forContact = friendDiscoverer.returnMyself();

            System.out.println("Create friends for " + forContact.getName());

            viralo.createNewFriends(forContact, keywords, ";");

        } else if (line.hasOption("refreshFollowers")) {

            Viralo viralo = new Viralo();

            FriendDiscoverer friendDiscoverer = new FriendDiscoverer();
            Contact forContact = friendDiscoverer.returnMyself();

            viralo.refreshFollowers(forContact);

        } else if (line.hasOption("cleanup")) {

            Viralo viralo = new Viralo();

            FriendDiscoverer friendDiscoverer = new FriendDiscoverer();
            Contact forContact = friendDiscoverer.returnMyself();

            viralo.cleanup(forContact);

        } else if (line.hasOption("smartTweet")) {
            Viralo viralo = new Viralo();
            viralo.postSmartTweet();
        } else if (line.hasOption("createTrendFriends")) {
            FriendDiscoverer friendDiscoverer = new FriendDiscoverer();
            Contact forContact = friendDiscoverer.returnMyself();
            Viralo viralo = new Viralo();
            viralo.createNewFriends(forContact);
        } else if (line.hasOption("answerTopQuestion")) {
            Viralo viralo = new Viralo();
            viralo.answerTopQuestion();
        } else if (line.hasOption("robotConversation")) {

            boolean taskFlag = true;
            Viralo viralo = new Viralo();

            while (taskFlag) {

                /*
                reply to mentions
                 */
                viralo.autoConversation();

                /*
                wait some seconds.
                 */
                Thread.sleep(WAIT_MS_BETWEEN_ACTIONS);
            }

        }

        System.exit(0);
    }
}