Example usage for twitter4j TwitterException getMessage

List of usage examples for twitter4j TwitterException getMessage

Introduction

In this page you can find the example usage for twitter4j TwitterException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:spammerwadgets.AutoOAuthAccess.java

License:Apache License

public static void main(String[] args) {
    File file = new File(fileName);
    Properties prop = new Properties();
    InputStream is = null;//from  w w  w . j a v a  2 s . c  om
    OutputStream os = null;

    prop.setProperty("oauth.consumerKey", "E7uuluwC0ZsAFbV9pHimQ");
    prop.setProperty("oauth.consumerSecret", "c7amwRvOgb4icJArTRfgU8m2G64mbpVioqgmPDT4Yg");
    prop.setProperty("username", "AdrienneccDusti");
    prop.setProperty("password", "SIRENSSUBROUTINE");

    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        }
        if (args.length < 4) {
            if (null == prop.getProperty("oauth.consumerKey")
                    && null == prop.getProperty("oauth.consumerSecret") && null == prop.getProperty("username")
                    && null == prop.getProperty("password")) {
                // consumer key/secret are not set in twitter4j.properties
                System.out.println(
                        "Usage: java twitter4j.examples.oauth.AutoOAuthAccess [consumer key] [consumer secret][username] [password]");
                System.exit(-1);
            }
        } else {
            prop.setProperty("oauth.consumerKey", args[0]);
            prop.setProperty("oauth.consumerSecret", args[1]);
            prop.setProperty("username", args[2]);
            prop.setProperty("password", args[3]);
            os = new FileOutputStream(fileName);
            prop.store(os, fileName);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        RequestToken requestToken = twitter.getOAuthRequestToken();
        System.out.println("Got request token.");
        System.out.println("Request token: " + requestToken.getToken());
        System.out.println("Request token secret: " + requestToken.getTokenSecret());
        AccessToken accessToken = null;

        while (null == accessToken) {
            System.out.println("Open the following URL and grant access to your account:");
            String url = requestToken.getAuthorizationURL();
            System.out.println(url);

            /*
            BufferedReader urlbr = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
            String strTemp = "";
            while(null != (strTemp = urlbr.readLine())){
               System.out.println(strTemp);
            }
                    
                    
            try {
            Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
            } catch (IOException ignore) {
            } catch (URISyntaxException e) {
            throw new AssertionError(e);
            }
            System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
            String pin = br.readLine();
                    
            */

            // Alternatively, we will generate the POST request automatically
            // Firstly, we parse the webpage of the authotizationURL
            Document doc = Jsoup.connect(url).get();

            Element form = doc.getElementById("oauth_form");

            System.out.println(form.toString());

            String action = form.attr("action");

            String authenticity_token = form.select("input[name=authenticity_token]").attr("value");
            String oauth_token = form.select("input[name=oauth_token]").attr("value");

            // Then submit the authentication webpage
            Document doc2 = Jsoup.connect(action).data("authenticity_token", authenticity_token)
                    .data("oauth_token", oauth_token)
                    .data("session[username_or_email]", prop.getProperty("username"))
                    .data("session[password]", prop.getProperty("password")).post();

            System.out.println(doc2.toString());

            Element oauth_pin = doc2.getElementsByTag("code").first();
            String pin = oauth_pin.text();
            try {
                if (pin.length() > 0) {
                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                } else {
                    accessToken = twitter.getOAuthAccessToken(requestToken);
                }
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    System.out.println("Unable to get the access token.");
                } else {
                    te.printStackTrace();
                }
            }
        }
        System.out.println("Got access token.");
        System.out.println("Access token: " + accessToken.getToken());
        System.out.println("Access token secret: " + accessToken.getTokenSecret());

        try {
            prop.setProperty("oauth.accessToken", accessToken.getToken());
            prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret());
            os = new FileOutputStream(file);
            prop.store(os, fileName);
            os.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ignore) {
                }
            }
        }
        System.out.println("Successfully stored access token to " + file.getAbsolutePath() + ".");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get accessToken: " + te.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to read the system input.");
        System.exit(-1);
    }
}

From source file:spammerwadgets.AutoOAuthAccessFromFile.java

License:Apache License

