Example usage for twitter4j FilterQuery FilterQuery

List of usage examples for twitter4j FilterQuery FilterQuery

Introduction

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

Prototype

public FilterQuery() 

Source Link

Document

Creates a new FilterQuery

Usage

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;/* w w w.  j  ava2  s  . c o 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) {
        }

        @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

@SuppressWarnings({ "unchecked" })
@Inject// w  w  w  . jav a 2 s.c  o m
public TwitterRiver(RiverName riverName, RiverSettings settings, Client client, ThreadPool threadPool) {
    super(riverName, settings);
    this.client = client;
    this.threadPool = threadPool;

    if (settings.settings().containsKey("twitter")) {
        Map<String, Object> twitterSettings = (Map<String, Object>) settings.settings().get("twitter");

        // Check removed properties
        if (twitterSettings.get("user") != null || twitterSettings.get("password") != null) {
            logger.warn(
                    "user and password are not supported anymore. See https://github.com/elasticsearch/elasticsearch-river-twitter/issues/28");
        }

        raw = XContentMapValues.nodeBooleanValue(twitterSettings.get("raw"), false);
        ignoreRetweet = XContentMapValues.nodeBooleanValue(twitterSettings.get("ignore_retweet"), false);

        if (twitterSettings.containsKey("oauth")) {
            Map<String, Object> oauth = (Map<String, Object>) twitterSettings.get("oauth");
            if (oauth.containsKey("consumerKey")) {
                oauthConsumerKey = XContentMapValues.nodeStringValue(oauth.get("consumerKey"), null);
            }
            if (oauth.containsKey("consumer_key")) {
                oauthConsumerKey = XContentMapValues.nodeStringValue(oauth.get("consumer_key"), null);
            }
            if (oauth.containsKey("consumerSecret")) {
                oauthConsumerSecret = XContentMapValues.nodeStringValue(oauth.get("consumerSecret"), null);
            }
            if (oauth.containsKey("consumer_secret")) {
                oauthConsumerSecret = XContentMapValues.nodeStringValue(oauth.get("consumer_secret"), null);
            }
            if (oauth.containsKey("accessToken")) {
                oauthAccessToken = XContentMapValues.nodeStringValue(oauth.get("accessToken"), null);
            }
            if (oauth.containsKey("access_token")) {
                oauthAccessToken = XContentMapValues.nodeStringValue(oauth.get("access_token"), null);
            }
            if (oauth.containsKey("accessTokenSecret")) {
                oauthAccessTokenSecret = XContentMapValues.nodeStringValue(oauth.get("accessTokenSecret"),
                        null);
            }
            if (oauth.containsKey("access_token_secret")) {
                oauthAccessTokenSecret = XContentMapValues.nodeStringValue(oauth.get("access_token_secret"),
                        null);
            }
        }
        if (twitterSettings.containsKey("proxy")) {
            Map<String, Object> proxy = (Map<String, Object>) twitterSettings.get("proxy");
            if (proxy.containsKey("host")) {
                proxyHost = XContentMapValues.nodeStringValue(proxy.get("host"), null);
            }
            if (proxy.containsKey("port")) {
                proxyPort = XContentMapValues.nodeStringValue(proxy.get("port"), null);
            }
            if (proxy.containsKey("user")) {
                proxyUser = XContentMapValues.nodeStringValue(proxy.get("user"), null);
            }
            if (proxy.containsKey("password")) {
                proxyPassword = XContentMapValues.nodeStringValue(proxy.get("password"), null);
            }
        }
        streamType = XContentMapValues.nodeStringValue(twitterSettings.get("type"), "sample");
        Map<String, Object> filterSettings = (Map<String, Object>) twitterSettings.get("filter");

        if (streamType.equals("filter") && filterSettings == null) {
            stream = null;
            indexName = null;
            typeName = "status";
            bulkSize = 100;
            this.maxConcurrentBulk = 1;
            this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
            logger.warn("no filter defined for type filter. Disabling river...");
            return;
        }

        if (filterSettings != null) {
            streamType = "filter";
            filterQuery = new FilterQuery();
            filterQuery.count(XContentMapValues.nodeIntegerValue(filterSettings.get("count"), 0));
            Object tracks = filterSettings.get("tracks");
            boolean filterSet = false;
            if (tracks != null) {
                if (tracks instanceof List) {
                    List<String> lTracks = (List<String>) tracks;
                    filterQuery.track(lTracks.toArray(new String[lTracks.size()]));
                } else {
                    filterQuery.track(Strings.commaDelimitedListToStringArray(tracks.toString()));
                }
                filterSet = true;
            }
            Object follow = filterSettings.get("follow");
            if (follow != null) {
                if (follow instanceof List) {
                    List lFollow = (List) follow;
                    long[] followIds = new long[lFollow.size()];
                    for (int i = 0; i < lFollow.size(); i++) {
                        Object o = lFollow.get(i);
                        if (o instanceof Number) {
                            followIds[i] = ((Number) o).intValue();
                        } else {
                            followIds[i] = Long.parseLong(o.toString());
                        }
                    }
                    filterQuery.follow(followIds);
                } else {
                    String[] ids = Strings.commaDelimitedListToStringArray(follow.toString());
                    long[] followIds = new long[ids.length];
                    for (int i = 0; i < ids.length; i++) {
                        followIds[i] = Long.parseLong(ids[i]);
                    }
                    filterQuery.follow(followIds);
                }
                filterSet = true;
            }
            Object locations = filterSettings.get("locations");
            if (locations != null) {
                if (locations instanceof List) {
                    List lLocations = (List) locations;
                    double[][] dLocations = new double[lLocations.size()][];
                    for (int i = 0; i < lLocations.size(); i++) {
                        Object loc = lLocations.get(i);
                        double lat;
                        double lon;
                        if (loc instanceof List) {
                            List lLoc = (List) loc;
                            if (lLoc.get(0) instanceof Number) {
                                lon = ((Number) lLoc.get(0)).doubleValue();
                            } else {
                                lon = Double.parseDouble(lLoc.get(0).toString());
                            }
                            if (lLoc.get(1) instanceof Number) {
                                lat = ((Number) lLoc.get(1)).doubleValue();
                            } else {
                                lat = Double.parseDouble(lLoc.get(1).toString());
                            }
                        } else {
                            String[] sLoc = Strings.commaDelimitedListToStringArray(loc.toString());
                            lon = Double.parseDouble(sLoc[0]);
                            lat = Double.parseDouble(sLoc[1]);
                        }
                        dLocations[i] = new double[] { lon, lat };
                    }
                    filterQuery.locations(dLocations);
                } else {
                    String[] sLocations = Strings.commaDelimitedListToStringArray(locations.toString());
                    double[][] dLocations = new double[sLocations.length / 2][];
                    int dCounter = 0;
                    for (int i = 0; i < sLocations.length; i++) {
                        double lon = Double.parseDouble(sLocations[i]);
                        double lat = Double.parseDouble(sLocations[++i]);
                        dLocations[dCounter++] = new double[] { lon, lat };
                    }
                    filterQuery.locations(dLocations);
                }
                filterSet = true;
            }
            Object language = filterSettings.get("language");
            if (language != null) {
                if (filterSet) {
                    if (language instanceof List) {
                        List<String> lLanguage = (List<String>) language;
                        filterQuery.language(lLanguage.toArray(new String[lLanguage.size()]));
                    } else {
                        filterQuery.language(Strings.commaDelimitedListToStringArray(language.toString()));
                    }
                } else {
                    indexName = null;
                    typeName = "status";
                    bulkSize = 100;
                    this.maxConcurrentBulk = 1;
                    this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
                    logger.warn(
                            "can not set language filter without tracks, follow or locations. Disabling river.");
                    return;
                }
            }
        }
    }

    logger.info("creating twitter stream river");
    if (raw && logger.isDebugEnabled()) {
        logger.debug("will index twitter raw content...");
    }

    if (oauthAccessToken == null && oauthConsumerKey == null && oauthConsumerSecret == null
            && oauthAccessTokenSecret == null) {
        stream = null;
        indexName = null;
        typeName = "status";
        bulkSize = 100;
        this.maxConcurrentBulk = 1;
        this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
        logger.warn("no oauth specified, disabling river...");
        return;
    }

    if (settings.settings().containsKey("index")) {
        Map<String, Object> indexSettings = (Map<String, Object>) settings.settings().get("index");
        indexName = XContentMapValues.nodeStringValue(indexSettings.get("index"), riverName.name());
        typeName = XContentMapValues.nodeStringValue(indexSettings.get("type"), "status");
        this.bulkSize = XContentMapValues.nodeIntegerValue(indexSettings.get("bulk_size"), 100);
        this.bulkFlushInterval = TimeValue.parseTimeValue(
                XContentMapValues.nodeStringValue(indexSettings.get("flush_interval"), "5s"),
                TimeValue.timeValueSeconds(5));
        this.maxConcurrentBulk = XContentMapValues.nodeIntegerValue(indexSettings.get("max_concurrent_bulk"),
                1);
    } else {
        indexName = riverName.name();
        typeName = "status";
        bulkSize = 100;
        this.maxConcurrentBulk = 1;
        this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
    }

    stream = buildTwitterStream();
}

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 w  w.ja v  a 2  s. 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 2s.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

@SuppressWarnings({ "unchecked", "rawtypes" })
@Inject/*w w  w  .  j  av a  2  s  .co m*/
public TwitterRiver(RiverName riverName, RiverSettings riverSettings, Client client, ThreadPool threadPool,
        Settings settings) {
    super(riverName, riverSettings);
    this.riverStatus = RiverStatus.UNKNOWN;
    this.client = client;
    this.threadPool = threadPool;

    String riverStreamType;

    if (riverSettings.settings().containsKey("twitter")) {
        Map<String, Object> twitterSettings = (Map<String, Object>) riverSettings.settings().get("twitter");

        raw = XContentMapValues.nodeBooleanValue(twitterSettings.get("raw"), false);
        ignoreRetweet = XContentMapValues.nodeBooleanValue(twitterSettings.get("ignore_retweet"), false);
        geoAsArray = XContentMapValues.nodeBooleanValue(twitterSettings.get("geo_as_array"), false);

        if (twitterSettings.containsKey("oauth")) {
            Map<String, Object> oauth = (Map<String, Object>) twitterSettings.get("oauth");
            if (oauth.containsKey("consumer_key")) {
                oauthConsumerKey = XContentMapValues.nodeStringValue(oauth.get("consumer_key"), null);
            } else {
                oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
            }
            if (oauth.containsKey("consumer_secret")) {
                oauthConsumerSecret = XContentMapValues.nodeStringValue(oauth.get("consumer_secret"), null);
            } else {
                oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
            }
            if (oauth.containsKey("access_token")) {
                oauthAccessToken = XContentMapValues.nodeStringValue(oauth.get("access_token"), null);
            } else {
                oauthAccessToken = settings.get("river.twitter.oauth.access_token");
            }
            if (oauth.containsKey("access_token_secret")) {
                oauthAccessTokenSecret = XContentMapValues.nodeStringValue(oauth.get("access_token_secret"),
                        null);
            } else {
                oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
            }
        } else {
            oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
            oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
            oauthAccessToken = settings.get("river.twitter.oauth.access_token");
            oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
        }

        if (twitterSettings.containsKey("retry_after")) {
            retryAfter = XContentMapValues.nodeTimeValue(twitterSettings.get("retry_after"),
                    TimeValue.timeValueSeconds(10));
        } else {
            retryAfter = XContentMapValues.nodeTimeValue(settings.get("river.twitter.retry_after"),
                    TimeValue.timeValueSeconds(10));
        }

        if (twitterSettings.containsKey("proxy")) {
            Map<String, Object> proxy = (Map<String, Object>) twitterSettings.get("proxy");
            proxyHost = XContentMapValues.nodeStringValue(proxy.get("host"), null);
            proxyPort = XContentMapValues.nodeStringValue(proxy.get("port"), null);
            proxyUser = XContentMapValues.nodeStringValue(proxy.get("user"), null);
            proxyPassword = XContentMapValues.nodeStringValue(proxy.get("password"), null);
        } else {
            // Let's see if we have that in node settings
            proxyHost = settings.get("river.twitter.proxy.host");
            proxyPort = settings.get("river.twitter.proxy.port");
            proxyUser = settings.get("river.twitter.proxy.user");
            proxyPassword = settings.get("river.twitter.proxy.password");
        }

        riverStreamType = XContentMapValues.nodeStringValue(twitterSettings.get("type"), "sample");
        Map<String, Object> filterSettings = (Map<String, Object>) twitterSettings.get("filter");

        if (riverStreamType.equals("filter") && filterSettings == null) {
            filterQuery = null;
            stream = null;
            streamType = null;
            indexName = null;
            typeName = "status";
            bulkSize = 100;
            this.maxConcurrentBulk = 1;
            this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
            logger.warn("no filter defined for type filter. Disabling river...");
            return;
        }

        if (filterSettings != null) {
            riverStreamType = "filter";
            filterQuery = new FilterQuery();
            filterQuery.count(XContentMapValues.nodeIntegerValue(filterSettings.get("count"), 0));
            Object tracks = filterSettings.get("tracks");
            boolean filterSet = false;
            if (tracks != null) {
                if (tracks instanceof List) {
                    List<String> lTracks = (List<String>) tracks;
                    filterQuery.track(lTracks.toArray(new String[lTracks.size()]));
                } else {
                    filterQuery.track(Strings.commaDelimitedListToStringArray(tracks.toString()));
                }
                filterSet = true;
            }
            Object follow = filterSettings.get("follow");
            if (follow != null) {
                if (follow instanceof List) {
                    List lFollow = (List) follow;
                    long[] followIds = new long[lFollow.size()];
                    for (int i = 0; i < lFollow.size(); i++) {
                        Object o = lFollow.get(i);
                        if (o instanceof Number) {
                            followIds[i] = ((Number) o).intValue();
                        } else {
                            followIds[i] = Long.parseLong(o.toString());
                        }
                    }
                    filterQuery.follow(followIds);
                } else {
                    String[] ids = Strings.commaDelimitedListToStringArray(follow.toString());
                    long[] followIds = new long[ids.length];
                    for (int i = 0; i < ids.length; i++) {
                        followIds[i] = Long.parseLong(ids[i]);
                    }
                    filterQuery.follow(followIds);
                }
                filterSet = true;
            }
            Object locations = filterSettings.get("locations");
            if (locations != null) {
                if (locations instanceof List) {
                    List lLocations = (List) locations;
                    double[][] dLocations = new double[lLocations.size()][];
                    for (int i = 0; i < lLocations.size(); i++) {
                        Object loc = lLocations.get(i);
                        double lat;
                        double lon;
                        if (loc instanceof List) {
                            List lLoc = (List) loc;
                            if (lLoc.get(0) instanceof Number) {
                                lon = ((Number) lLoc.get(0)).doubleValue();
                            } else {
                                lon = Double.parseDouble(lLoc.get(0).toString());
                            }
                            if (lLoc.get(1) instanceof Number) {
                                lat = ((Number) lLoc.get(1)).doubleValue();
                            } else {
                                lat = Double.parseDouble(lLoc.get(1).toString());
                            }
                        } else {
                            String[] sLoc = Strings.commaDelimitedListToStringArray(loc.toString());
                            lon = Double.parseDouble(sLoc[0]);
                            lat = Double.parseDouble(sLoc[1]);
                        }
                        dLocations[i] = new double[] { lon, lat };
                    }
                    filterQuery.locations(dLocations);
                } else {
                    String[] sLocations = Strings.commaDelimitedListToStringArray(locations.toString());
                    double[][] dLocations = new double[sLocations.length / 2][];
                    int dCounter = 0;
                    for (int i = 0; i < sLocations.length; i++) {
                        double lon = Double.parseDouble(sLocations[i]);
                        double lat = Double.parseDouble(sLocations[++i]);
                        dLocations[dCounter++] = new double[] { lon, lat };
                    }
                    filterQuery.locations(dLocations);
                }
                filterSet = true;
            }
            Object userLists = filterSettings.get("user_lists");
            if (userLists != null) {
                if (userLists instanceof List) {
                    List<String> lUserlists = (List<String>) userLists;
                    String[] tUserlists = lUserlists.toArray(new String[lUserlists.size()]);
                    filterQuery.follow(getUsersListMembers(tUserlists));
                } else {
                    String[] tUserlists = Strings.commaDelimitedListToStringArray(userLists.toString());
                    filterQuery.follow(getUsersListMembers(tUserlists));
                }
                filterSet = true;
            }

            // We should have something to filter
            if (!filterSet) {
                streamType = null;
                indexName = null;
                typeName = "status";
                bulkSize = 100;
                this.maxConcurrentBulk = 1;
                this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
                logger.warn(
                        "can not set language filter without tracks, follow, locations or user_lists. Disabling river.");
                return;
            }

            Object language = filterSettings.get("language");
            if (language != null) {
                if (language instanceof List) {
                    List<String> lLanguage = (List<String>) language;
                    filterQuery.language(lLanguage.toArray(new String[lLanguage.size()]));
                } else {
                    filterQuery.language(Strings.commaDelimitedListToStringArray(language.toString()));
                }
            }
        } else {
            filterQuery = null;
        }
    } else {
        // No specific settings. We need to use some defaults
        riverStreamType = "sample";
        raw = false;
        ignoreRetweet = false;
        geoAsArray = false;
        oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
        oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
        oauthAccessToken = settings.get("river.twitter.oauth.access_token");
        oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
        retryAfter = XContentMapValues.nodeTimeValue(settings.get("river.twitter.retry_after"),
                TimeValue.timeValueSeconds(10));
        filterQuery = null;
        proxyHost = null;
        proxyPort = null;
        proxyUser = null;
        proxyPassword = null;
    }

    if (oauthAccessToken == null || oauthConsumerKey == null || oauthConsumerSecret == null
            || oauthAccessTokenSecret == null) {
        stream = null;
        streamType = null;
        indexName = null;
        typeName = "status";
        bulkSize = 100;
        this.maxConcurrentBulk = 1;
        this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
        logger.warn("no oauth specified, disabling river...");
        return;
    }

    if (riverSettings.settings().containsKey("index")) {
        Map<String, Object> indexSettings = (Map<String, Object>) riverSettings.settings().get("index");
        indexName = XContentMapValues.nodeStringValue(indexSettings.get("index"), riverName.name());
        typeName = XContentMapValues.nodeStringValue(indexSettings.get("type"), "status");
        this.bulkSize = XContentMapValues.nodeIntegerValue(indexSettings.get("bulk_size"), 100);
        this.bulkFlushInterval = TimeValue.parseTimeValue(
                XContentMapValues.nodeStringValue(indexSettings.get("flush_interval"), "5s"),
                TimeValue.timeValueSeconds(5));
        this.maxConcurrentBulk = XContentMapValues.nodeIntegerValue(indexSettings.get("max_concurrent_bulk"),
                1);
    } else {
        indexName = riverName.name();
        typeName = "status";
        bulkSize = 100;
        this.maxConcurrentBulk = 1;
        this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
    }

    logger.info("creating twitter stream river");
    if (raw && logger.isDebugEnabled()) {
        logger.debug("will index twitter raw content...");
    }

    streamType = riverStreamType;
    this.riverStatus = RiverStatus.INITIALIZED;
}

