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:android.stickynotes.StickyNotesActivity.java

License:Apache License

private void getTweets(String twit) {
    wifi.setWifiEnabled(true);/*w ww.  j  av  a  2  s  . c  o  m*/
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("TvywVhWx7r7QQev2UGfA4g")
            .setOAuthConsumerSecret("Nv22zsyf1VS0vvi6hwAMyvJk9LUtSXwRUB4xwp2gRs");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    try {
        QueryResult result = twitter.search(new Query(twit));
        List<Tweet> tweets = result.getTweets();
        textStatus.setText("");
        textStatus.append("Recent tweets about '" + twit + "':\n");
        for (Tweet tweet : tweets) {
            textStatus.append("@" + tweet.getFromUser() + " - " + tweet.getText() + "\n\n");
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        textStatus.append("Failed to search tweets: " + te.getMessage() + " " + twit);
    }
}

From source file:apptwitter.Metodos.java

public Metodos() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("5yqwH2WcqZ3YlkE0W8BLYkeoW")
            .setOAuthConsumerSecret("s5s5QGigqzULDGbbQs4Rm0pKdqLEefbO4gCh53XgcnA0RoMA9n")
            .setOAuthAccessToken("3055143533-jeaPEtFscvDMVKwPVLubxkmHs9DzCjZ2eIoWdcQ")
            .setOAuthAccessTokenSecret("NzIM687NlVRe3VxKh0a0xRRYpsGytrSEKISaqpuQGtTZL");

    twitter = new TwitterFactory(cb.build()).getInstance();
}

From source file:au.com.infiniterecursion.hashqanda.MainActivity.java

public boolean loadTweets() {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Q2DfeCNOxprbDp66fWlw")
            .setOAuthConsumerSecret("TgyJ26CKXz1SxGiEJx4HG9rytFsqiMZlEPeCO7S9g");

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    // clear tweet list.
    MainActivity.this.runOnUiThread(new Runnable() {

        public void run() {
            // add to it , via the runOnUIThread
            app.getTweetList().clear();//from   w  ww  .j  a  v  a 2  s .  com
        }

    });

    try {
        Log.d(TAG, " starting Tweets loading ");
        Query q = new Query("#qanda");

        q.setRpp(numberTweets);

        QueryResult result = twitter.search(q);
        List<Tweet> tweets = result.getTweets();
        for (Tweet tweet : tweets) {
            // Log.d(TAG, "@" + tweet.getFromUser() + " - " +
            // tweet.getText());
            // Log.d(TAG, " img url " + tweet.getProfileImageUrl());

            final CachedBitmapAndTweet cachedbmtwt = new CachedBitmapAndTweet();

            cachedbmtwt.twt = tweet;

            try {
                URL url = new URL(tweet.getProfileImageUrl());
                InputStream is = (InputStream) url.getContent();

                if (is != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    cachedbmtwt.bm = bitmap;
                } else {
                    cachedbmtwt.bm = null;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
                cachedbmtwt.bm = null;
            } catch (IOException e) {
                e.printStackTrace();
                cachedbmtwt.bm = null;
            } catch (NullPointerException npe) {
                npe.printStackTrace();
                cachedbmtwt.bm = null;
            }

            MainActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                    // add to it , via the runOnUIThread
                    app.getTweetList().add(cachedbmtwt);
                }

            });

        }

        Log.d(TAG, " finished Tweets loading ");

        return true;

    } catch (TwitterException te) {
        te.printStackTrace();
        Log.d(TAG, "Failed to search tweets: " + te.getMessage());
        return false;
    }
}

From source file:Beans.Crawler.java

/**
 * Retrieve the "bearer" token from Twitter in order to make application-authenticated calls.
 *
 * This is the first step in doing application authentication, as described in Twitter's documentation at
 * https://dev.twitter.com/docs/auth/application-only-auth
 *
 * Note that if there's an error in this process, we just print a message and quit.  That's a pretty
 * dramatic side effect, and a better implementation would pass an error back up the line...
 *
 * @return   The oAuth2 bearer token/*from  w  w  w .  j  a  va  2 s . co m*/
 */
public static OAuth2Token getOAuth2Token() {
    OAuth2Token token = null;
    ConfigurationBuilder cb;

    cb = new ConfigurationBuilder();
    cb.setApplicationOnlyAuthEnabled(true);

    cb.setOAuthConsumerKey(CONSUMER_KEY).setOAuthConsumerSecret(CONSUMER_SECRET);

    try {
        token = new TwitterFactory(cb.build()).getInstance().getOAuth2Token();
    } catch (Exception e) {
        System.out.println("Could not get OAuth2 token");
        e.printStackTrace();
        System.exit(0);
    }

    return token;
}

From source file:Beans.Crawler.java

/**
 * Get a fully application-authenticated Twitter object useful for making subsequent calls.
 *
 * @return   Twitter4J Twitter object that's ready for API calls
 *//* www . j a va  2 s .c  o m*/
public static Twitter getTwitter() {
    OAuth2Token token;

    //   First step, get a "bearer" token that can be used for our requests
    token = getOAuth2Token();

    //   Now, configure our new Twitter object to use application authentication and provide it with
    //   our CONSUMER key and secret and the bearer token we got back from Twitter
    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setApplicationOnlyAuthEnabled(true);

    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);

    cb.setOAuth2TokenType(token.getTokenType());
    cb.setOAuth2AccessToken(token.getAccessToken());

    //   And create the Twitter object!
    return new TwitterFactory(cb.build()).getInstance();

}

From source file:benche.me.TwitterParser.Main.java

License:Open Source License