void getAndWriteTokenToXML(String username, String password) {

    try {//from w w  w  . j a va 2  s  .  c  om
        AccessToken accessToken = null;
        accessToken = getAccessToken(username, password);
        if (accessToken == null) {
            return;
        }
        // Store the accessToken;
        org.w3c.dom.Element token = doc.createElement("UserAccessToken");
        rootElement.appendChild(token);

        org.w3c.dom.Element usernameXML = doc.createElement("username");
        usernameXML.appendChild(doc.createTextNode(username));
        token.appendChild(usernameXML);

        org.w3c.dom.Element passwordXML = doc.createElement("password");
        passwordXML.appendChild(doc.createTextNode(password));
        token.appendChild(passwordXML);

        org.w3c.dom.Element tokenXML = doc.createElement("AccessToken");
        tokenXML.appendChild(doc.createTextNode(accessToken.getToken()));
        token.appendChild(tokenXML);

        org.w3c.dom.Element tokenSecretXML = doc.createElement("AccessTokenSecret");
        tokenSecretXML.appendChild(doc.createTextNode(accessToken.getTokenSecret()));
        token.appendChild(tokenSecretXML);

        count++;
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get accessToken: " + te.getMessage());
        // storeTokensToFile();
        // System.exit(-1);
        Thread.currentThread();
        Random rand = new Random();
        try {
            Thread.sleep(rand.nextInt(200) * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // We will try again
        getAndWriteTokenToXML(username, password);
    } catch (IOException ie) {
        ie.printStackTrace();
        System.out.println("Failed to connect the server: " + ie.getMessage());
        // storeTokensToFile;
        // System.exit(-1);
        Thread.currentThread();
        Random rand = new Random();
        try {
            Thread.sleep(rand.nextInt(200) * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // We will try again
        getAndWriteTokenToXML(username, password);
    }
}

From source file:StringMatching.GetTweet.java

/**
 * @param args the command line arguments
 */// w w  w  . ja v a  2s  .  c  o  m
public static void main(String[] args) throws JSONException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH")
            .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK")
            .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA")
            .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC")
            .setHttpProxyHost("cache.itb.ac.id").setHttpProxyPort(8080).setHttpProxyUser("jonathan.benedict")
            .setHttpProxyPassword("rollingonthefloor");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    JSONObject obj = new JSONObject();
    int counterTweet = 0;
    FileWriter file = new FileWriter("C:\\Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt");
    file.flush();
    try {
        Query query = new Query("barca".toLowerCase());
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                counterTweet++;
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                obj.put("user", tweet.getUser().getScreenName());
                obj.put("tweets", tweet.getText());

                //Tulis file ke dalam txt

                try {
                    file.write(obj.toString());
                    System.out.println("Successfully Copied JSON Object to File...");
                    System.out.println("\nJSON Object: " + obj);

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        } while (counterTweet < 1000);

        file.close();
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}

From source file:timeline.CmdSearchTerm.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length != 6)) {

        System.err.println("Please provide command as following.");
        System.err.println("java -cp twitter4j-multi-oauth-0.5.jar "
                + "timeline.CmdSearchTerm consumer_key consumer_secret" + " user_token user_secret output_path "
                + "term ");
        System.exit(-1);/*  w ww. ja  v  a 2  s  . c o m*/
    }

    AppOAuth AppOAuths = new AppOAuth();
    String endpoint = "/search/tweets";

    String consumer_key = null;
    try {
        consumer_key = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    String consumer_secret = null;
    try {
        consumer_secret = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(-1);
    }
    String user_token = null;
    try {
        user_token = StringEscapeUtils.escapeJava(args[2]);
    } catch (Exception e) {
        System.err.println("Argument" + args[2] + " must be an String.");
        System.exit(-1);
    }
    String user_secret = null;
    try {
        user_secret = StringEscapeUtils.escapeJava(args[3]);
    } catch (Exception e) {
        System.err.println("Argument" + args[3] + " must be an String.");
        System.exit(-1);
    }

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[4]);
    } catch (Exception e) {
        System.err.println("Argument" + args[4] + " must be an String.");
        System.exit(-1);
    }

    String term = "";
    try {
        term = StringEscapeUtils.escapeJava(args[5]);
    } catch (Exception e) {
        System.err.println("Argument" + args[5] + " must be an String.");
        System.exit(-1);
    }

    try {

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, consumer_key, consumer_secret, user_token,
                user_secret);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("Remianing Calls: " + RemainingCalls);

        // screen_name / user_id provided by arguments
        System.out.println("Trying to create output directory");
        String filesPath = OutputDirPath + "/";
        File theDir = new File(filesPath);

        // If the directory does not exist, create it
        if (!theDir.exists()) {

            try {
                theDir.mkdirs();

            } catch (SecurityException se) {

                System.err.println("Could not create output " + "directory: " + OutputDirPath);
                System.err.println(se.getMessage());
                System.exit(-1);
            }
        }

        String fileName = filesPath + term.replace(" ", "");
        PrintWriter writer = new PrintWriter(fileName, "UTF-8");

        Query query = new Query(term);
        QueryResult result;

        List<Status> statuses = new ArrayList<>();
        int totalTweets = 0;
        int numberOfTweetsToGet = 5000;
        long lastID = Long.MAX_VALUE;

        while (totalTweets < numberOfTweetsToGet) {
            if (numberOfTweetsToGet - totalTweets > 100) {
                query.setCount(100);
            } else {
                query.setCount(numberOfTweetsToGet - totalTweets);
            }
            try {
                result = twitter.search(query);
                statuses.addAll(result.getTweets());

                if (statuses.size() > 0) {
                    for (Status status : statuses) {
                        String rawJSON = TwitterObjectFactory.getRawJSON(status);
                        writer.println(rawJSON);

                        totalTweets += 1;

                        if (status.getId() < lastID) {
                            lastID = status.getId();
                        }
                    }
                } else {
                    break;
                }
                System.out.println("totalTweets: " + totalTweets);
                statuses.clear();

            } catch (TwitterException e) {
                // e.printStackTrace();

                System.out.println("Tweets Get Exception: " + e.getMessage());

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    System.out.println("No more remianing calls");
                }

                if (totalTweets < 1) {
                    writer.close();
                    // Remove file if tweets not found
                    File fileToDelete = new File(fileName);
                    fileToDelete.delete();
                    break;
                }
            }
            query.setMaxId(lastID - 1);

            // If rate limit reached then switch Auth user
            RemainingCallsCounter++;
            if (RemainingCallsCounter >= RemainingCalls) {

                System.out.println("No more remianing calls");
                break;
            }
        }

        if (totalTweets > 0) {
            System.out.println("Total dumped tweets of " + term + " are: " + totalTweets);
        } else {

            // Remove file if tweets not found
            File fileToDelete = new File(fileName);
            fileToDelete.delete();
        }
        writer.close();
    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get term results because: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");
}

