Example usage for twitter4j TwitterStreamFactory TwitterStreamFactory

List of usage examples for twitter4j TwitterStreamFactory TwitterStreamFactory

Introduction

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

Prototype

public TwitterStreamFactory(String configTreePath) 

Source Link

Document

Creates a TwitterStreamFactory with a specified config tree.

Usage

From source file:org.apache.apex.malhar.contrib.twitter.TwitterSampleInput.java

License:Apache License

@Override
public void setup(OperatorContext context) {
    operatorThread = Thread.currentThread();

    if (feedMultiplier != 1) {
        logger.info("Load set to be {}% of the entire twitter feed", feedMultiplier);
    }//from   www  .  ja va  2  s. c  o m

    ConfigurationBuilder cb = setupConfigurationBuilder();
    ts = new TwitterStreamFactory(cb.build()).getInstance();
}

From source file:org.apache.apex.malhar.contrib.twitter.TwitterSampleInput.java

License:Apache License

private void setUpTwitterConnection() {
    ConfigurationBuilder cb = setupConfigurationBuilder();
    ts = new TwitterStreamFactory(cb.build()).getInstance();
    ts.addListener(TwitterSampleInput.this);
    // we can only listen to tweets containing links by callng ts.links().
    // it seems it requires prior signed agreement with twitter.
    ts.sample();/* ww  w .j  av a2s .c om*/
}

From source file:org.apache.camel.component.twitter.TwitterConfiguration.java

License:Apache License

public TwitterStream createTwitterStream() {
    if (twitterStream == null) {
        twitterStream = new TwitterStreamFactory(getConfiguration()).getInstance();
    }//from   w  ww  .j a va2  s  .  c  o m

    return twitterStream;
}

From source file:org.apache.s4.example.twitter.TwitterInputAdapter.java

License:Apache License

public void connectAndRead() throws Exception {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    Properties twitterProperties = new Properties();
    File twitter4jPropsFile = new File(System.getProperty("user.home") + "/twitter4j.properties");
    if (!twitter4jPropsFile.exists()) {
        logger.error(/*from   w  ww .  j  a  v a2 s.c o  m*/
                "Cannot find twitter4j.properties file in this location :[{}]. Make sure it is available at this place and includes user/password credentials",
                twitter4jPropsFile.getAbsolutePath());
        return;
    }
    twitterProperties.load(new FileInputStream(twitter4jPropsFile));

    cb.setDebugEnabled(Boolean.valueOf(twitterProperties.getProperty("debug")))
            .setUser(twitterProperties.getProperty("user"))
            .setPassword(twitterProperties.getProperty("password"));
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener statusListener = new StatusListener() {

        @Override
        public void onException(Exception ex) {
            logger.error("error", ex);
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            logger.error("error");
        }

        @Override
        public void onStatus(Status status) {
            messageQueue.add(status);

        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            logger.error("error");
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            logger.error("error");
        }
    };
    twitterStream.addListener(statusListener);
    twitterStream.sample();

}

From source file:org.apache.storm.starter.spout.Q2TwitterSampleSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;/*from   w w  w.  ja v a 2 s  .c  om*/

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            queue.offer(status);
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
        }

        @Override
        public void onTrackLimitationNotice(int i) {
        }

        @Override
        public void onScrubGeo(long l, long l1) {
        }

        @Override
        public void onException(Exception ex) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

        }

    };

    _twitterStream = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build())
            .getInstance();

    _twitterStream.addListener(listener);
    _twitterStream.setOAuthConsumer(consumerKey, consumerSecret);
    AccessToken token = new AccessToken(accessToken, accessTokenSecret);
    _twitterStream.setOAuthAccessToken(token);

    if (keyWords.length == 0) {

        _twitterStream.sample();
    }

    else {

        FilterQuery query = new FilterQuery().track(keyWords);
        query.language(new String[] { "en" });
        query.track(new String[] { "job", "vote", "local", "Clinton", "Trump", "debate", "tech",
                "TheWalkingDead", "android", "Election", "USA", "GOT7", "Empire", "Apple", "ISIS", "election",
                "Football", "iPhone", "Amazon", "uber" });
        _twitterStream.filter(query);
    }

}

From source file:org.elasticsearch.river.twitter.TwitterRiver.java

License:Apache License

/**
 * Twitter Stream Builder/*from   w  w  w  .  j  a  v  a 2s.  com*/
 * @return
 */
private TwitterStream buildTwitterStream() {
    logger.debug("creating TwitterStreamFactory");
    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);

    // We force SSL usage
    cb.setUseSSL(true);

    TwitterStream stream = new TwitterStreamFactory(cb.build()).getInstance();
    stream.addListener(new StatusHandler());

    return stream;
}