/**
     * Configure twitter API connection for historical search
     * @return Twitter connection instance
     *//*from   w  w  w  .j a  va  2  s  .  c  om*/
    private static Twitter configureSearch() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY_KEY)
                .setOAuthConsumerSecret(Constants.CONSUMER_SECRET_KEY)
                .setOAuthAccessToken(Constants.ACCESS_TOKEN_KEY)
                .setOAuthAccessTokenSecret(Constants.ACCESS_TOKEN_SECRET_KEY);
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        return twitter;
    }

From source file:br.com.controller.TweetController.java

public static OAuth2Token getOAuth2Token() {
    OAuth2Token token = null;//from w w w  .jav  a 2 s .  c om
    ConfigurationBuilder cb;

    cb = new ConfigurationBuilder();
    cb.setApplicationOnlyAuthEnabled(true);

    cb.setOAuthConsumerKey(CONSUMER_KEY).setOAuthConsumerSecret(CONSUMER_SECRET);

    try {
        token = new TwitterFactory(cb.build()).getInstance().getOAuth2Token();
    } catch (Exception e) {
        System.out.println("Could not get OAuth2 token");
        e.printStackTrace();
        System.exit(0);
    }

    return token;
}

From source file:br.com.controller.TweetController.java

public static Twitter getTwitter() {
    OAuth2Token token;//from ww  w .  ja v  a  2  s.c om

    token = getOAuth2Token();
    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setApplicationOnlyAuthEnabled(true);

    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);

    cb.setOAuth2TokenType(token.getTokenType());
    cb.setOAuth2AccessToken(token.getAccessToken());

    return new TwitterFactory(cb.build()).getInstance();
}

From source file:br.com.porcelli.hornetq.integration.twitter.outgoing.impl.OutgoingTwitterHandler.java

License:Apache License

public OutgoingTwitterHandler(final String connectorName, final Map<String, Object> configuration,
        final PostOffice postOffice) {

    this.connectorName = connectorName;

    this.mbean = new TwitterOutgoingManagement(this);

    try {//from  w  w w  .ja  va 2s .  c  om
        MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName mbeanName = new ObjectName("org.hornetq:module=ConnectorService,name=" + connectorName);
        mbServer.registerMBean(mbean, mbeanName);
    } catch (Exception e) {
        log.error("Error on registering JMX info.", e);
    }

    final Configuration conf = new ConfigurationBuilder()
            .setOAuthConsumerKey(ConfigurationHelper
                    .getStringProperty(InternalTwitterConstants.PROP_CONSUMER_KEY, null, configuration))
            .setOAuthConsumerSecret(ConfigurationHelper
                    .getStringProperty(InternalTwitterConstants.PROP_CONSUMER_SECRET, null, configuration))
            .setOAuthAccessToken(ConfigurationHelper
                    .getStringProperty(InternalTwitterConstants.PROP_ACCESS_TOKEN, null, configuration))
            .setOAuthAccessTokenSecret(ConfigurationHelper
                    .getStringProperty(InternalTwitterConstants.PROP_ACCESS_TOKEN_SECRET, null, configuration))
            .build();

    this.postOffice = postOffice;
    this.twitter = new TwitterFactory(conf).getInstance();

    final String queueName = ConfigurationHelper.getStringProperty(InternalTwitterConstants.PROP_QUEUE_NAME,
            null, configuration);

    final String errorQueueName = ConfigurationHelper
            .getStringProperty(InternalTwitterConstants.PROP_ERROR_QUEUE_NAME, null, configuration);

    final String sentQueueName = ConfigurationHelper
            .getStringProperty(InternalTwitterConstants.PROP_SENT_QUEUE_NAME, null, configuration);

    final Binding queueBinding = postOffice.getBinding(new SimpleString(queueName));
    if (queueBinding == null) {
        throw new RuntimeException(connectorName + ": queue " + queueName + " not found");
    }
    queue = (Queue) queueBinding.getBindable();

    if (errorQueueName != null && errorQueueName.trim().length() > 0) {
        final Binding errorQueueBinding = postOffice.getBinding(new SimpleString(errorQueueName));
        if (errorQueueBinding == null) {
            throw new RuntimeException(connectorName + ": queue " + errorQueueName + " not found");
        }
        errorQueue = (Queue) errorQueueBinding.getBindable();
    } else {
        errorQueue = null;
    }

    if (sentQueueName != null && sentQueueName.trim().length() > 0) {
        final Binding sentQueueBinding = postOffice.getBinding(new SimpleString(sentQueueName));
        if (sentQueueBinding == null) {
            throw new RuntimeException(connectorName + ": queue " + sentQueueName + " not found");
        }
        sentQueue = (Queue) sentQueueBinding.getBindable();
    } else {
        sentQueue = null;
    }
}

From source file:br.com.porcelli.hornetq.integration.twitter.stream.MessageQueuing.java

License:Apache License

private void executeReclaimers() {
    if (reclaimersSet != null && reclaimersSet.size() > 0) {
        final Twitter twitter = new TwitterFactory(data.getConf()).getInstance();
        final Set<AbstractBaseReclaimLostTweets> executedReclaimers = new HashSet<AbstractBaseReclaimLostTweets>();
        for (final AbstractBaseReclaimLostTweets reclaimer : reclaimersSet) {
            try {
                reclaimer.execute(twitter);
                executedReclaimers.add(reclaimer);
            } catch (final Exception e) {
                log.error("Couldn't execute reclaimer:" + reclaimer.getClass().getName(), e);
            }//from   w w  w .ja  v a  2 s .c  o  m
        }
        twitter.shutdown();
        for (AbstractBaseReclaimLostTweets activeExecutedReclaimer : executedReclaimers) {
            reclaimersSet.remove(activeExecutedReclaimer);
            activeExecutedReclaimer = null;
        }
    }
}