Example usage for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

List of usage examples for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

Introduction

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

Prototype

ConfigurationBuilder

Source Link

Usage

From source file:com.nookdevs.twook.activities.Settings.java

License:Open Source License

public ConfigurationBuilder getConfiguration() {
    ConfigurationBuilder confBuilder = new ConfigurationBuilder();
    confBuilder.setOAuthConsumerKey(consumerKey);
    confBuilder.setOAuthConsumerSecret(consumerSecret);

    return confBuilder;
}

From source file:com.oldterns.vilebot.handlers.user.UrlTweetAnnouncer.java

License:Open Source License

/**
 * Accesses the source of a HTML page and looks for a title element
 * //ww  w. j  av  a  2 s . c  om
 * @param url http tweet String
 * @return String of text which represents the tweet.  Empty if error.
 */
private String scrapeURLHTMLTitle(String url) {
    String text = "";

    URL page;
    try {
        page = new URL(url);
    } catch (MalformedURLException x) {
        // System.err.format("scrapeURLHTMLTitle new URL error: %s%n", x);
        return text;
    }

    //split the url into pieces, change the request based on what we have
    String parts[] = url.split("/");
    int userPosition = 0;
    long tweetID = 0;
    for (int i = 0; i < parts.length; i++) {

        if (parts[i].toString().equals("twitter.com"))
            userPosition = i + 1;
        if (parts[i].toString().equals("status") || parts[i].toString().equals("statuses"))
            tweetID = Long.valueOf(parts[i + 1].toString()).longValue();
    }
    if (userPosition == 0)
        return text;
    else {
        try {
            String consumerKey = ""; //may be known as 'API key'
            String consumerSecret = ""; //may be known as 'API secret'
            String accessToken = ""; //may be known as 'Access token'
            String accessTokenSecret = ""; //may be known as 'Access token secret'
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
                    .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);
            TwitterFactory tf = new TwitterFactory(cb.build());
            Twitter twitter = tf.getInstance();
            if (tweetID != 0) //tweet of the twitter.com/USERID/status/TWEETID variety
            {
                Status status = twitter.showStatus(tweetID);
                return (status.getUser().getName() + ": " + status.getText());
            } else //just the user is given, ie, twitter.com/USERID 
            {
                User user = twitter.showUser(parts[userPosition].toString());
                if (!user.getDescription().isEmpty()) //the user has a description
                    return ("Name: " + user.getName() + " | " + user.getDescription() + "\'\nLast Tweet: \'"
                            + user.getStatus().getText());
                else //the user doesn't have a description, don't print it
                    return ("Name: " + user.getName() + "\'\nLast Tweet: \'" + user.getStatus().getText());

            }
        } catch (TwitterException x) {
            return text;
        }
    }
}

From source file:com.ontotext.s4.TwitterVisualization.downloadTweets.SearchTweets.java

License:Open Source License

/**
 * Creates and configures Twitter client instance with provided credentials.
 * /*from w  ww  .ja va 2s  .com*/
 * @return Twitter instance
 */
private Twitter ConfigurateTwitterAccount() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret)
            .setJSONStoreEnabled(true);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    return twitter;
}

From source file:com.producer.TwitterProducer.java

License:Apache License

public static void main(String[] args) {

    if (args.length == 0) {
        System.out.println(/*from   w ww  . j  av a  2  s .com*/
                "SimpleCounter {broker-list} {topic} {type old/new} {type sync/async} {delay (ms)} {count}");
        return;
    }

    /* get arguments */
    String brokerList = args[0];
    String topic = args[1];
    String age = args[2];
    String sync = args[3];
    /*
     * In order to create the spout, you need to get twitter credentials
     * If you need to use Twitter firehose/Tweet stream for your idea,
     * create a set of credentials by following the instructions at
     *
     * https://dev.twitter.com/discussions/631
     *
     */
    String custkey = "WXDgVgeJMwHEn0Z9VHDx5j93h";
    String custsecret = "DgP9CsaPtG87urpNU14fZySXOjNX4j4v2PqmeTndcjjYBgLldy";
    String accesstoken = "3243813491-ixCQ3HWWeMsthKQvj5MiBvNw3dSNAuAd3IfoDUw";
    String accesssecret = "aHOXUB4nbhZv2vbAeV15ZyTAD0lPPCptCr32N0PX7OaMe";

    producer = new DemoProducerOld(topic);

    /* start a producer */
    producer.configure(brokerList, sync);
    producer.start();

    //long startTime = System.currentTimeMillis();
    System.out.println("Starting...");
    producer.produce("Starting...");

    ConfigurationBuilder config = new ConfigurationBuilder().setOAuthConsumerKey(custkey)
            .setOAuthConsumerSecret(custsecret).setOAuthAccessToken(accesstoken)
            .setOAuthAccessTokenSecret(accesssecret);

    // create the twitter stream factory with the config
    TwitterStream twitterStream;
    TwitterStreamFactory fact = new TwitterStreamFactory(config.build());

    // get an instance of twitter stream
    twitterStream = fact.getInstance();
    // message to kafka
    Map<String, String> headers = new HashMap<String, String>();

    //filter non-english tweets
    FilterQuery tweetFilterQuery = new FilterQuery();
    tweetFilterQuery.language(new String[] { "en" });
    // tweetFilterQuery.locations(new double[][] { { -180, -90 }, { 180, 90 } });

    // provide the handler for twitter stream
    twitterStream.addListener(new TweetListener());

    twitterStream.filter(tweetFilterQuery);

    // start the sampling of tweets
    twitterStream.sample();

    // TODO ADD timestamp
    //long endTime = System.currentTimeMillis();
    //System.out.println("... and we are done. This took " + (endTime - startTime) + " ms.");
    //producer.produce("... and we are done. This took " + (endTime - startTime) + " ms.");

    /* close shop and leave */
    //producer.close();
    //System.exit(0);
}

