Example usage for twitter4j FilterQuery language

List of usage examples for twitter4j FilterQuery language

Introduction

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

Prototype

String[] language

To view the source code for twitter4j FilterQuery language.

Click Source Link

Usage

From source file:adapter.TwitterLanguageAdapter.java

License:Apache License

public void connectAndRead() throws Exception {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    Properties twitterProperties = new Properties();
    File twitter4jPropsFile = new File("../twitter4j.properties");
    if (!twitter4jPropsFile.exists()) {
        logger.error("Cannot find twitter4j.properties file in this location :[{}]",
                twitter4jPropsFile.getAbsolutePath());
        return;//from   w  ww  . j a  va 2s  .  c om
    }

    twitterProperties.load(new FileInputStream(twitter4jPropsFile));

    cb = new ConfigurationBuilder();

    cb.setOAuthConsumerKey(twitterProperties.getProperty("oauth.consumerKey"));
    cb.setOAuthConsumerSecret(twitterProperties.getProperty("oauth.consumerSecret"));
    cb.setOAuthAccessToken(twitterProperties.getProperty("oauth.accessToken"));
    cb.setOAuthAccessTokenSecret(twitterProperties.getProperty("oauth.accessTokenSecret"));

    cb.setDebugEnabled(false);
    cb.setPrettyDebugEnabled(false);
    cb.setIncludeMyRetweetEnabled(false);

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

    StatusListener statusListener = new StatusListener() {
        @Override
        public void onException(Exception ex) {
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

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

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

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

    };

    //Filter language and track
    FilterQuery tweetFilterQuery = new FilterQuery();
    tweetFilterQuery.track(new String[] { "palabra1,palabra2,palabra3" });
    tweetFilterQuery.language(new String[] { "es" });

    //TwitterStream
    twitterStream.addListener(statusListener);
    twitterStream.filter(tweetFilterQuery);
}

From source file:at.aictopic1.twitter.AICStream.java

private FilterQuery getQuery() {
    FilterQuery query = new FilterQuery();
    String[] track = this.track.toArray(new String[this.track.size()]);
    query.track(track);/*from w  w w.j  av a 2  s .  co m*/
    String[] lang = this.lang.toArray(new String[this.lang.size()]);
    query.language(lang);

    return query;
}

From source file:at.illecker.sentistorm.spout.TwitterStreamSpout.java

License:Apache License

@Override
public void open(Map config, TopologyContext context, SpoutOutputCollector collector) {
    m_collector = collector;//from   w  ww  .  jav a2  s .  c o m
    m_tweetsQueue = new LinkedBlockingQueue<Status>(1000);

    // Optional startup sleep to finish bolt preparation
    // before spout starts emitting
    if (config.get(CONF_STARTUP_SLEEP_MS) != null) {
        long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS);
        TimeUtils.sleepMillis(startupSleepMillis);
    }

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

    // Set Listener
    twitterStream.addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            m_tweetsQueue.offer(status); // add tweet into queue
        }

        @Override
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

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

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
    });

    // Set credentials
    twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret);
    AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    // Filter twitter stream
    FilterQuery tweetFilterQuery = new FilterQuery();
    if (m_keyWords != null) {
        tweetFilterQuery.track(m_keyWords);
    }

    // Filter location
    // https://dev.twitter.com/docs/streaming-apis/parameters#locations
    tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any geotagged tweet

    // Filter language
    tweetFilterQuery.language(new String[] { m_filterLanguage });

    twitterStream.filter(tweetFilterQuery);
}

From source file:at.storm.spout.TwitterStreamSpout.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override//from  w w  w .  j  av  a 2 s.  co  m
public void open(Map config, TopologyContext context, SpoutOutputCollector collector) {
    m_collector = collector;
    m_tweetsQueue = new LinkedBlockingQueue<Status>(1000);

    // Optional startup sleep to finish bolt preparation
    // before spout starts emitting
    if (config.get(CONF_STARTUP_SLEEP_MS) != null) {
        long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS);
        TimeUtils.sleepMillis(startupSleepMillis);
    }

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

    // Set Listener
    twitterStream.addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            m_tweetsQueue.offer(status); // add tweet into queue
        }

        @Override
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

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

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
    });

    // Set credentials
    twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret);
    AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    // Filter twitter stream
    FilterQuery tweetFilterQuery = new FilterQuery();
    if (m_keyWords != null) {
        tweetFilterQuery.track(m_keyWords);
    }

    // Filter location
    // https://dev.twitter.com/docs/streaming-apis/parameters#locations
    tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any
    // geotagged
    // tweet

    // Filter language
    tweetFilterQuery.language(new String[] { m_filterLanguage });

    twitterStream.filter(tweetFilterQuery);
}

