Example usage for twitter4j TwitterStream sample

List of usage examples for twitter4j TwitterStream sample

Introduction

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

Prototype

TwitterStream sample(final String language);

Source Link

Document

Starts listening on random sample of all public statuses.

Usage

From source file:StreamToFiles.java

License:Apache License

public static void main(String[] args) throws TwitterException, FileNotFoundException {
    String oAuthFileName = "mjlauKeys.auth";
    outFile = args[0];/*  w  w  w  .ja  va2s  . c  om*/

    ConfigurationBuilder cb = Utils.createConfigurationBuilder(new File(oAuthFileName));
    if (cb == null) {
        throw new IllegalArgumentException("Configuration File Issues");
    }

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

    StatusListener listener = new StatusListener() {
        long counter = 0;
        PrintWriter outputWriter = new PrintWriter(new File(outFile));

        @Override
        public void onStatus(Status status) {
            // System.out.println("@" + status.getUser().getScreenName()
            // + " - " + status.getText());
            if (status.getText().contains("#")) {
                Utils.sanitizeAndWriteTweet(status.getText(), outputWriter);
                counter++;
                if (counter % 1000 == 0) {
                    System.out.println("done with " + counter);
                }
                if (counter >= NUM_TWEETS) {
                    twitterStream.cleanUp();
                    twitterStream.shutdown();
                    outputWriter.close();
                }

            }
        }

        @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);
    twitterStream.sample("en");
}

From source file:org.wso2.carbon.inbound.custom.poll.TwitterStreamData.java

License:Open Source License

/**
 * Setting up a connection with Twitter Stream API with the given
 * credentials/*  w  w  w  .j  a  v  a  2  s . com*/
 *
 * @throws TwitterException
 */
private void setupConnection() throws TwitterException {
    if (log.isDebugEnabled()) {
        log.debug("Starting to setup the connection with the twitter streaming endpoint");
    }
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);
    StatusListener statusStreamsListener;
    UserStreamListener userStreamListener;
    SiteStreamsListener siteStreamslistener;
    TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
    String twitterOperation = properties.getProperty(TwitterConstant.TWITTER_OPERATION);
    if (twitterOperation.equals(TwitterConstant.FILTER_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.LINK_STREAM_OPERATION)
            || twitterOperation.equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) {
        statusStreamsListener = new StatusListenerImpl();
        twitterStream.addListener(statusStreamsListener);
    } else if (twitterOperation.equals(TwitterConstant.USER_STREAM_OPERATION)) {
        userStreamListener = new UserStreamListenerImpl();
        twitterStream.addListener(userStreamListener);
    } else if (twitterOperation.equals(TwitterConstant.SITE_STREAM_OPERATION)) {
        siteStreamslistener = new siteStreamsListenerImpl();
        twitterStream.addListener(siteStreamslistener);
    } else {
        handleException("The operation :" + twitterOperation + " not found");
    }

    /* Synchronously retrieves public statuses that match one or more filter predicates.*/
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.FILTER_STREAM_OPERATION)) {
        FilterQuery query = new FilterQuery();
        if (languages != null) {
            query.language(languages);
        }
        if (tracks != null) {
            query.track(tracks);
        }
        if (follow != null) {
            query.follow(follow);
        }
        if (filterLevel != null) {
            query.filterLevel(filterLevel);
        }
        query.count(count);
        twitterStream.filter(query);
    }

    /* Returns a small random sample of all public statuses. */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) {

        if (languages != null) {
            if (languages.length == 1) {
                twitterStream.sample(languages[1]);
            } else {
                handleException("A language can be used for the sample operation");
            }
        }
        twitterStream.sample();
    }
    /* Asynchronously retrieves all public statuses.*/
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)) {
        if (countParam != null) {
            twitterStream.firehose(count);
        }
    }
    /*
     User Streams provide a stream of data and events specific to the
     authenticated user.This provides to access the Streams messages for a
     single user.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.USER_STREAM_OPERATION)) {
        if (tracks != null) {
            twitterStream.user(tracks);
        }
        twitterStream.user();
    }
    /*
     * Link Streams provide asynchronously retrieves all statuses containing 'http:' and 'https:'.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.LINK_STREAM_OPERATION)) {
        if (countParam != null) {
            twitterStream.links(count);
        }
    }
    /*
     * User Streams provide a stream of data and events specific to the
     * authenticated user.This provides to access the Streams messages for a
     * single user.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.RETWEET_STREAM_OPERATION)) {
        twitterStream.retweet();
    }
    /*
     * Site Streams allows services, such as web sites or mobile push
     * services, to receive real-time updates for a large number of users.
     * Events may be streamed for any user who has granted OAuth access to
     * your application. Desktop applications or applications with few users
     * should use user streams.
     */
    if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION))
            .equals(TwitterConstant.SITE_STREAM_OPERATION)) {
        twitterStream.site(withFollowings, follow);
    }

}