Example usage for twitter4j.conf ConfigurationBuilder setJSONStoreEnabled

List of usage examples for twitter4j.conf ConfigurationBuilder setJSONStoreEnabled

Introduction

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

Prototype

public ConfigurationBuilder setJSONStoreEnabled(boolean enabled) 

Source Link

Usage

From source file:org.loklak.scraper.TwitterRiver.java

License:Apache License

/**
 * Build configuration object with credentials and proxy settings
 * @return/*from  ww  w  .  ja  va  2 s  .c  o  m*/
 */
private Configuration buildTwitterConfiguration() {
    logger.debug("creating twitter configuration");
    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setOAuthConsumerKey(oauthConsumerKey).setOAuthConsumerSecret(oauthConsumerSecret)
            .setOAuthAccessToken(oauthAccessToken).setOAuthAccessTokenSecret(oauthAccessTokenSecret);

    if (proxyHost != null)
        cb.setHttpProxyHost(proxyHost);
    if (proxyPort != null)
        cb.setHttpProxyPort(Integer.parseInt(proxyPort));
    if (proxyUser != null)
        cb.setHttpProxyUser(proxyUser);
    if (proxyPassword != null)
        cb.setHttpProxyPassword(proxyPassword);
    if (raw)
        cb.setJSONStoreEnabled(true);
    logger.debug("twitter configuration created");
    return cb.build();
}

From source file:org.mixare.utils.TwitterClient.java

License:Open Source License

/**
 * Query the twitter search API using oAuth 2.0
 * @return//www  .jav  a  2 s  .  c om
 */
public static String queryData() {
    ConfigurationBuilder cb = new ConfigurationBuilder(); //to be configured in a properties...
    cb.setDebugEnabled(true).setOAuthConsumerKey("mt10dv6tTKacqlm14lw5w")
            .setOAuthConsumerSecret("4kRV1E1XIU3kj4JQj2R5LE1yct0RRaRl9sB5PpPrB0")
            .setOAuthAccessToken("390019380-IQ5VdvUKvxY9JOsTToEU8ElCabebc76H9X2g3QX4")
            .setOAuthAccessTokenSecret("ghJn4LTfDr7uHUCsbt6ycmpeVTwwpa3hZnXyEjyZvs");
    cb.setJSONStoreEnabled(true);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    Query query = new Query();
    query = query.geoCode(new GeoLocation(lat, lon), rad, Query.KILOMETERS);

    String jsonArrayAsString = "{\"results\":[";//start
    try {
        QueryResult result = twitter.search(query);
        int size = 0;
        for (Status status : result.getTweets()) {
            {
                if (status.getGeoLocation() != null) {
                    String jsonSingleObject = DataObjectFactory.getRawJSON(status);
                    if (size == 0)
                        jsonArrayAsString += jsonSingleObject;
                    else
                        jsonArrayAsString += "," + jsonSingleObject;
                    size++;
                }
            }
        }
        jsonArrayAsString += "]}";//close array
        return jsonArrayAsString;
    } catch (Exception e) {
        Log.e(Config.TAG, "Error querying twitter data :" + e);
        e.printStackTrace();
    }
    return null;
}

From source file:org.onepercent.utils.twitterstream.agent.src.main.java.com.cloudera.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.
 */// ww w . j  av a2  s  .c o  m
