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:miproyectolunadepluton.MiProyectoLunaDePluton.java

/**
 * @param args the command line arguments
 *///w  ww  .j  a  va2 s  .co m
public static void main(String[] args) throws TwitterException {
    /* ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)      
      .setOAuthConsumerKey("BNmJ2oaezMC2qciIgkpC33kYq")
    .setOAuthConsumerSecret("1b0xCDy2Qh5nFWeuJjdF1MDcjpVJTh4LSpjTfUKeabacirUOn4")
    .setOAuthAccessToken("708080587068858368-FxuwrHLsox7xVSQRbwjDV14vOpSxNyN")
    .setOAuthAccessTokenSecret("c4gBOfVZL7MwyhI7jVPVrGupnNJAiZAJ2cDE9qp48OVl1");*/
    TwitterFactory tf = new TwitterFactory();
    Twitter mitwitter = tf.getInstance();
    for (int i = 0; i < 11150; i++) {
        Status mistatus = mitwitter.updateStatus("Me la bufa");
        System.out.println(mistatus.getText());

    }
}

From source file:net.bluemix.droneselfie.TwitterUtilities.java

License:Open Source License

private String tweet(String pictureId, String message) {
    String output = null;/*from  www.  jav a 2 s  . c o m*/
    if (message == null)
        return null;
    if (message.equalsIgnoreCase(""))
        return null;

    try {
        String consumerKey = ConfigUtilities.getSingleton().getTwitterConsumerKey();
        String consumerSecret = ConfigUtilities.getSingleton().getTwitterConsumerSecret();
        String accessToken = ConfigUtilities.getSingleton().getTwitterAccessToken();
        String accessTokenSecret = ConfigUtilities.getSingleton().getTwitterAccessTokenSecret();

        TwitterFactory twitterFactory = new TwitterFactory();
        Twitter twitter = twitterFactory.getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

        StatusUpdate statusUpdate = new StatusUpdate(message);

        AttachmentInputStream data = DatabaseUtilities.getSingleton().getDB().getAttachment(pictureId,
                pictureId);
        statusUpdate.setMedia("picture", data);

        Status status = twitter.updateStatus(statusUpdate);
        if (status == null)
            return null;
        output = "https://twitter.com/bluedroneselfie/status/" + String.valueOf(status.getId());

        return output;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return output;
}

From source file:net.bluemix.newsaggregator.TwitterUtilities.java

License:Open Source License

private String tweet(String message) {
    String output = null;//from w  w  w . j  a va  2s.  co  m
    if (message == null)
        return null;
    if (message.equalsIgnoreCase(""))
        return null;

    try {
        String consumerKey = ConfigUtilities.getSingleton().getTwitterConsumerKey();
        String consumerSecret = ConfigUtilities.getSingleton().getTwitterConsumerSecret();
        String accessToken = ConfigUtilities.getSingleton().getTwitterAccessToken();
        String accessTokenSecret = ConfigUtilities.getSingleton().getTwitterAccessTokenSecret();

        TwitterFactory twitterFactory = new TwitterFactory();
        Twitter twitter = twitterFactory.getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

        StatusUpdate statusUpdate = new StatusUpdate(message);

        Status status = twitter.updateStatus(statusUpdate);
        if (status == null)
            return null;
        output = "https://twitter.com/BluemixInfo/status/" + String.valueOf(status.getId());

        return output;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return output;
}

From source file:net.hekatoncheir.speechtweet.SpeechTweet.java

License:Apache License

private boolean verifyCredentials() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String accessToken = sharedPreferences.getString("accessToken", null);
    String accessTokenSecret = sharedPreferences.getString("accessTokenSecret", null);
    if (accessToken == null || accessTokenSecret == null)
        return false;

    // Read configuration
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(mOAuthConsumerKey).setOAuthConsumerSecret(mOAuthConsumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);

    ///*from  w w  w .  j  a v  a2s  .com*/
    mIsVerifyCredentials = true;
    mRingPlayer = MediaPlayer.create(SpeechTweet.this, R.raw.ring);
    mRingPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            if (mIsVerifyCredentials) {
                mRingPlayer.start();
            }
        }
    });
    mRingPlayer.start();
    showMessage(R.string.message_ring);

    TwitterFactory factory = new TwitterFactory(cb.build());
    mTwitter = factory.getInstance();
    new Thread(new Runnable() {
        @Override
        public void run() {
            User user = null;
            try {
                user = mTwitter.verifyCredentials();
            } catch (TwitterException e) {
            }
            final User u = user;
            mHandler.post(new Runnable() {
                public void run() {
                    SpeechTweet.this.finishVerifyCredentials(u);
                }
            });
        }
    }).start();
    return true;
}

