[英]Search new twitter with a specific hashtag form a stream with twitter4j
I need my java app to be updated on all the new tweet with a specific hashtag (let's say #test
) with twitter4j. 我需要使用twitter4j在具有特定主题标签(比如#test
)的所有新tweet上更新我的Java应用程序。 Due to avoiding continuous polling on twitter REST API I'll gonna use Twitter Stream API. 由于避免在twitter REST API上进行连续轮询,因此我将使用Twitter Stream API。
Running this code I'm expecting to: 运行此代码,我期望:
#test
打开浏览器,转到Twitter并发布包含#test
的推文 But.. I do not see anything in my app.. (SEE THE EDIT) 但是..我在我的应用程序中什么都看不到..(请参阅编辑)
Someone can give me some advice? 有人可以给我一些建议吗?
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET)
.setOAuthAccessToken(ACCESS_TOKEN)
.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getUser().getId() + " - " + status.getText());
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Post Deletado:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.err.println("Limitação:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
};
List<String> queries = new ArrayList<String>();
queries.add("#test");
twitterStream.addListener(listener);
twitterStream.firehose(20);
String[] trackQueries = (String[]) queries.toArray(new String[queries.size()]);
FilterQuery filterQuery = new FilterQuery();
twitterStream.filter(filterQuery.track(trackQueries));
After some errors fixed I'm reading something in the sys.out but.. it seems random stuffs after some errors: 在修复了一些错误之后,我正在sys.out中读取内容,但是..在出现一些错误之后,似乎是随机的东西:
[Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:31 CEST 2013]Connection established.
[Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
[Tue Jul 02 14:58:31 CEST 2013]Connection established.
[Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
[Tue Jul 02 14:58:31 CEST 2013]Stream closed.
[Tue Jul 02 14:58:31 CEST 2013]Stream closed.
[Tue Jul 02 14:58:31 CEST 2013]Waiting for 250 milliseconds
Stream closed.
Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=a8fd061d or
http://www.google.co.jp/search?q=00070a0c
TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
Caused by: java.io.IOException: the end of the stream has been reached
at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
... 2 more
Stream closed.
Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=a8fd061d or
http://www.google.co.jp/search?q=00070a0c
TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
Caused by: java.io.IOException: the end of the stream has been reached
at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
... 2 more
[Tue Jul 02 14:58:31 CEST 2013]Unhandled event: {"disconnect":{"code":7,"stream_name":"urbanspirit5-statuses1734930","reason":"admin logout"}}
[Tue Jul 02 14:58:32 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:33 CEST 2013]Connection established.
[Tue Jul 02 14:58:33 CEST 2013]Receiving status stream.
And then some random tweets like this: 然后像这样的一些随机推文:
@markinhos_jm - 300044892 - CRAZY PARTY AGUARDEM!!!
@bindibba7 - 511548205 - ??????? .. ??????? = ?? ??? ????
@Tamirmdty - 1521605059 - Salah ngetik
@PAOLOves_you - 222755754 - Hirap ah. Text, Chat FB at Tweet? SIYET! #MedyoAZARitu
@agokichi_mmmm - 793252368 - ???DVD??????
Access to twitter firehose is only given to selected companies via resellers. 仅通过转销商才能选择Twitter公司使用twitter firehose。 So unless you are one of those companies, you want to use sample()
instead of firehose
. 因此,除非您是这些公司之一,否则您要使用sample()
而不是firehose
。 You should see some logs though. 您应该会看到一些日志。 Are you sure your loggers are set up correctly? 您确定记录器设置正确吗?
EDIT: Read this section of the documentation and add a valid twitter4j.properties file. 编辑:阅读本节内容 ,并添加有效的twitter4j.properties文件。 Note that you have to sign up with twitter to get an access token. 请注意,您必须注册Twitter才能获得访问令牌。 Once you have a token and filled the config file, you can just include it in your classpath. 获得令牌并填充配置文件后,就可以将其包括在类路径中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.