From source file:org.graylog2.inputs.twitter.TwitterTransport.java

License:Open Source License

@Override
public void launch(final MessageInput input) throws MisfireException {
    final ConfigurationBuilder cb = new ConfigurationBuilder()
            .setOAuthConsumerKey(configuration.getString(CK_OAUTH_CONSUMER_KEY))
            .setOAuthConsumerSecret(configuration.getString(CK_OAUTH_CONSUMER_SECRET))
            .setOAuthAccessToken(configuration.getString(CK_OAUTH_ACCESS_TOKEN))
            .setOAuthAccessTokenSecret(configuration.getString(CK_OAUTH_ACCESS_TOKEN_SECRET))
            .setJSONStoreEnabled(true);/*from  w  ww.  java  2s  .co m*/

    final StatusListener listener = new StatusListener() {
        public void onStatus(final Status status) {
            try {
                input.processRawMessage(createMessageFromStatus(status));
            } catch (IOException e) {
                LOG.debug("Error while processing tweet status", e);
            }
        }

        private RawMessage createMessageFromStatus(final Status status) throws IOException {
            return new RawMessage(TwitterObjectFactory.getRawJSON(status).getBytes(StandardCharsets.UTF_8));
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onException(final Exception ex) {
            LOG.error("Error while reading Twitter stream", ex);
        }

        @Override
        public void onScrubGeo(long lon, long lat) {
        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {
            LOG.info("Stall warning: {} ({}% full)", stallWarning.getMessage(), stallWarning.getPercentFull());
        }
    };

    final String[] track = Iterables.toArray(
            Splitter.on(',').omitEmptyStrings().trimResults().split(configuration.getString(CK_KEYWORDS)),
            String.class);
    final FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(track);

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

    twitterStream.addListener(listener);
    twitterStream.filter(filterQuery);
}

From source file:org.hubiquitus.hubotsdk.adapters.HTwitterAdapterInbox.java

License:Open Source License

/**
 * Function for tweet Streaming/* w w  w .ja  v  a 2  s.  c  o  m*/
 */
private void stream() {
    /**
     * Configuration for access to twitter account
     */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setUseSSL(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(twitterAccessToken)
            .setOAuthAccessTokenSecret(twitterAccessTokenSecret);

    //Instantiation of tweet stream
    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {
        public void onStatus(Status tweet) {
            String lang = tweet.getUser().getLang();
            //Set the filter for the language
            if ((langFilter == null) || (lang != null && lang.equalsIgnoreCase(langFilter))) {
                HMessage message = transformtweet(tweet);
                put(message);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
            log.info("message: ", ex);
        }
    };

    FilterQuery fq = new FilterQuery();
    fq.track(tags.split(","));
    twitterStream.addListener(listener);
    twitterStream.filter(fq);
}

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

License:Apache License

/**
 * Start twitter stream//w w w  .j  a va 2 s . com
 */
private void startTwitterStream() {
    logger.info("starting {} twitter stream", streamType);

    if (stream == null) {
        logger.debug("creating twitter stream");

        stream = new TwitterStreamFactory(buildTwitterConfiguration()).getInstance();
        if (streamType.equals("user")) {
            stream.addListener(new UserStreamHandler());
        } else {
            stream.addListener(new StatusHandler());
        }

        logger.debug("twitter stream created");
    }

    if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
        if (streamType.equals("filter") || filterQuery != null) {
            stream.filter(filterQuery);
        } else if (streamType.equals("firehose")) {
            stream.firehose(0);
        } else if (streamType.equals("user")) {
            stream.user();
        } else {
            stream.sample();
        }
    }
    logger.debug("{} twitter stream started!", streamType);
}

From source file:org.mule.twitter.TwitterConnector.java

License:Open Source License

private TwitterStream newStream() {
    ConfigurationBuilder cb = new ConfigurationBuilder().setUseSSL(useSSL).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setStreamBaseURL(getStreamBaseUrl())
            .setSiteStreamBaseURL(getSiteStreamBaseUrl()).setHttpProxyHost(proxyHost)
            .setHttpProxyPort(proxyPort).setHttpProxyUser(proxyUsername).setHttpProxyPassword(proxyPassword);

    if (getAccessToken() != null) {
        cb.setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);
    }// w  w w .  j a v  a  2 s.  c  o m

    HttpClientHiddenConstructionArgument.setUseMule(false);
    return new TwitterStreamFactory(cb.build()).getInstance();
}