From source file:net.hekatoncheir.speechtweet.SpeechTweet.java

License:Apache License

private void startOAuth() {
    mIsOpenOAuth = true;//  w w  w . j  a v  a2 s  .c  o  m

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(mOAuthConsumerKey)
            .setOAuthConsumerSecret(mOAuthConsumerSecret);

    TwitterFactory factory = new TwitterFactory(cb.build());
    mTwitter = factory.getInstance();

    new Thread(new Runnable() {
        @Override
        public void run() {
            Boolean isSuccess = false;
            try {
                mRequestToken = mTwitter.getOAuthRequestToken(mOAuthCallbackURL);
                isSuccess = true;
            } catch (TwitterException e) {
            }
            final Boolean s = isSuccess;
            mHandler.post(new Runnable() {
                public void run() {
                    SpeechTweet.this.finishGetOAuthRequestToken(s);
                }
            });
        }
    }).start();
}

From source file:nl.isaac.dotcms.twitter.util.TwitterUtil.java

License:Creative Commons License

public TwitterUtil(String twitterConsumerKey, String twitterSecretConsumerKey, String twitterAccessToken,
        String twitterSecretAccessToken) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true) //Added for extra logging
            .setOAuthConsumerKey(twitterConsumerKey).setOAuthConsumerSecret(twitterSecretConsumerKey)
            .setOAuthAccessToken(twitterAccessToken).setOAuthAccessTokenSecret(twitterSecretAccessToken)
            .setUseSSL(true);//  ww w .ja v a  2 s  .  co  m
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter = tf.getInstance();
}

From source file:nl.utwente.bigdata.bolts.TimelineBolt.java

License:Apache License

@Override
public void prepare(Map stormConf, TopologyContext context) {
    String consumerKey = (String) stormConf.get("consumerKey");
    String consumerSecret = (String) stormConf.get("consumerSecret");
    String accessToken = (String) stormConf.get("accessToken");
    String accessTokenSecret = (String) stormConf.get("accessTokenSecret");
    System.out.printf("%s %s %s %s\n", consumerKey, consumerSecret, accessToken, accessTokenSecret);
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret)
            .setJSONStoreEnabled(true);/*from w  w w . j a  va2 s.  co  m*/
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter = tf.getInstance();
    try {
        getRateLimit();
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:noki.preciousshot.helper.TwitterHelper.java

License:Apache License

public static void tweetMedia(String text, File file) {

    Thread thread = new Thread() {
        private String text;
        private File file;

        public Thread setArgs(String text, File file) {
            this.text = text;
            this.file = file;
            return this;
        }/*ww w . j a  v a  2s .c o  m*/

        @Override
        public void run() {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true).setOAuthConsumerKey(PreciousShotData.twitterKeys[0])
                    .setOAuthConsumerSecret(PreciousShotData.twitterKeys[1])
                    .setOAuthAccessToken(PreciousShotData.twitterKeys[2])
                    .setOAuthAccessTokenSecret(PreciousShotData.twitterKeys[3]);
            TwitterFactory tf = new TwitterFactory(cb.build());
            Twitter twitter = tf.getInstance();

            try {
                Status status = twitter.updateStatus(new StatusUpdate(this.text).media(this.file));
                if (status != null && status.getId() != 0) {
                    String url = String.format("https://twitter.com/%s/status/%s", twitter.getScreenName(),
                            status.getId());
                    PreciousShotCore.log("the url is %s.", url);
                    LangHelper.sendChatWithUrl(LangKey.TWITTER_SUCCESS, LangKey.TWITTER_URL, url);
                } else {
                    LangHelper.sendChat(LangKey.TWITTER_FAILED);
                }
            } catch (TwitterException e) {
                LangHelper.sendChat(LangKey.TWITTER_FAILED);
            }
        }
    }.setArgs(text, file);

    thread.start();

}

