Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

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

Prototype

public TwitterFactory(String configTreePath) 

Source Link

Document

Creates a TwitterFactory with a specified config tree

Usage

From source file:net.nokok.twitduke.core.factory.TTwitterFactory.java

License:Open Source License

/**
 * Consumer/ConsumerSecret?????Twitter????
 *
 * ???????????????????//from  www . jav a  2s  . co  m
 * ??????????????
 *
 * @return ??Twitter
 */
public static Twitter newInstance() {
    Configuration configuration = ConfigurationProvider.getConfiguration();
    Twitter twitter = new TwitterFactory(configuration).getInstance();
    return twitter;
}

From source file:net.nokok.twitduke.core.factory.TTwitterFactory.java

License:Open Source License

/**
 * ????Twitter????/*  ww w.  j  a  va  2  s  .c om*/
 *
 * ???????Twitter?API??????
 *
 * @param accessToken 
 *
 * @return ???Twitter
 */
public static Twitter newInstance(AccessToken accessToken) {
    Configuration configuration = ConfigurationProvider.getConfiguration();
    Twitter twitter = new TwitterFactory(configuration).getInstance(accessToken);
    return twitter;
}

From source file:net.nokok.twitduke.core.twitter.UpdateProfileImpl.java

License:Open Source License

public UpdateProfileImpl(AccessToken accessToken) {
    if (accessToken == null) {
        throw new NullPointerException("???null??");
    }/*from w  w w . j a v  a  2s.  c  o m*/
    this.user = new TwitterFactory(ConfigurationProvider.getConfiguration()).getInstance(accessToken);
}

From source file:net.wasdev.gameon.auth.twitter.TwitterAuth.java

License:Apache License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ConfigurationBuilder c = new ConfigurationBuilder();
    c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret);

    Twitter twitter = new TwitterFactory(c.build()).getInstance();
    request.getSession().setAttribute("twitter", twitter);

    try {//from ww  w. j  ava 2 s  . c o  m
        // twitter will tell the users browser to go to this address once
        // they are done authing.
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/TwitterCallback");

        // to initiate an auth request, twitter needs us to have a request
        // token.
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());

        // stash the request token in the session.
        request.getSession().setAttribute("requestToken", requestToken);

        // send the user to twitter to be authenticated.
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }

}

From source file:net.wasdev.gameon.auth.twitter.TwitterCallback.java

License:Apache License

/**
 * Method that performs introspection on an AUTH string, and returns data as
 * a String->String hashmap.//from   w w  w .  j  a va2  s  . c o  m
 * 
 * @param auth
 *            the authstring to query, as built by an auth impl.
 * @return the data from the introspect, in a map.
 * @throws IOException
 *             if anything goes wrong.
 */
public Map<String, String> introspectAuth(String token, String tokensecret) throws IOException {
    Map<String, String> results = new HashMap<String, String>();

    ConfigurationBuilder c = new ConfigurationBuilder();
    c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret).setOAuthAccessToken(token)
            .setOAuthAccessTokenSecret(tokensecret);

    Twitter twitter = new TwitterFactory(c.build()).getInstance();

    try {
        // ask twitter to verify the token & tokensecret from the auth
        // string
        // if invalid, it'll throw a TwitterException
        twitter.verifyCredentials();

        // if it's valid, lets grab a little more info about the user.
        long id = twitter.getId();
        ResponseList<User> users = twitter.lookupUsers(id);
        User u = users.get(0);
        String name = u.getName();
        String screenname = u.getScreenName();

        results.put("valid", "true");
        results.put("id", "twitter:" + id);
        results.put("name", name);
        results.put("screenname", screenname);

    } catch (TwitterException e) {
        results.put("valid", "false");
    }

    return results;
}

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);/*w w  w . java 2s  . 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 ww  .  j  av  a 2s. c  o  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;
        }//  w  w  w . j  av 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   w  w w  . j a  v a  2 s .  c om
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:oauth.AppOAuth.java

License:Apache License

public TwitterFactory loadOAuthUser(String endpoint)
        throws ClassNotFoundException, SQLException, TwitterException {

    MysqlDB DB = new MysqlDB();
    // System.out.println("DEBUG AppOAuth.java");

    // get OAuth cradentials by calling DB's loadOAuthUser(endpoint)
    ConstVars StaticVars = DB.loadOAuthUser(endpoint);

    String consumer_key = StaticVars.consumer_key;
    String consumer_secret = StaticVars.consumer_secret;
    String user_token = StaticVars.user_token;
    String user_secret = StaticVars.user_secret;

    RemainingCalls = StaticVars.Remaining;
    screen_name = StaticVars.screen_name;

    // working with twitter4j.properties
    // Twitter twitter = new TwitterFactory().getInstance(); 
    // initialize Twitter OAuth
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setJSONStoreEnabled(true).setDebugEnabled(true).setOAuthConsumerKey(consumer_key)
            .setOAuthConsumerSecret(consumer_secret).setOAuthAccessToken(user_token)
            .setOAuthAccessTokenSecret(user_secret);

    TwitterFactory tf = new TwitterFactory(cb.build());
    return tf;/*from ww w  .j  a v a  2s .  c  o m*/
}