Created
March 18, 2013 14:22
-
-
Save moaikids/5187478 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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