From source file:nselive.NSELive.java

/**
 * @param args the command line arguments
 *//*from ww w . j  ava 2  s.  c  o m*/
public static void main(String[] args) {
    try {
        String url = "http://www.nellydata.com/CapitalFM/livedata.asp";
        //fetch data
        String docString = Jsoup.connect(url).get().toString();
        String[] tBodyArray = docString.split("<tbody>");
        String[] tableArray = tBodyArray[1].split("</tbody>");
        String tableContent = tableArray[0].trim();
        //delete header rows
        String[] headerlessContent = tableContent.split("Low</strong> </td>");
        String[] rowArray = headerlessContent[1].split("<tr>");
        //skip rowArray[0] which has string "</tr>
        for (int i = 1; i <= rowArray.length - 1; i++) {
            String rowContent = rowArray[i];
            String[] cellArray = rowContent.split("</td>");
            Stock stock = new Stock();
            String[] idArray = cellArray[0].split("mycell\">");
            stock.setId(Integer.parseInt(idArray[1]));
            String[] stockNameArray1 = cellArray[1].split("<strong>");
            String[] stockNameArray2 = stockNameArray1[1].split("</strong>");
            stock.setName(stockNameArray2[0]);
            String[] priceYesterdayArray = cellArray[2].split("mycell\">");
            stock.setPriceYesterday(Double.parseDouble(priceYesterdayArray[1].replace(",", "")));
            String[] currentPriceArray = cellArray[3].split("style2\">");
            stock.setCurrentPrice(Double.parseDouble(currentPriceArray[1].replace(",", "")));
            if (stock.getCurrentPrice() != stock.getPriceYesterday()) {
                String tweet = "";
                //TODO: Change to hourly updates
                if (stock.getCurrentPrice() > stock.getPriceYesterday()) {
                    tweet = stock.getName().toUpperCase() + " has RISEN to " + stock.getCurrentPrice()
                            + " from " + stock.getPriceYesterday() + " yesterday";
                } else if (stock.getCurrentPrice() < stock.getPriceYesterday()) {
                    tweet = stock.getName().toUpperCase() + " has FALLEN to " + stock.getCurrentPrice()
                            + " from " + stock.getPriceYesterday() + " yesterday";
                }
                //get the following from your twitter account
                String consumerKey = "yourConsumerKey";
                String consumerSecret = "yourConsumerSecret";
                String accessToken = "yourAccessToken";
                String accessSecret = "yourAccessSecret";

                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
                        .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessSecret);

                try {
                    TwitterFactory factory = new TwitterFactory(cb.build());
                    Twitter twitter = factory.getInstance();

                    Status status = twitter.updateStatus(tweet);
                    System.out.println("NEW TWEET: " + status.getText());
                } catch (TwitterException te) {
                    te.printStackTrace();
                    System.exit(-1);
                }
            }
        }
    } catch (IOException ex) {
        System.out.println("Ooops! No data this time. Our connection timed out :(");
        Logger.getLogger(NSELive.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:nz.co.lolnet.james137137.lolnettwitchaddonbc.TwitterAPI.java

public TwitterAPI() {
    //Instantiate a re-usable and thread-safe factory
    TwitterFactory twitterFactory = new TwitterFactory();

    //Instantiate a new Twitter instance
    twitter = twitterFactory.getInstance();

    //setup OAuth Consumer Credentials
    twitter.setOAuthConsumer(consumerKey, consumerSecret);

    //setup OAuth Access Token
    twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));
}