[英]Get Tweets from Twitter hbc API
public class TwitterStreamImpl implements TwitterStream {
public void setUpStream() throws InterruptedException {
final String consumerKey = getTwitterCredentials().get(0).toString();
final String consumerSecret = getTwitterCredentials().get(1).toString();
final String token = getTwitterCredentials().get(2).toString();
final String secret = getTwitterCredentials().get(3).toString();
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// add some track terms
endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo", "trump", "donald", "lol"));
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Authentication auth = new BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
// Do whatever needs to be done with messages
for (int msgRead = 0; msgRead < 1000; msgRead++) {
if (client.isDone()) {
System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
break;
}
String msg = queue.poll(5, TimeUnit.SECONDS);
if (msg == null) {
System.out.println("Did not receive a message in 5 seconds");
} else {
System.out.println(msg);
}
}
client.stop();
}
/**
* Reads twitterStup.txt from C:/Users/"user"/documents/ and returns them as
* an array
*
* @return Twitter Api Credentials
*/
private ArrayList getTwitterCredentials() {
BufferedReader in;
String str;
ArrayList<String> list = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("*******"));
while ((str = in.readLine()) != null) {
list.add(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
The console log says: 控制台日志显示:
Did not receive a message in 5 seconds
And it says that every five seconds. 它说,每五秒钟一次。 I want to "sysout" (live) every tweet, that has one of the endpoint trackTerms in it.
我想“ sysout”(实时)每条推文,其中包含端点trackTerms之一。 But there is no error or something similar.
但是没有错误或类似的东西。 Is there a problem with the proxy perhaps?
代理可能有问题吗?
The code is working, as it is at the time. 该代码正在运行,就像当时一样。 The problem was the proxy.
问题是代理。 Because I've been in the office-network, the connection to the stream wasn't possible.
因为我去过办公室网络,所以无法连接到流。 So i tried it with my own notebook, guess what, it worked.
因此,我用自己的笔记本电脑尝试了一下,猜猜它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.