Example usage for twitter4j TwitterFactory getInstance

List of usage examples for twitter4j TwitterFactory getInstance

Introduction

In this page you can find the example usage for twitter4j TwitterFactory getInstance.

Prototype

public Twitter getInstance() 

Source Link

Document

Returns a instance associated with the configuration bound to this factory.

Usage

From source file:tiofortwitter.TioForTwitter.java

/**
 * @param args the command line arguments
 *///from   w w  w . j a va  2  s.  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");
    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:tokyo.raysn.hanaasagi.app.OAuthInfo.java

public OAuthInfo(OAuthWindow authinfo) {
    ConsumerKey ck = new ConsumerKey();
    //OAuth????//w w  w .  jav a  2  s  .c o m
    oAuthWindow = authinfo;

    //cb.setDebugEnabled(true)
    //      .setOAuthConsumerKey(ck.getConsumerKey(0))
    //      .setOAuthConsumerSecret(ck.getConsumerSecret(0));
    //TwitterFactory tf = new TwitterFactory(cb.build());

    TwitterFactory tf = new TwitterFactory();
    twitter = tf.getInstance();

    //CK/CS
    twitter.setOAuthConsumer(ck.getConsumerKey(0), ck.getConsumerSecret(0));
    try {
        requestToken = twitter.getOAuthRequestToken();
    } catch (TwitterException te) {
        te.printStackTrace();
        System.exit(1);
    }
    if (oAuthWindow != null) {
        oAuthWindow.setOAuthUrlOnWindow(requestToken.getAuthorizationURL());
    }
}

From source file:tokyo.ryogo.dropkick.sns.twitter.DKTwitter.java

License:Apache License

public static Twitter getTwitterInstance(Context context) {
    String consumerKey = TwitterSecret.getTwitterConsumerKey(context.getString(R.string.cryption));
    String consumerSecret = TwitterSecret.getTwitterConsumerKeySecret(context.getString(R.string.cryption));

    TwitterFactory factory = new TwitterFactory();
    Twitter twitter = factory.getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);

    if (hasAccessToken(context)) {
        twitter.setOAuthAccessToken(loadAccessToken(context));
    }//  ww w  . j a v a2  s  .c om
    return twitter;
}

From source file:toninbot.ToninStatusListener.java

public ToninStatusListener() {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(Credenciales.consumerKey);
    builder.setOAuthConsumerSecret(Credenciales.consumerSecret);

    Configuration configuration = builder.build();
    AccessToken accessToken = new AccessToken(Credenciales.token, Credenciales.tokenSecret);

    TwitterFactory twFactory = new TwitterFactory(configuration);
    twitter = twFactory.getInstance();
    twitter.setOAuthAccessToken(accessToken);

    randomGenerator = new Random();

    //La lista de tweets
    respuestas = new ArrayList();
    respuestas.add("Fora de horario! Este tweet esta fora de horario!");
    respuestas.add("Eeeeh!! Estas fora de horario!");
    respuestas.add("FORA DE HORARIO!");
}

From source file:traffickarmasent.newgetpage.java