@Override
public void configure(Context context) {
    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 keywordString = context.getString(TwitterSourceConstants.KEYWORDS_KEY, "");
    if (keywordString.trim().length() == 0) {
        keywords = new String[0];
    } else {
        keywords = keywordString.split(",");
        for (int i = 0; i < keywords.length; i++) {
            keywords[i] = keywords[i].trim();
        }
    }

    String languageString = context.getString(TwitterSourceConstants.LANGUAGE_KEY, "");
    if (languageString.trim().length() == 0) {
        languages = new String[0];
    } else {
        languages = languageString.split(",");
        for (int i = 0; i < languages.length; i++) {
            languages[i] = languages[i].trim();
        }
    }

    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:org.primeoservices.cfgateway.twitter.railo.RailoTwitterUserStreamGateway.java

License:Apache License

/**
 * Initializes this gateway// w  w w .  j a  va 2 s .  c  o  m
 */
@Override
@SuppressWarnings("rawtypes")
public void init(final GatewayEngine engine, final String id, final String cfcPath, final Map config)
        throws IOException {
    try {
        this.argType = ArgumentType.fromConfigValue((String) config.get(ARGUMENT_TYPE));
        final ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setOAuthConsumerKey((String) config.get(OAUTH_CONSUMER_KEY));
        cb.setOAuthConsumerSecret((String) config.get(OAUTH_CONSUMER_SECRET));
        cb.setOAuthAccessToken((String) config.get(OAUTH_ACCESS_TOKEN));
        cb.setOAuthAccessTokenSecret((String) config.get(OAUTH_ACCESS_SECRET));
        cb.setUserStreamRepliesAllEnabled(Boolean.valueOf((String) config.get(ALL_REPLIES)));
        cb.setJSONStoreEnabled(ArgumentType.JSON.equals(this.argType));
        super.init(engine, id, new TwitterUserStream(this, cb.build()));
    } catch (Throwable t) {
        final IOException ex = new IOException("Unable to initialize gateway", t);
        this.onException(ex);
        throw ex;
    }
}

From source file:org.twitter.sample.main.Stream.java

public void getTweetsFromTwitter() {

    StatusListener listener = new StatusListener() {
        private boolean logQueueFull = true;

        @Override/* www  .  j a v a  2s  .c  o  m*/
        public void onException(Exception e) {
            log.error(e);
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice notice) {
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onStatus(Status status) {
            db.insert(status);
        }

        @Override
        public void onTrackLimitationNotice(int notice) {
            log.warn("*** TRACK LIMITATION REACHED: " + notice + " ***");
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            log.warn("*** STALL WARNING: " + arg0);
        }
    };

    ConfigurationBuilder twitterConfig = new ConfigurationBuilder();
    twitterConfig.setDebugEnabled(false);
    twitterConfig.setOAuthAccessTokenSecret(access_token_secret);
    twitterConfig.setOAuthAccessToken(access_token);
    twitterConfig.setOAuthConsumerKey(consumer_key);
    twitterConfig.setOAuthConsumerSecret(consumer_secret);
    twitterConfig.setJSONStoreEnabled(true);

    TwitterStreamFactory fact = new TwitterStreamFactory(twitterConfig.build());
    this.twitterStream = fact.getInstance();
    this.twitterStream.addListener(listener);
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.language(new String[] { "en" });
    this.twitterStream.filter(filterQuery);
    this.twitterStream.sample();

}

From source file:org.wso2.carbon.connector.twitter.TwitterClientLoader.java

License:Open Source License

public Twitter loadApiClient() throws TwitterException {
    Twitter twitter;// w  w  w.j  a v  a2  s.  c  om
    if (messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_KEY) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_SECRET) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN_SECRET) != null) {
        ConfigurationBuilder build = new ConfigurationBuilder();
        build.setJSONStoreEnabled(true);
        build.setOAuthAccessToken(
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN).toString());
        build.setOAuthAccessTokenSecret(messageContext
                .getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN_SECRET).toString());
        build.setOAuthConsumerKey(
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_KEY).toString());
        build.setOAuthConsumerSecret(
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_SECRET).toString());
        twitter = new TwitterFactory(build.build()).getInstance();
        twitter.verifyCredentials();
    } else {
        twitter = new TwitterFactory().getInstance();
    }
    return twitter;
}