From source file:com.pulzitinc.flume.source.TwitterSource.java

License:Apache License

/**
 * The initialization method for the Source. The context contains all the
 * Flume configuration info, and can be used to retrieve any configuration
 * values necessary to set up the Source.
 *//*w  ww  .  jav a  2  s.c  o m*/
@Override
public void configure(Context context) {

    logger.info(context.toString());

    consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY_KEY);
    consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET_KEY);
    accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN_KEY);
    accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET_KEY);

    String accountIdsString = context.getString(TwitterSourceConstants.ACCOUNT_IDS_KEY, "");
    if (accountIdsString.trim().length() == 0) {
        throw new IllegalStateException("No accounts to follow provided");
    } else {
        String[] accountIds = accountIdsString.split(",");
        accountsToFollow = new long[accountIds.length];
        for (int i = 0; i < accountIds.length; i++) {
            accountsToFollow[i] = Long.valueOf(accountIds[i]);
        }
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
}

From source file:com.redhat.middleware.jdg.TwitterDemoClient.java

License:Open Source License

public void startAsync() {

    ConfigurationBuilder cb = new ConfigurationBuilder();

    if (accessTokenSecret == null || accessToken == null) {
        try {/* w ww . j  ava2  s.  c  o m*/
            this.authorize();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "error occured while authorizing", e);
        }
    }

    cb.setDebugEnabled(false).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    twitterStream.addListener(new DemoTwitterListener(twitterStream));
    twitterStream.sample();
}

From source file:com.reyk.socialmedia.implementations.TwitterSB.java

@Override
public String connectToSocialMedia(String username) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("a7QcplYaabjAy7Qw6WsyNAkwy")
            .setOAuthConsumerSecret("1xHSPlDU6eUp7YkkZL9oQioeQGuKiZHtwNuUCmTbARD2MoJLwV");
    twitter = new TwitterFactory(cb.build()).getInstance();
    try {/*from  ww w.j  a v a  2  s .  c  o m*/
        token = twitter.getOAuthRequestToken();
    } catch (TwitterException ex) {
        return "Impossible to connect with Twitter";
    }
    return token.getAuthorizationURL();
}

From source file:com.reyk.socialmedia.implementations.TwitterSB.java

@Override
public void postComment(String username, String post) throws EJBException {
    try {/*ww w .ja v a2  s  . c  o m*/
        String fileName = "twitter-" + username + ".properties";
        File file = new File(fileName);
        OutputStream outputStream = null;
        Properties prop = new Properties();
        InputStream inputStream = null;

        try {
            if (file.exists()) {
                inputStream = new FileInputStream(file);
                prop.load(inputStream);
            } else {
                throw new EJBException("There is no configuration int Twitter for " + username);
            }

        } catch (IOException ioEx) {
            ioEx.printStackTrace();
            System.exit(-1);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ignore) {
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ignore) {
                }
            }
        }
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setOAuthConsumerKey("a7QcplYaabjAy7Qw6WsyNAkwy")
                .setOAuthConsumerSecret("1xHSPlDU6eUp7YkkZL9oQioeQGuKiZHtwNuUCmTbARD2MoJLwV")
                .setOAuthAccessToken(prop.getProperty("oauth.accessToken"))
                .setOAuthAccessTokenSecret(prop.getProperty("oauth.accessTokenSecret"));
        twitter = new TwitterFactory(cb.build()).getInstance();
        twitter.updateStatus(post);
    } catch (TwitterException e) {
        throw new EJBException("Error en el twitter sb al twittear", e);
    }
}

From source file:com.richardstansbury.tweetfollow.TwitterHelper.java

License:Open Source License

/**
 * Creates an authentication configuration for accessing the _twitter streaming
 * API./*from   w  w  w . ja va  2  s  . c o  m*/
 *
 * See readme.txt in root folder for location and format of twitter4j.txt.
 *
 * @param c - context to access the asset's folder
 * @return configuration object based upon keys read from asset's folder
 */
private Configuration readConfiguration(Context c) {

    //Create a Configuration Builder
    ConfigurationBuilder cb = new ConfigurationBuilder();
    try {

        //Create an input stream for the properties file
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getAssets().open("twitter4j.txt")));

        //Set up the configuration
        cb.setDebugEnabled(true).setOAuthConsumerKey(in.readLine()).setOAuthConsumerSecret(in.readLine())
                .setOAuthAccessToken(in.readLine()).setOAuthAccessTokenSecret(in.readLine());

    } catch (IOException io) {
        Log.e("TwitterHelper", "Error opening input file");
        System.exit(-1);
    }

    //Build the configuration and return the object
    return cb.build();
}

From source file:com.rowland.hashtrace.utility.Utility.java

License:Apache License

public static Twitter getTwitter() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
    cb.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
    cb.setOAuthAccessToken(Constants.TWITTER_ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(Constants.TWITTER_TOKEN_SECRET).setHttpConnectionTimeout(100000);
    //cb.setUseSSL(true);

    /*//from   w  ww . j a  v  a 2s .  co  m
     * if() { cb.setHttpProxyHost(httpProxyHost);
     * cb.setHttpProxyPort(httpProxyPort);
     * cb.setHttpProxyUser(httpProxyUser);
     * cb.setHttpProxyPassword(httpProxyPassword);
     *
     * }
     */

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