From source file:timeline.CmdUserTimeline.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length != 7)) {

        System.err.println("Please provide command as following.");
        System.err.println("java -cp twitter4j-multi-oauth-0.3.jar "
                + "timeline.CmdUserTimeline consumer_key consumer_secret"
                + " user_token user_secret output_path " + "screen_name_or_userid "
                + "number_of_tweets_to_get_max_is_3200");
        System.exit(-1);/* w  w w  .  j  a v  a 2s  .  c o  m*/
    }

    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/statuses/user_timeline";

    String consumer_key = null;
    try {
        consumer_key = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    String consumer_secret = null;
    try {
        consumer_secret = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(-1);
    }
    String user_token = null;
    try {
        user_token = StringEscapeUtils.escapeJava(args[2]);
    } catch (Exception e) {
        System.err.println("Argument" + args[2] + " must be an String.");
        System.exit(-1);
    }
    String user_secret = null;
    try {
        user_secret = StringEscapeUtils.escapeJava(args[3]);
    } catch (Exception e) {
        System.err.println("Argument" + args[3] + " must be an String.");
        System.exit(-1);
    }

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[4]);
    } catch (Exception e) {
        System.err.println("Argument" + args[4] + " must be an String.");
        System.exit(-1);
    }

    String targetedUser = "";
    try {
        targetedUser = StringEscapeUtils.escapeJava(args[5]);
    } catch (Exception e) {
        System.err.println("Argument" + args[5] + " must be an String.");
        System.exit(-1);
    }

    int NUMBER_OF_TWEETS = 0;
    try {
        NUMBER_OF_TWEETS = Integer.parseInt(args[6]);
    } catch (Exception e) {
        System.err.println("Argument" + args[6] + " must be an integer.");
        System.exit(-1);
    }

    try {

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, consumer_key, consumer_secret, user_token,
                user_secret);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("Remianing Calls: " + RemainingCalls);

        // screen_name / user_id provided by arguments
        System.out.println("Trying to create output directory");
        String filesPath = OutputDirPath + "/";
        File theDir = new File(filesPath);

        // If the directory does not exist, create it
        if (!theDir.exists()) {

            try {
                theDir.mkdirs();

            } catch (SecurityException se) {

                System.err.println("Could not create output " + "directory: " + OutputDirPath);
                System.err.println(se.getMessage());
                System.exit(-1);
            }
        }

        String fileName = filesPath + targetedUser;
        PrintWriter writer = new PrintWriter(fileName, "UTF-8");

        // Call different functions for screen_name and id_str
        Boolean chckedNumaric = helpers.isNumeric(targetedUser);

        List<Status> statuses = new ArrayList<>();
        int size = statuses.size();
        int pageno = 1;
        int totalTweets = 0;
        boolean tweetCounterReached = false;
        System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS);
        while (true) {

            try {

                Paging page = new Paging(pageno++, 200);

                if (chckedNumaric) {

                    long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                    statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page));

                    if (statuses.size() > 0) {
                        for (Status status : statuses) {
                            String rawJSON = TwitterObjectFactory.getRawJSON(status);
                            writer.println(rawJSON);

                            totalTweets += 1;
                            if (totalTweets >= NUMBER_OF_TWEETS) {
                                tweetCounterReached = true;
                                break;
                            }
                        }
                        if (tweetCounterReached) {
                            break;
                        }
                    }
                } else {

                    statuses.addAll(twitter.getUserTimeline(targetedUser, page));

                    if (statuses.size() > 0) {
                        for (Status status : statuses) {
                            String rawJSON = TwitterObjectFactory.getRawJSON(status);
                            writer.println(rawJSON);

                            totalTweets += 1;
                            if (totalTweets >= NUMBER_OF_TWEETS) {
                                tweetCounterReached = true;
                                break;
                            }
                        }
                        if (tweetCounterReached) {
                            break;
                        }
                    }
                }

                // If user's total tweet are less than 195 then no next call
                if (size == 0) {
                    if (totalTweets < 195) {
                        break;
                    }
                }

                // If user's all tweets parsed then exit
                if (totalTweets == size) {
                    break;
                }

                size = totalTweets;

                statuses.clear();

            } catch (TwitterException e) {

                // e.printStackTrace();
                // do not throw if user has protected tweets, or if they
                // deleted their account
                if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                        || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                    System.out.println(targetedUser + " is protected or account is deleted");
                } else {
                    System.out.println("Tweets Get Exception: " + e.getMessage());
                }

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    System.out.println("No more remianing calls");
                }

                if (totalTweets < 1) {
                    writer.close();
                    // Remove file if tweets not found
                    File fileToDelete = new File(fileName);
                    fileToDelete.delete();
                }
                break;
            }

            // If rate limit reached then switch Auth user
            RemainingCallsCounter++;
            if (RemainingCallsCounter >= RemainingCalls) {

                System.out.println("No more remianing calls");
                break;
            }

        } // while get tweets
        writer.close();

        if (totalTweets > 0) {
            System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets);
        } else {

            // Remove file if tweets not found
            File fileToDelete = new File(fileName);
            fileToDelete.delete();
        }

    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get followers' ids: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");
}