From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.StreamReaderService.java

public void readTwitterFeed() {

    TwitterStream stream = TwitterStreamBuilderUtil.getStream();

    StatusListener listener = new StatusListener() {

        @Override//from   ww  w.  j a va  2 s  .c o  m
        public void onException(Exception e) {
        }

        @Override
        public void onTrackLimitationNotice(int n) {
        }

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("en")) {
                System.out.println(status.getText());
            }
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

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

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }
    };

    stream.addListener(listener);
    FilterQuery f = new FilterQuery();
    f.language(new String[] { "en" });
    f.follow(new long[] { 816653 });
    stream.filter(f);

    try {
        Thread.sleep(60000);
    } catch (InterruptedException ex) {
        Logger.getLogger(StreamReaderService.class.getName()).log(Level.SEVERE, null, ex);
    }
    stream.shutdown();
}

From source file:com.left8.evs.utilities.dsretriever.TweetsRetriever.java

License:Open Source License

/**
 * Method that handles the Twitter streaming API. <br>
 * <b>WARNING:</b> Method does not terminate by itself, due to the fact that
 * the streamer runs in a different thread.
 * @param keywords The keywords for which the streamer searches for tweets.
 * @param mongoDB A handler for the MongoDB database.
 * @param config A configuration object.
 *//*from w ww . ja  v  a  2  s. c o m*/
public final void retrieveTweetsWithStreamingAPI(String[] keywords, MongoHandler mongoDB, Config config) {

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

    final StatusListener listener;
    listener = new StatusListener() {

        @Override
        public final void onStatus(Status status) {
            //Insert tweet to MongoDB
            mongoDB.insertSingleTweetIntoMongoDB(status, "NULL");
        }

        @Override
        public final void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            Utilities.printMessageln("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public final void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            Utilities.printMessageln("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public final void onScrubGeo(long userId, long upToStatusId) {
            Utilities.printMessageln("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public final void onStallWarning(StallWarning warning) {
            Utilities.printMessageln("Got stall warning:" + warning);
        }

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

    FilterQuery fq = new FilterQuery();
    fq.language("en"); //Set language of tweets to "English"
    fq.track(keywords); //Load the search terms

    twitterStream.addListener(listener); //Start listening to the stream
    twitterStream.filter(fq); //Apply the search filters
}

From source file:com.producer.TwitterProducer.java

License:Apache License

public static void main(String[] args) {

    if (args.length == 0) {
        System.out.println(/*  w  w w.j av  a  2s.  c  o m*/
                "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.saugereau.spout.TwitterSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;//from  ww w.j a v  a2s .  co m

    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) {
            LOG.info("Got track limitation notice:" + i);
        }

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

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

        @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" });
        _twitterStream.filter(query);
    }

}

From source file:com.wso2.stream.connector.protocol.TwitterPollingConsumer.java

License:Open Source License

/**
 * Setting up a connection with Twitter Stream API with the given credentials
 *///from   w  w w.ja  v  a 2 s. c  o m
private void setupConnection() {
    StatusListener listener = new StatusListenerImpl();

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);

    TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
    twitterStream.addListener(listener);

    FilterQuery query = new FilterQuery();
    query.language(new String[] { "en" });
    query.track(filterTags);
    twitterStream.filter(query);
}

From source file:com.yoshio3.javaee7.sampleapp.TwitterStreamImpl.java

public void initTwitterStream(Session session, String filterString) {
    this.session = session;

    //Twitter Stream ??
    twStream = TwitterStreamFactory.getSingleton();
    FilterQuery filter = new FilterQuery();
    filter.track(new String[] { filterString });
    filter.language(new String[] { "ja" });
    twStream.addListener(this);
    twStream.filter(filter);/*from w w w  .  j a  v  a 2  s.  c  o  m*/
}