Skip to content

Instantly share code, notes, and snippets.

@xgrommx
Forked from moaikids/gist:5187478
Created July 3, 2014 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgrommx/2caa3698ae84ca1b034b to your computer and use it in GitHub Desktop.
Save xgrommx/2caa3698ae84ca1b034b to your computer and use it in GitHub Desktop.
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;
import com.twitter.hbc.ClientBuilder;
import com.twitter.hbc.core.Client;
import com.twitter.hbc.core.Constants;
import com.twitter.hbc.core.HttpHosts;
import com.twitter.hbc.core.endpoint.StatusesSampleEndpoint;
import com.twitter.hbc.core.event.Event;
import com.twitter.hbc.httpclient.auth.OAuth1;
/**
* Hello twitter world!
*/
public class App2 {
String consumerKey = "";
String consumerSecret = "";
String authToken = "";
String authSecret = "";
String langDetectProfiles = "";
public static void main(String[] args) throws LangDetectException {
new App2().doit();
}
public void doit() throws LangDetectException {
BlockingQueue<Map<String, Object>> msgQueue = new LinkedBlockingQueue<Map<String, Object>>(100000);
BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<Event>(1000);
DetectorFactory.loadProfile(langDetectProfiles);
// client
ClientBuilder builder = new ClientBuilder()
.name("hbc test client")
.hosts(new HttpHosts(Constants.STREAM_HOST))
.authentication(
new OAuth1(consumerKey, consumerSecret, authToken, authSecret))
.endpoint(new StatusesSampleEndpoint())
.processor(new MapDelimitedProcessor(msgQueue))
.eventMessageQueue(eventQueue)
.connectionTimeout(15000)
.socketTimeout(15000)
.gzipEnabled(true)
.retries(3);
Client client = builder.build();
client.connect();
while (!client.isDone()) {
try {
Map<String, Object> tweet = msgQueue.take();
String text = (String) tweet.get("text");
if (tweet.containsKey("delete")) {
System.out.println("(this message was deleted)");
} else if (text == null) {
System.out.println("(this message is null)");
} else if (text.trim().isEmpty()) {
System.out.println("(this message is empty)");
} else {
String lang = "";
try {
Detector detector = DetectorFactory.create();
detector.append(text);
lang = detector.detect();
} catch (LangDetectException ex) {
lang = "UNK";
}
if (lang.equals("ja")) {
System.out.println("[" + lang + "]" + text.replaceAll("\n", " "));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment