private static void startStream(List<String> terms) throws TwitterException { if (twitter != null) { twitter.cleanUp(); } if (esClient != null) { esClient.close(); esClient = null; } play.Configuration pconf = Play.application().configuration(); String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name"); if (elasticSearchCluster != null) { Logger.info("Configuring ElasticSearch..."); Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build(); esClient = new TransportClient(settings) .addTransportAddress( new InetSocketTransportAddress( pconf.getString("tweet.elasticsearch.transport.host"), pconf.getInt("tweet.elasticsearch.transport.port"))); } else { esClient = null; } twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration(); TwitterStreamFactory tf = new TwitterStreamFactory(tconf); twitter = tf.getInstance(); StatusListener l = new TweetListener( terms, esClient, pconf.getString("tweet.elasticsearch.index"), pconf.getString("tweet.elasticsearch.type")); twitter.addListener(l); String[] tracks = new String[terms.size()]; StringBuffer termsString = new StringBuffer(); for (int i = 0; i < terms.size(); i++) { tracks[i] = terms.get(i); if (i != 0) termsString.append(","); termsString.append(terms.get(i)); } FilterQuery q = new FilterQuery().track(tracks); twitter.filter(q); Logger.info("Starting listening for tweets using terms " + termsString.toString() + "..."); }
public TweetListener(List<String> terms, Client esClient, String esIndex, String esType) { this.terms = terms; this.esClient = esClient; StringBuilder query = new StringBuilder(); for (int i = 0; i < terms.size(); i++) { if (i > 0) query.append("|"); // query.append("(").append(Pattern.quote(terms.get(i))).append(")"); // query.append("(").append(terms.get(i)).append(")"); query.append(terms.get(i)); } Logger.info("Query match pattern: " + query.toString()); this.matchPattern = Pattern.compile(query.toString(), Pattern.CASE_INSENSITIVE); Logger.info("Query match pattern: " + matchPattern.toString()); }
private static StreamConfig getConfig() { List<StreamConfig> configs = StreamConfig.find.findList(); if (configs.size() > 1) { Logger.error("Multiple stream configurations present!"); } StreamConfig config; if (configs.isEmpty()) { String[] tracks = new String[3]; tracks[0] = "nl-alert"; tracks[1] = "nlalert"; tracks[2] = "\"nl alert\""; config = new StreamConfig(tracks); config.save(); } else { config = configs.get(0); } return config; }