From source file:timeline.DBNonThreader.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length == 0)) {
        System.err.println("First: 'OUTPUT_PATH' is mendatory.");
        System.err.println("Second: (int) Number of Tweets to get. Max 3200");
        System.err.println("Third: 'screen_name / id_str'" + " is optional.");
        System.err.println("If 3rd argument not provided then provide" + " Twitter users through database.");

        System.exit(-1);/*from   w  w  w.  ja  va2 s  .com*/
    }

    MysqlDB DB = new MysqlDB();
    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/statuses/user_timeline";

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    int NUMBER_OF_TWEETS = 3200;
    try {
        NUMBER_OF_TWEETS = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an integer.");
        System.exit(-1);
    }

    String targetedUser = "";
    if (args.length == 3) {
        try {
            targetedUser = StringEscapeUtils.escapeJava(args[2]);
        } catch (Exception e) {
            System.err.println("Argument" + args[2] + " must be an String.");
            System.exit(-1);
        }
    }

    try {

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("First Time Remianing Calls: " + RemainingCalls);

        String Screen_name = AppOAuths.screen_name;
        System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name);

        System.out.println("User's Tweets");

        // If targetedUser not provided by argument, then look into
        // database.
        if (StringUtils.isEmpty(targetedUser)) {

            String selectQuery = "SELECT `id`,`targeteduser` FROM " + "`twitter_users` WHERE "
                    + "`tweets_dumped_all` = 0";

            ResultSet results = DB.selectQ(selectQuery);

            int numRows = DB.numRows(results);
            if (numRows < 1) {
                System.err.println("No User in database to get Tweets");
                System.exit(-1);
            }

            System.out.println("Trying to create output directory");
            String filesPath = OutputDirPath + "/";
            File theDir = new File(filesPath);

            // If the directory does not exist, create it
            if (!theDir.exists()) {

                try {
                    theDir.mkdirs();

                } catch (SecurityException se) {

                    System.err.println("Could not create output " + "directory: " + OutputDirPath);
                    System.err.println(se.getMessage());
                    System.exit(-1);
                }
            }

            OUTERMOST: while (results.next()) {

                int targetedUserID = results.getInt("id");
                targetedUser = results.getString("targeteduser");
                System.out.println("Targeted User: " + targetedUser);

                String fileName = filesPath + "/" + targetedUser;
                PrintWriter writer = new PrintWriter(fileName, "UTF-8");

                // Call different functions for screen_name and id_str
                Boolean chckedNumaric = helpers.isNumeric(targetedUser);

                List<Status> statuses = new ArrayList<>();
                int size = statuses.size();
                int pageno = 1;
                int totalTweets = 0;
                boolean tweetCounterReached = false;
                System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS);
                while (true) {

                    try {

                        Paging page = new Paging(pageno++, 200);

                        if (chckedNumaric) {

                            long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                            statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page));

                            if (statuses.size() > 0) {
                                for (Status status : statuses) {
                                    String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                    writer.println(rawJSON);

                                    totalTweets += 1;
                                    if (totalTweets >= NUMBER_OF_TWEETS) {
                                        tweetCounterReached = true;
                                        break;
                                    }
                                }
                                if (tweetCounterReached) {
                                    break;
                                }
                            }
                        } else {

                            statuses.addAll(twitter.getUserTimeline(targetedUser, page));

                            if (statuses.size() > 0) {
                                for (Status status : statuses) {
                                    String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                    writer.println(rawJSON);

                                    totalTweets += 1;
                                    if (totalTweets >= NUMBER_OF_TWEETS) {
                                        tweetCounterReached = true;
                                        break;
                                    }
                                }
                                if (tweetCounterReached) {
                                    break;
                                }
                            }
                        }

                        // If user's total tweet are less than 195 
                        // then no next call
                        if (size == 0) {
                            if (totalTweets < 195) {
                                break;
                            }
                        }

                        // If user's all tweets parsed 
                        // then proceed to next user 
                        if (totalTweets == size) {
                            break;
                        }

                        size = totalTweets;

                        statuses.clear();

                    } catch (TwitterException e) {

                        // e.printStackTrace();
                        // do not throw if user has protected tweets, 
                        // or if they deleted their account
                        if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                                || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                            System.out.println(targetedUser + " is protected or account is deleted");
                        } else {
                            System.out.println("Tweets Get Exception: " + e.getMessage());
                        }

                        // If rate limit reached then switch Auth user
                        RemainingCallsCounter++;
                        if (RemainingCallsCounter >= RemainingCalls) {

                            // Load OAuth user
                            tf = AppOAuths.loadOAuthUser(endpoint);
                            twitter = tf.getInstance();

                            System.out.println(
                                    "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name);

                            RemainingCalls = AppOAuths.RemainingCalls - 2;
                            RemainingCallsCounter = 0;

                            System.out.println("New Remianing Calls: " + RemainingCalls);
                        }

                        if (totalTweets < 1) {
                            writer.close();
                            // Remove file if tweets not found
                            File fileToDelete = new File(fileName);
                            fileToDelete.delete();
                        }

                        // Update stats
                        String fieldValues = "`tweets_dumped_all` = 2";
                        String where = "id = " + targetedUserID;
                        DB.Update("`twitter_users`", fieldValues, where);
                        continue OUTERMOST;

                    }

                    // If rate limit reached then switch Auth user
                    RemainingCallsCounter++;
                    if (RemainingCallsCounter >= RemainingCalls) {

                        // Load OAuth user
                        tf = AppOAuths.loadOAuthUser(endpoint);
                        twitter = tf.getInstance();

                        System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                        RemainingCalls = AppOAuths.RemainingCalls - 2;
                        RemainingCallsCounter = 0;

                        System.out.println("New Remianing Calls: " + RemainingCalls);
                    }
                } // while get tweets
                writer.close();

                if (totalTweets > 0) {

                    System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets);

                    // Update stats
                    String fieldValues = "`tweets_dumped_all` = 1, " + " `tweets_all_count` = " + totalTweets;
                    String where = "id = " + targetedUserID;
                    DB.Update("`twitter_users`", fieldValues, where);
                } else {

                    // Update stats
                    String fieldValues = "`tweets_dumped_all` = 2";
                    String where = "id = " + targetedUserID;
                    DB.Update("`twitter_users`", fieldValues, where);

                    // Remove file if tweets not found
                    File fileToDelete = new File(fileName);
                    fileToDelete.delete();
                }

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    // Load OAuth user
                    tf = AppOAuths.loadOAuthUser(endpoint);
                    twitter = tf.getInstance();

                    System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                    RemainingCalls = AppOAuths.RemainingCalls - 2;
                    RemainingCallsCounter = 0;

                    System.out.println("New Remianing Calls: " + RemainingCalls);
                }

            } // while get users from database

        } else {
            // screen_name / user_id provided by arguments
            System.out.println("screen_name / user_id provided by arguments");

            System.out.println("Trying to create output directory");
            String filesPath = OutputDirPath + "/";
            File theDir = new File(filesPath);

            // If the directory does not exist, create it
            if (!theDir.exists()) {

                try {
                    theDir.mkdirs();

                } catch (SecurityException se) {

                    System.err.println("Could not create output " + "directory: " + OutputDirPath);
                    System.err.println(se.getMessage());
                    System.exit(-1);
                }
            }

            System.out.println("Targeted User: " + targetedUser);

            String fileName = filesPath + "/" + targetedUser;
            PrintWriter writer = new PrintWriter(fileName, "UTF-8");

            // Call different functions for screen_name and id_str
            Boolean chckedNumaric = helpers.isNumeric(targetedUser);

            List<Status> statuses = new ArrayList<>();
            int size = statuses.size();
            int pageno = 1;
            int totalTweets = 0;
            boolean tweetCounterReached = false;
            System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS);
            while (true) {

                try {

                    Paging page = new Paging(pageno++, 200);

                    if (chckedNumaric) {

                        long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                        statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page));

                        if (statuses.size() > 0) {
                            for (Status status : statuses) {
                                String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                writer.println(rawJSON);

                                totalTweets += 1;
                                if (totalTweets >= NUMBER_OF_TWEETS) {
                                    tweetCounterReached = true;
                                    break;
                                }
                            }
                            if (tweetCounterReached) {
                                break;
                            }
                        }
                    } else {

                        statuses.addAll(twitter.getUserTimeline(targetedUser, page));

                        if (statuses.size() > 0) {
                            for (Status status : statuses) {
                                String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                writer.println(rawJSON);

                                totalTweets += 1;
                                if (totalTweets >= NUMBER_OF_TWEETS) {
                                    tweetCounterReached = true;
                                    break;
                                }
                            }
                            if (tweetCounterReached) {
                                break;
                            }
                        }
                    }

                    // If user's total tweet are less than 195 then no next call
                    if (size == 0) {
                        if (totalTweets < 195) {
                            break;
                        }
                    }

                    // If user's all tweets parsed then proceed to next user 
                    if (totalTweets == size) {
                        break;
                    }

                    size = totalTweets;

                    statuses.clear();

                } catch (TwitterException e) {

                    // e.printStackTrace();
                    // do not throw if user has protected tweets, or if they deleted their account
                    if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                            || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                        System.out.println(targetedUser + " is protected or account is deleted");
                    } else {
                        System.out.println("Tweets Get Exception: " + e.getMessage());
                    }

                    // If rate limit reached then switch Auth user
                    RemainingCallsCounter++;
                    if (RemainingCallsCounter >= RemainingCalls) {

                        // Load OAuth user
                        tf = AppOAuths.loadOAuthUser(endpoint);
                        twitter = tf.getInstance();

                        System.out.println("New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name);

                        RemainingCalls = AppOAuths.RemainingCalls - 2;
                        RemainingCallsCounter = 0;

                        System.out.println("New Remianing Calls: " + RemainingCalls);
                    }

                    if (totalTweets < 1) {
                        writer.close();
                        // Remove file if tweets not found
                        File fileToDelete = new File(fileName);
                        fileToDelete.delete();
                    }
                }

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    // Load OAuth user
                    tf = AppOAuths.loadOAuthUser(endpoint);
                    twitter = tf.getInstance();

                    System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                    RemainingCalls = AppOAuths.RemainingCalls - 2;
                    RemainingCallsCounter = 0;

                    System.out.println("New Remianing Calls: " + RemainingCalls);
                }

            } // while get tweets
            writer.close();

            if (totalTweets > 0) {
                System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets);
            } else {

                // Remove file if tweets not found
                File fileToDelete = new File(fileName);
                fileToDelete.delete();
            }

        } // screen_name / user_id provided by arguments

    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get followers' ids: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");
}

From source file:timeline.DBThreaderParser.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length < 5)) {
        System.err.println("5 Parameters are required to launch a Job.");
        System.err.println("First: String 'OUTPUT_PATH'");
        System.err.println("Second: (int) Total Number Of Jobs");
        System.err.println("Third: (int) This Job Number");
        System.err.println("Fourth: (int) Seconds to pause between next launch");
        System.err.println("Fifth: (int) Number of Tweets to get. Max 3200");
        System.err.println("Example: fileName.class /output/path 10 1 2 3200");
        System.exit(-1);//  ww  w  .  ja va 2  s.  c o m
    }

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(1);
    }

    int TOTAL_JOBS = 0;
    try {
        TOTAL_JOBS = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[1] + " must be an integer.");
        System.exit(1);
    }

    int JOB_NO = 0;
    try {
        JOB_NO = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[2] + " must be an integer.");
        System.exit(1);
    }

    int NUMBER_OF_TWEETS = 0;
    try {
        NUMBER_OF_TWEETS = Integer.parseInt(args[4]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[4] + " must be an integer.");
        System.exit(1);
    }

    MysqlDB DB = new MysqlDB();
    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/statuses/user_timeline";

    try {

        String totalRowsQuery = "SELECT count( * ) as wLoad FROM `twitter_users`"
                + " WHERE `tweets_dumped_all` = 0";
        ResultSet totalRowsResults = DB.selectQ(totalRowsQuery);

        int TotalWorkLoad = 0;
        while (totalRowsResults.next()) {
            TotalWorkLoad = totalRowsResults.getInt("wLoad");
        }

        if (TotalWorkLoad < 1) {
            System.err.println("No User in database to get Tweets");
            System.exit(-1);
        }

        // Free memmory
        totalRowsResults = null;

        if (TOTAL_JOBS > TotalWorkLoad) {
            System.err.println("Number of jobs are more than total work"
                    + " load Please reduce Number of jobs to launch.");
            System.exit(-1);
        }

        float TotalWorkLoadf = TotalWorkLoad;
        float TOTAL_JOBSf = TOTAL_JOBS;
        float res = (TotalWorkLoadf / TOTAL_JOBSf);

        int chunkSize = (int) Math.ceil(res);
        int offSet = JOB_NO * chunkSize;

        String selectQuery = "SELECT `id`,`targeteduser` FROM " + "`twitter_users` WHERE "
                + "`tweets_dumped_all` = 0 LIMIT " + offSet + "," + chunkSize;

        ResultSet results = DB.selectQ(selectQuery);

        int numRows = DB.numRows(results);
        if (numRows < 1) {
            System.err.println(
                    "No User in database to get Tweets" + " with offset " + offSet + " and limit " + chunkSize);
            System.exit(-1);
        }

        /**
         * wait before launching actual job
         */
        int secondsToPause = 0;
        try {
            secondsToPause = Integer.parseInt(args[3]);
        } catch (NumberFormatException e) {
            System.err.println("Argument" + args[3] + " must be an integer.");
            System.exit(-1);
        }

        secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause);
        System.out.println("secondsToPause: " + secondsToPause);
        helpers.pause(secondsToPause);
        /**
         * wait before launching actual job
         */

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("First Time Remianing Calls: " + RemainingCalls);

        String Screen_name = AppOAuths.screen_name;
        System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name);

        System.out.println("User's Tweets");

        System.out.println("Trying to create output directory");
        String filesPath = OutputDirPath + "/";
        File theDir = new File(filesPath);

        // If the directory does not exist, create it
        if (!theDir.exists()) {
            try {
                theDir.mkdirs();

            } catch (SecurityException se) {

                System.err.println("Could not create output " + "directory: " + OutputDirPath);
                System.err.println(se.getMessage());
                System.exit(-1);
            }
        }

        System.out.flush();

        OUTERMOST: while (results.next()) {

            int targetedUserID = results.getInt("id");
            String targetedUser = results.getString("targeteduser");
            System.out.println("Targeted User: " + targetedUser);

            // Create User file to push tweets in it
            String fileName = filesPath + "/" + targetedUser;
            PrintWriter writer = new PrintWriter(fileName, "UTF-8");

            // Call different functions for screen_name and id_str
            Boolean chckedNumaric = helpers.isNumeric(targetedUser);

            List<Status> statuses = new ArrayList<>();
            int size = statuses.size();
            int pageno = 1;
            int totalTweets = 0;
            boolean tweetCounterReached = false;
            System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS);
            while (true) {
                try {

                    Paging page = new Paging(pageno++, 200);

                    if (chckedNumaric) {

                        long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                        statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page));

                        if (statuses.size() > 0) {
                            for (Status status : statuses) {
                                String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                writer.println(rawJSON);

                                totalTweets += 1;
                                if (totalTweets >= NUMBER_OF_TWEETS) {
                                    tweetCounterReached = true;
                                    break;
                                }
                            }
                            if (tweetCounterReached) {
                                break;
                            }
                        }
                    } else {

                        statuses.addAll(twitter.getUserTimeline(targetedUser, page));

                        if (statuses.size() > 0) {
                            for (Status status : statuses) {
                                String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                writer.println(rawJSON);

                                totalTweets += 1;
                                if (totalTweets >= NUMBER_OF_TWEETS) {
                                    tweetCounterReached = true;
                                    break;
                                }
                            }
                            if (tweetCounterReached) {
                                break;
                            }
                        }
                    }

                    // If user's total tweet are less 
                    // than 195 then no next call
                    if (size == 0) {
                        if (totalTweets < 195) {
                            break;
                        }
                    }

                    // If user's all tweets parsed 
                    // then proceed to next user 
                    if (totalTweets == size) {
                        break;
                    }

                    size = totalTweets;

                    statuses.clear();

                } catch (TwitterException e) {

                    // e.printStackTrace();
                    // do not throw if user has protected tweets, 
                    // or if they deleted their account
                    if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                            || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                        System.out.println(targetedUser + " is protected or account is deleted");
                    } else {
                        System.out.println("Tweets Get Exception: " + e.getMessage());
                    }

                    // If rate limit reached then switch Auth user
                    RemainingCallsCounter++;
                    if (RemainingCallsCounter >= RemainingCalls) {

                        // Load OAuth user
                        tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                        twitter = tf.getInstance();

                        System.out.println("New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name);

                        RemainingCalls = AppOAuths.RemainingCalls - 2;
                        RemainingCallsCounter = 0;

                        System.out.println("New Remianing Calls: " + RemainingCalls);
                    }

                    if (totalTweets < 1) {
                        writer.close();
                        // Remove file if tweets not found
                        File fileToDelete = new File(fileName);
                        fileToDelete.delete();
                    }

                    // Update stats
                    String fieldValues = "`tweets_dumped_all` = 2";
                    String where = "id = " + targetedUserID;
                    DB.Update("`twitter_users`", fieldValues, where);
                    continue OUTERMOST;

                }

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    // Load OAuth user
                    tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                    twitter = tf.getInstance();

                    System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                    RemainingCalls = AppOAuths.RemainingCalls - 2;
                    RemainingCallsCounter = 0;

                    System.out.println("New Remianing Calls: " + RemainingCalls);
                }

            } // while get tweets
            writer.close();

            if (totalTweets > 0) {

                System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets);
                // Update stats
                String fieldValues = "`tweets_dumped_all` = 1, " + " `tweets_all_count` = " + totalTweets;
                String where = "id = " + targetedUserID;
                DB.Update("`twitter_users`", fieldValues, where);
            } else {

                // Update stats
                String fieldValues = "`tweets_dumped_all` = 2, " + " `tweets_all_count` = " + totalTweets;
                String where = "id = " + targetedUserID;
                DB.Update("`twitter_users`", fieldValues, where);

                // Remove file if tweets not found
                File fileToDelete = new File(fileName);
                fileToDelete.delete();
            }

            // If rate limit reached then switch Auth user
            RemainingCallsCounter++;
            if (RemainingCallsCounter >= RemainingCalls) {

                // Load OAuth user
                tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                twitter = tf.getInstance();

                System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                RemainingCalls = AppOAuths.RemainingCalls - 2;
                RemainingCallsCounter = 0;

                System.out.println("New Remianing Calls: " + RemainingCalls);
            }

            System.out.flush();
        } // while get users from database
    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");

    // Close System.out for this thread which will
    // flush and close this thread.
    System.out.close();
}

From source file:timeline.FilesThreaderParser.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length < 7)) {
        System.err.println("7 Parameters are required to launch a Job.");
        System.err.println("First: String 'INPUT: " + "/path/to/perline/screen_names/files/'");
        System.err.println("Second: String 'OUTPUT_PATH'");
        System.err.println("Third: (int) Total Number Of Jobs");
        System.err.println("Fourth: (int) This Job Number");
        System.err.println("Fifth: (int) Seconds to pause between " + "next launch");
        System.err.println("Sixth: (int) Number of Tweets to get. Max 3200");
        System.err.println("Name of current threader.");
        System.err.println("Example: fileName.class /output/path " + "10 2 3200 pool-1-thread-1");
        System.exit(-1);//from w ww .  j  a va 2  s .c o m
    }

    String inputPath = null;
    try {
        inputPath = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument " + args[0] + " must be an String.");
        System.exit(-1);
    }

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(1);
    }

    int TOTAL_JOBS = 0;
    try {
        TOTAL_JOBS = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[2] + " must be an integer.");
        System.exit(1);
    }

    int JOB_NO = 0;
    try {
        JOB_NO = Integer.parseInt(args[3]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[3] + " must be an integer.");
        System.exit(1);
    }

    int NUMBER_OF_TWEETS = 0;
    try {
        NUMBER_OF_TWEETS = Integer.parseInt(args[5]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[5] + " must be an integer.");
        System.exit(1);
    }

    String ThreadName = null;
    try {
        ThreadName = StringEscapeUtils.escapeJava(args[6]);
    } catch (Exception e) {
        System.err.println("Argument" + args[6] + " must be an String.");
        System.exit(1);
    }

    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/statuses/user_timeline";

    try {

        int TotalWorkLoad = 0;
        ArrayList<String> allFiles = null;
        try {
            final File folder = new File(inputPath);
            allFiles = helpers.listFilesForSingleFolder(folder);
            TotalWorkLoad = allFiles.size();
        } catch (Exception e) {

            System.err.println("Input folder is not exists: " + e.getMessage());
            System.exit(-1);
        }

        System.out.println("Total Workload is: " + TotalWorkLoad);

        if (TotalWorkLoad < 1) {
            System.err.println("No input file exists in: " + inputPath);
            System.exit(-1);
        }

        if (TOTAL_JOBS > TotalWorkLoad) {
            System.err.println("Number of jobs are more than total work"
                    + " load. Please reduce 'Number of jobs' to launch.");
            System.exit(-1);
        }

        float TotalWorkLoadf = TotalWorkLoad;
        float TOTAL_JOBSf = TOTAL_JOBS;
        float res = (TotalWorkLoadf / TOTAL_JOBSf);

        int chunkSize = (int) Math.ceil(res);
        int offSet = JOB_NO * chunkSize;
        int chunkSizeToGet = (JOB_NO + 1) * chunkSize;

        System.out.println("My Share is " + chunkSize);
        System.out.println("My offSet is " + offSet);
        System.out.println("My chunkSizeToGet is " + chunkSizeToGet);
        System.out.println();

        /**
         * wait before launching actual job
         */
        int secondsToPause = 0;
        try {
            secondsToPause = Integer.parseInt(args[4]);
        } catch (NumberFormatException e) {
            System.err.println("Argument" + args[4] + " must be an integer.");
            System.exit(-1);
        }

        secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause);
        System.out.println("secondsToPause: " + secondsToPause);
        helpers.pause(secondsToPause);
        /**
         * wait before launching actual job
         */

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("First Time Remianing Calls: " + RemainingCalls);

        String Screen_name = AppOAuths.screen_name;
        System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name);

        System.out.println("User's Tweets");

        System.out.println("Trying to create output directory");

        int dirCounter = 1;
        int fileCounter = 0;

        String filesPath = OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter;
        File theDir = new File(filesPath);

        // If the directory does not exist, create it
        if (!theDir.exists()) {
            try {
                theDir.mkdirs();

            } catch (SecurityException se) {

                System.err.println("Could not create output " + "directory: " + OutputDirPath + "/" + ThreadName
                        + "-tweets-" + dirCounter);
                System.err.println(se.getMessage());
                System.exit(-1);
            }
        }

        if (JOB_NO + 1 == TOTAL_JOBS) {
            chunkSizeToGet = TotalWorkLoad;
        }

        // to write output in a file
        System.out.flush();

        List<String> myFilesShare = allFiles.subList(offSet, chunkSizeToGet);

        for (String myFile : myFilesShare) {
            System.out.println("Going to parse file: " + myFile);
            System.out.println();

            try (BufferedReader br = new BufferedReader(new FileReader(inputPath + "/" + myFile))) {
                String line;

                OUTERMOST: while ((line = br.readLine()) != null) {

                    if (fileCounter >= 30000) {

                        dirCounter++;
                        fileCounter = 0;
                        filesPath = OutputDirPath + "/" + ThreadName + "-tweets-" + dirCounter;
                        theDir = new File(filesPath);

                        // If the directory does not exist, create it
                        if (!theDir.exists()) {
                            try {
                                theDir.mkdirs();

                            } catch (SecurityException se) {

                                System.err.println("Could not create output " + "directory: " + OutputDirPath
                                        + "/" + ThreadName + "-tweets-" + dirCounter);
                                System.err.println(se.getMessage());
                                System.exit(-1);
                            }
                        }
                    }

                    System.out.println("Going to get tweets of " + "Screen-name / user_id: " + line);

                    String targetedUser = line.trim();
                    System.out.println("Targeted User: " + targetedUser);

                    // Create User file to push tweets in it
                    String fileName = filesPath + "/" + targetedUser;
                    PrintWriter writer = new PrintWriter(fileName, "UTF-8");

                    // Call different functions for screen_name and id_str
                    Boolean chckedNumaric = helpers.isNumeric(targetedUser);

                    List<Status> statuses = new ArrayList<>();
                    int size = statuses.size();
                    int pageno = 1;
                    int totalTweets = 0;
                    boolean tweetCounterReached = false;
                    System.out.println("NUMBER_OF_TWEETS to get:" + NUMBER_OF_TWEETS);
                    System.out.println();

                    while (true) {
                        try {

                            Paging page = new Paging(pageno++, 200);

                            if (chckedNumaric) {

                                long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                                statuses.addAll(twitter.getUserTimeline(LongValueTargetedUser, page));

                                if (statuses.size() > 0) {
                                    for (Status status : statuses) {
                                        String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                        writer.println(rawJSON);

                                        totalTweets += 1;
                                        if (totalTweets >= NUMBER_OF_TWEETS) {
                                            tweetCounterReached = true;
                                            break;
                                        }
                                    }
                                    if (tweetCounterReached) {
                                        break;
                                    }
                                }
                            } else {

                                statuses.addAll(twitter.getUserTimeline(targetedUser, page));

                                if (statuses.size() > 0) {
                                    for (Status status : statuses) {
                                        String rawJSON = TwitterObjectFactory.getRawJSON(status);
                                        writer.println(rawJSON);

                                        totalTweets += 1;
                                        if (totalTweets >= NUMBER_OF_TWEETS) {
                                            tweetCounterReached = true;
                                            break;
                                        }
                                    }
                                    if (tweetCounterReached) {
                                        break;
                                    }
                                }
                            }

                            // If user's total tweet are less 
                            // than 195 then no next call
                            if (size == 0) {
                                if (totalTweets < 195) {
                                    break;
                                }
                            }

                            // If user's all tweets parsed 
                            // then proceed to next user 
                            if (totalTweets == size) {
                                break;
                            }

                            size = totalTweets;

                            statuses.clear();

                        } catch (TwitterException e) {

                            // e.printStackTrace();
                            // do not throw if user has protected tweets, 
                            // or if they deleted their account
                            if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                                    || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                                System.out.println(targetedUser + " is protected or account is deleted");
                            } else {
                                System.out.println("Tweets Get Exception: " + e.getMessage());
                            }

                            // If rate limit reached then switch Auth user
                            RemainingCallsCounter++;
                            if (RemainingCallsCounter >= RemainingCalls) {

                                // Load OAuth user
                                tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                                twitter = tf.getInstance();

                                System.out.println(
                                        "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name);

                                RemainingCalls = AppOAuths.RemainingCalls - 2;
                                RemainingCallsCounter = 0;

                                System.out.println("New Remianing Calls: " + RemainingCalls);
                            }

                            if (totalTweets < 1) {
                                writer.close();
                                // Remove file if tweets not found
                                File fileToDelete = new File(fileName);
                                fileToDelete.delete();
                            }

                            continue OUTERMOST;

                        }

                        // If rate limit reached then switch Auth user
                        RemainingCallsCounter++;
                        if (RemainingCallsCounter >= RemainingCalls) {

                            // Load OAuth user
                            tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                            twitter = tf.getInstance();

                            System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                            RemainingCalls = AppOAuths.RemainingCalls - 2;
                            RemainingCallsCounter = 0;

                            System.out.println("New Remianing Calls: " + RemainingCalls);
                        }

                    } // while get tweets
                    writer.close();

                    if (totalTweets > 0) {
                        fileCounter++;
                        System.out.println("Total dumped tweets of " + targetedUser + " are: " + totalTweets);

                    } else {

                        // Remove file if tweets not found
                        File fileToDelete = new File(fileName);
                        fileToDelete.delete();
                    }

                    // If rate limit reached then switch Auth user
                    RemainingCallsCounter++;
                    if (RemainingCallsCounter >= RemainingCalls) {

                        // Load OAuth user
                        tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                        twitter = tf.getInstance();

                        System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name);

                        RemainingCalls = AppOAuths.RemainingCalls - 2;
                        RemainingCallsCounter = 0;

                        System.out.println("New Remianing Calls: " + RemainingCalls);
                    }

                } // while get users

            } // read my single file
            catch (IOException e) {
                System.err.println("Failed to read lines from " + myFile);
            }

            File currentFile = new File(inputPath + "/" + myFile);
            currentFile.delete();

            // to write output in a file
            System.out.flush();
        } // all my files share
    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");

    // Close System.out for this thread which will
    // flush and close this thread.
    System.out.close();
}