From source file:org.nosceon.titanite.examples.TweetEventSource.java

License:Apache License

@Override
protected void onSubscribe(String id) {
    if (stream == null) {
        stream = new TwitterStreamFactory().getInstance();
        stream.addListener(new StatusAdapter() {

            @Override//from www  .ja v  a 2 s .co m
            public void onStatus(Status status) {
                GeoLocation location = status.getGeoLocation();
                if (location != null) {
                    send("{ \"lat\" : " + location.getLatitude() + ", \"lng\" : " + location.getLongitude()
                            + " }");
                }
            }

        });
        stream.filter(new FilterQuery().locations(RANGE));
    }
}

From source file:org.onepercent.utils.twitterstream.agent.src.main.java.com.cloudera.flume.source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets./*from w w w.  ja va2 s  .c o  m*/
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and Sink,
    // and is used to process events.
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a Twitter
    // stream, and will execute methods every time a message comes in through
    // the stream.
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes in.
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers and
            // the raw JSON of a tweet
            logger.debug(status.getUser().getScreenName() + ": " + status.getText());

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

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

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    if (keywords.length == 0) {
        logger.debug("Starting up Twitter sampling...");
        twitterStream.sample();
    } else if (keywords.length > 0) {
        if (languages.length == 0) {
            logger.debug("Starting up Twitter Keyword filtering...");

            FilterQuery query = new FilterQuery().track(keywords);
            twitterStream.filter(query);
        } else {
            logger.debug("Starting up Twitter Keyword and Language filtering...");

            FilterQuery query = new FilterQuery();
            query.track(keywords);
            query.language(languages);
            twitterStream.filter(query);
        }
    }

    super.start();
}

