twitter4j - 从 Streaming API 访问推文信息 [英] twitter4j - access tweet information from Streaming API
问题描述
我的目标是收集所有包含法国"和德国"字样的推文,并收集相关的元数据(例如,附加到推文的地理坐标).我知道此元数据可用,但我不知道如何使用我正在使用的 Java 库访问它:twitter4j".
My goal is to collect all tweets containing the words "France" and "Germany" and to also collect associated metadata (e.g., the geo coordinates attached to the tweet). I know that this metadata is available, but I can't figure out how to access it with the Java library I'm using : "twitter4j".
好的,到目前为止我所拥有的是来自 twitter4j 站点上的代码示例.它打印出包含我选择的关键字的所有推文,因为它们是由 Twitter 的 Streaming API 实时提供的.我在 TwitterStream 对象上调用 filter 方法,这提供了流.但我需要更多的控制.也就是说,我希望能够:
Ok, so what I have so far is taken from code samples on the twitter4j site. It prints out all tweets containing my chosen keywords, as they are provided in real-time by Twitter's Streaming API. I call the filter method on my TwitterStream object, and this provides the stream. But I need more control. Namely, I would like to be able to:
1) 将推文写入文件;2)只打印出前1000条推文;3) 访问附加到推文的其他元数据(过滤器方法只是打印出用户名和推文本身).
1) write the tweets to a file; 2) only print out the first 1000 tweets; 3) access other metadata attached to the tweet (the filter method just prints out the username and the tweet itself).
这是我到目前为止的代码:
Here is the code I have so far:
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
public class Stream {
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("bbb");
cb.setOAuthConsumerSecret("bbb");
cb.setOAuthAccessToken("bbb");
cb.setOAuthAccessTokenSecret("bbb");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
};
FilterQuery fq = new FilterQuery();
String keywords[] = {"France", "Germany"};
fq.track(keywords);
twitterStream.addListener(listener);
twitterStream.filter(fq);
}
}
推荐答案
在以全新的眼光看待这个之后,我意识到了解决方案(这很明显).编辑以下代码部分:
After looking at this with fresh eyes I realised the solution (which was pretty obvious). Editing the following part of the code:
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
允许我访问其他元数据.例如,如果我想访问推文的日期,我只需要添加以下内容:
allows me to access other metadata. For example, if I want to access the tweet's date, I simply need to add the following:
System.out.println(status.getCreatedAt());
这篇关于twitter4j - 从 Streaming API 访问推文信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!