public static void main(String[] args) throws IOException {
    // gets Twitter instance with default credentials
    Twitter twitter = new TwitterFactory().getInstance();
    try {/*from   w w w. j a  v a2 s.  c o m*/
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("GPtsu5cjC08KTOEojEoaHw");
        cb.setOAuthConsumerSecret("SsgeXn73bN4CXUYtJfEdKOwBxVTmAEPvmFo3q2CX45w");
        cb.setOAuthAccessToken("154196958-J1Gqy86jmQ6YSoFVVq69bmbJB0acGxiDEocxtvre");
        cb.setOAuthAccessTokenSecret("DpTJr3huuDy2qMwsCMgsTn5yNbi0oQzSDGhDDWQsLog");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter1 = tf.getInstance();
        List<Status> statuses;
        String user;
        String[] users = { "livetrafficsyd", "trafficnsw", "sydtraffic_cs", "WazeTrafficSYD",
                "livetrafficnsw" };
        Date[] d_users = { new Date(99, 2, 12), new Date(99, 2, 12), new Date(99, 2, 12), new Date(99, 2, 12),
                new Date(99, 2, 12) };

        while (true) {
            for (int i = 0; i < users.length; i++) {

                statuses = twitter1.getUserTimeline(users[i]);

                for (int j = statuses.size() - 1; j >= 0; j--) {
                    Status st = statuses.get(j);
                    if (d_users[i].before(st.getCreatedAt())) {
                        String message = removeUrl(st.getText());

                        File file = new File("out_sydney_new.txt");

                        //if file doesnt exists, then create it
                        if (!file.exists()) {
                            file.createNewFile();
                        }

                        //true = append file
                        FileWriter fileWritter = new FileWriter(file.getName(), true);
                        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
                        bufferWritter.write(message + "\n");
                        bufferWritter.close();

                        System.out.println("Done");

                        System.out.println("@" + st.getUser().getScreenName() + " - " + st.getText());
                        d_users[i] = st.getCreatedAt();
                    }
                }
            }
            try {
                Thread.sleep(300000); //1000 milliseconds is one second.
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("firse");

        }

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:tweekan.kotikan.com.tweekan.twitter.BasicTwitterClient.java

License:Open Source License

public BasicTwitterClient() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(Twitter.consumerKey)
            .setOAuthConsumerSecret(Twitter.consumerSecret).setOAuthAccessToken(Twitter.accessToken)
            .setOAuthAccessTokenSecret(Twitter.accessTokenSecret);
    TwitterFactory twitterFactory = new TwitterFactory(cb.build());
    instance = twitterFactory.getInstance();
}

From source file:tweet.Miner.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param toSearch  // ww w.  j  av a  2  s  .co m
 */
public List<Status> mine(String toSearch) {
    List<Status> results = new ArrayList<Status>();
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY).setOAuthConsumerSecret(CONSUMER_SECRET)
            .setOAuthAccessToken(OAUTH_ACCESS_TOKEN).setOAuthAccessTokenSecret(OAUTH_ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    try {
        Query query = new Query(toSearch);
        query.setCount(100);
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            results.addAll(result.getTweets());
            for (Status tweet : tweets) {
                System.out.println(
                        tweet.getId() + " @ " + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return results;
}

From source file:tweete.Tweete.java

License:Open Source License

public void updateTweete(String sta) {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("######################")
            .setOAuthConsumerSecret("######################")
            .setOAuthAccessToken("############################################")
            .setOAuthAccessTokenSecret("############################################");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    try {/*  ww  w  .  j ava 2s.  c om*/

        twitter.updateStatus(sta);

        System.out.println("Successfully updated the status in Twitter.");

    } catch (TwitterException te) {

        if (401 == te.getStatusCode()) {
            System.out.println("Unable to get the access token.");
        }

        else if (92 == te.getStatusCode()) {
            System.out.println("SSL is required");
        }

        else {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }
}

From source file:tweete.Tweete.java

License:Open Source License

public void showTimeline() {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("######################")
            .setOAuthConsumerSecret("######################")
            .setOAuthAccessToken("############################################")
            .setOAuthAccessTokenSecret("############################################");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    try {//from   w w  w . jav  a 2 s.  c  om
        ResponseList<Status> a = twitter.getUserTimeline(new Paging(1, 10));
        String statuses = "";
        for (Status b : a) {
            statuses = statuses + b.getText() + "\n\n---------------------------------------\n\n";

        }

        new TweeteTimeline().Timeline(statuses);
    } catch (TwitterException te) {
        //te.printStackTrace();

        if (401 == te.getStatusCode()) {
            System.out.println("Unable to get the access token.");
        }

        else if (92 == te.getStatusCode()) {
            System.out.println("SSL is required");
        }

        else {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }
}

From source file:tweete.Tweete.java

License:Open Source License

public void sendMessage(String id, String msg) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("######################")
            .setOAuthConsumerSecret("######################")
            .setOAuthAccessToken("############################################")
            .setOAuthAccessTokenSecret("############################################");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    try {/* w  w  w  .  j  a v a 2s  .  c o  m*/
        DirectMessage message = null;
        message = twitter.sendDirectMessage(id, msg);
        System.out.println("Sent: " + message.getText() + " to @" + message.getRecipientScreenName());

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

        if (401 == te.getStatusCode()) {
            System.out.println("Unable to get the access token.");
        }

        else if (92 == te.getStatusCode()) {
            System.out.println("SSL is required");
        }

        else {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }

}