From source file:tiofortwitter.TioForTwitter.java

/**
 * @param args the command line arguments
 *///from   ww  w. jav  a  2s  . co  m
public static void main(String[] args) throws JSONException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH")
            .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK")
            .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA")
            .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    JSONObject obj = new JSONObject();
    int counterTweet = 0;
    FileWriter file = new FileWriter("Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt");
    file.flush();
    try {
        Query query = new Query("Satria");
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                counterTweet++;
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                obj.put("user", tweet.getUser().getScreenName());
                obj.put("tweets", tweet.getText());

                //Tulis file ke dalam txt

                try {
                    file.write(obj.toString());
                    System.out.println("Successfully Copied JSON Object to File...");
                    System.out.println("\nJSON Object: " + obj);

                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        } while (counterTweet < 1000);

        file.close();
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}

From source file:tkwatch.ItemAddPanel.java

License:Open Source License

/**
 * Constructor with argument. Needs the calling panel as an argument,
 * because this will update the list of symbols on add or delete.
 * //  w ww . j av  a2s. c  o m
 * @param theTab
 *            The panel that spawned this panel.
 */
public ItemAddPanel(final WatchlistPanel theTab) {
    // The panel for instrument, cost basis, and quantity.
    final JPanel topPanel = new JPanel(new GridBagLayout());
    topPanel.add(instrumentLabel, Utilities.getConstraints(0, 0, 1, 1, GridBagConstraints.CENTER));
    topPanel.add(instrumentField, Utilities.getConstraints(0, 1, 1, 1, GridBagConstraints.CENTER));
    topPanel.add(costBasisLabel, Utilities.getConstraints(1, 0, 1, 1, GridBagConstraints.CENTER));
    topPanel.add(costBasisField, Utilities.getConstraints(1, 1, 1, 1, GridBagConstraints.CENTER));
    topPanel.add(quantityLabel, Utilities.getConstraints(2, 0, 1, 1, GridBagConstraints.CENTER));
    topPanel.add(quantityField, Utilities.getConstraints(2, 1, 1, 1, GridBagConstraints.CENTER));

    // The panel for notation.
    JPanel notationPanel = new JPanel(new BorderLayout());
    notationPanel.add(notationLabel, BorderLayout.NORTH);
    notationPanel.add(notationFieldScrollPane, BorderLayout.CENTER);

    // The panel for the add and cancel buttons.
    JPanel buttonPanel = new JPanel(new GridBagLayout());
    buttonAdd.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            final String newInstrument = instrumentField.getText();
            if (newInstrument.isEmpty()) {
                Utilities.errorMessage("Instrument can't be empty.");
                return;
            }
            final String newCostString = costBasisField.getText();
            if (newCostString.isEmpty()) {
                Utilities.errorMessage("Cost basis can't be empty.");
                return;
            }
            final double newCostBasis = Double.parseDouble(newCostString);
            final String newNotation = notationField.getText();
            final String newQuantityString = quantityField.getText();
            if (newQuantityString.isEmpty()) {
                Utilities.errorMessage("Quantity can't be empty.");
                return;
            }
            final double newQuantity = Double.parseDouble(newQuantityString);
            final WatchlistItem newItem = new WatchlistItem(newCostBasis, newInstrument, newNotation,
                    newQuantity);
            final String newItemInstrument = newItem.getInstrument();
            if (newItem.isStored()) {
                Utilities.warningMessage("You are already watching " + newItemInstrument);
                return;
            }
            if (!newItem.store()) {
                Utilities.errorMessage("Could not store new item.");
            }

            theTab.instrumentListModel.addElement(newItemInstrument);
            if (theTab.tweet.isSelected()) {
                Twitter twitter = new TwitterFactory().getInstance();
                final String update;
                try {
                    update = "Now watching " + newItemInstrument + " on @TradeKing.";
                    twitter.updateStatus(update);
                } catch (TwitterException e1) {
                    Utilities.errorMessage(e1.getMessage());
                }
            }
            synchWatchlistWithTk();
            Utilities.closeFrame(buttonAdd);
        }
    });
    buttonPanel.add(buttonAdd, Utilities.getConstraints(0, 0, 1, 1, GridBagConstraints.CENTER));
    buttonCancel.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            Utilities.closeFrame(buttonCancel);
        }
    });
    buttonPanel.add(buttonCancel, Utilities.getConstraints(1, 0, 1, 1, GridBagConstraints.CENTER));

    // The status panel.
    final JPanel bottomPanel = Utilities.getStatusPanel();

    // The panel to hold topPanel, notationPanel, and buttonPanel
    final JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(notationPanel, BorderLayout.CENTER);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    setLayout(new BorderLayout());
    add(mainPanel, BorderLayout.CENTER);
    add(bottomPanel, BorderLayout.SOUTH);
}