From source file:org.richfaces.examples.tweetstream.dataserver.listeners.TweetStreamListener.java

License:Open Source License

public void startTwitterStream() {
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(tracks);//from  w w  w  .  j av a2 s.  co m

    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(this);
    twitterStream.filter(filterQuery);
}

From source file:org.rtsa.storm.TwitterSpout.java

License:Apache License

public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;//from  w w  w.  jav  a  2  s .c o m

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {

            if (status.getText().length() != 0 && status.getLang().equals("en")) {
                queue.offer(status);

            }
        }

        public void onDeletionNotice(StatusDeletionNotice sdn) {
        }

        public void onTrackLimitationNotice(int i) {
        }

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

        public void onException(Exception ex) {
        }

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

        }

    };

    TwitterStream 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);
        twitterStream.filter(query);
    }

}

From source file:org.selman.tweetamo.TweetamoClient.java

License:Apache License

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

    if (args.length != 2) {
        System.out.println("Usage: [language] [search topic]");
    }/*from ww w. ja  v a 2  s .  c  om*/

    kinesisClient = new AmazonKinesisClient(new ClasspathPropertiesFileCredentialsProvider());
    waitForStreamToBecomeAvailable(STREAM_NAME);

    LOG.info("Publishing tweets to stream : " + STREAM_NAME);
    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            try {
                PutRecordRequest putRecordRequest = new PutRecordRequest();
                putRecordRequest.setStreamName(STREAM_NAME);
                putRecordRequest.setData(TweetSerializer.toBytes(status));
                putRecordRequest.setPartitionKey(status.getUser().getScreenName());
                PutRecordResult putRecordResult = kinesisClient.putRecord(putRecordRequest);
                LOG.info("Successfully putrecord, partition key : " + putRecordRequest.getPartitionKey()
                        + ", ShardID : " + putRecordResult.getShardId());

            } catch (Exception e) {
                LOG.error("Failed to putrecord", e);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

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

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

        @Override
        public void onStallWarning(StallWarning arg0) {
        }
    };

    ClasspathTwitterCredentialsProvider provider = new ClasspathTwitterCredentialsProvider();
    TwitterCredentials credentials = provider.getTwitterCredentials();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(credentials.getConsumerKey())
            .setOAuthConsumerSecret(credentials.getConsumerSecret())
            .setOAuthAccessToken(credentials.getAccessToken())
            .setOAuthAccessTokenSecret(credentials.getAccessTokenSecret());
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    twitterStream.addListener(listener);
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.language(new String[] { args[0] });
    filterQuery.track(new String[] { args[1] });
    twitterStream.filter(filterQuery);
}