From source file:SentimentAnalyses.PrintSampleStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    final PrintSampleStream pr = new PrintSampleStream();

    try {//from  w  ww  .  ja v a 2s  . c  o m
        pr.LinkMongodb();
    } catch (Exception e) {
        e.printStackTrace();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Em3WTI7jc90HcvKzPkTLQ")
            .setOAuthConsumerSecret("vg4p6rOF32bmffqRR8m0jAUClrxvtGiMB5PrSr3Zsw")
            .setOAuthAccessToken("1681973072-1q0zI0VPjHD3ttNuaBOL94frzCI9sXInxAcDK0w")
            .setOAuthAccessTokenSecret("ZRLkOyjmhHBkU1iNyEVNyIgIBsKrl0DUDKOcOMneYFYEM");
    cb.setJSONStoreEnabled(true);

    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    TwitterStream twitterStream = tf.getInstance();
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            //System.out.println(status);
            String str = DataObjectFactory.getRawJSON(status);
            try {
                //JSONObject nnstr = new JSONObject(newstr);
                DBObject dbObject = (DBObject) JSON.parse(str);
                //                    System.out.println(dbObject);
                pr.collection.insert(dbObject);
                //System.out.println(dbObject);
                pr.count++;
                if (pr.count % 1000 == 0)
                    System.out.println(pr.count);
                if (pr.count > 100000) {
                    pr.mongo.close();
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);

    String[] trackArray;
    String[] Track = { "Malaysia Airlines", "Flight MH370", "Boeing-777", "Kuala Lumpur", "Bei jing" };
    //trackArray[0] = "Obama";
    //trackArray[1] = "Romney";

    FilterQuery filter = new FilterQuery();
    filter.track(Track);
    String[] lang = { "en" };
    filter.language(lang);
    twitterStream.filter(filter);
    //pr.mongo.close();
}

From source file: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.
 *
 * @param context Key-value store used to pass configuration information
 * throughout the system.//from  w  w w . jav a  2  s . c o m
 */
@Override
public void configure(Context context) {

    consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY);
    consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET);
    accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN);
    accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET);

    String swString = context.getString(TwitterSourceConstants.SW_LNG_LAT);
    String neString = context.getString(TwitterSourceConstants.NE_LNG_LAT);
    if (swString != null && neString != null) {
        String[] sw = swString.split(",");
        String[] ne = neString.split(",");
        if (sw.length == 2 && ne.length == 2) {
            for (int i = 0; i < 2; i++) {
                locations[0][i] = Double.parseDouble(sw[i].trim());
                locations[1][i] = Double.parseDouble(ne[i].trim());
            }
        } else {
            locations = null;
        }
    } else {
        locations = null;
    }

    String keywordString = context.getString(TwitterSourceConstants.KEYWORDS);
    if (keywordString != null) {
        keywords = keywordString.split(",");
        for (int i = 0; i < keywords.length; i++) {
            keywords[i] = keywords[i].trim();
        }
    }

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

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

}

From source file:stream.PrintUserStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    ConfigurationBuilder confbuilder = new ConfigurationBuilder();
    confbuilder.setOAuthAccessToken(TWITTER_ACCESS_TOKEN).setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET)
            .setOAuthConsumerKey(TWITTER_CONSUMER_KEY).setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
    confbuilder.setJSONStoreEnabled(true);

    TwitterStream twitterStream = new TwitterStreamFactory(confbuilder.build()).getInstance();
    //        TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    // user() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.user();//  w  w  w .ja  v a  2  s  .  com
}

From source file:Twitter.FilterStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    System.getProperties().put("http.proxyHost", "127.0.0.1");
    System.getProperties().put("http.proxyPort", "8580");

    /* if (args.length < 1) {
    System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
    System.exit(-1);/*from   ww w .  j  av a  2 s . c  om*/
     }*/

    final FilterStream fs = new FilterStream();

    try {
        fs.LinkMongodb();
    } catch (Exception e) {
        e.printStackTrace();
    }

    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            String str = DataObjectFactory.getRawJSON(status);
            try {
                //JSONObject nnstr = new JSONObject(newstr);  
                DBObject dbObject = (DBObject) JSON.parse(str);
                fs.collection.insert(dbObject);
                //System.out.println(dbObject);  
                fs.count++;
                if (fs.count > 900000000) {
                    fs.mongo.close();
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("7ZVgfKiOvBDcDFpytRWSA")
            .setOAuthConsumerSecret("JmeJVeym78arzmGthrDUshQyhkq6nWA9tWLUKxc")
            .setOAuthAccessToken("321341780-Zy7LptVYBZBVvAeQ5GFJ4aKFw8sdqhWBnvA3pDuO")
            .setOAuthAccessTokenSecret("foi8FnQCeN0J5cdwad05Q6d7dbytFayQn1ZOvmhF6Qc");
    cb.setJSONStoreEnabled(true);

    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());

    TwitterStream twitterStream = tf.getInstance();
    twitterStream.addListener(listener);
    ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();

    //String[] keywords = {"RT @justinbieber"};
    String[] keywords = { "27260086" }; // user_id(justinbieber)
    for (String arg : keywords) {
        if (isNumericalArgument(arg)) {
            for (String id : arg.split(",")) {
                follow.add(Long.parseLong(id));
            }
        } else {
            track.addAll(Arrays.asList(arg.split(",")));
        }
    }
    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
        followArray[i] = follow.get(i);
    }
    String[] trackArray = track.toArray(new String[track.size()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    //twitterStream.filter(new FilterQuery(0, followArray, trackArray));
    twitterStream.filter(new FilterQuery(followArray));
}