有没有更好的方法来使用 Twitter4J 捕获推文?
Is there a better way to catch tweets using Twitter4J?
我尝试使用 StatusListener 和 TwitterStream 方式来更简洁地执行此操作,但它似乎无法捕获我想要的用户的所有推文。
StatusListener statusListener = new StatusListener() {
@Override
public void onStatus(Status status) {
//my code
}
//other requiredly-overriden methods
}
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey("XXXXX");
configurationBuilder.setOAuthConsumerSecret("XXXXXXX");
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(new AccessToken("XXXXX", "XXXXXXX"));
twitterStream.addListener(statusListener);
twitterStream.sample();
所以我每 3 秒手动检查一次新推文是否已由我想要的用户发布。这工作得很好,但看起来不干净而且很老套。有没有更好的方法?
我不是很明白。您如何尝试从您感兴趣的用户那里获取推文?如果您只是简单地使用推特流,然后检查一条推文是否来自您想要的用户,您可能会从不同的来源获取推文(如果推文很多,可能会丢失一些推文)。
如果您想关注一些用户,您应该使用过滤器,如下所示:
StatusListener statusListener = new StatusListener() {
@Override
public void onStatus(Status status) {
//your code to manage the statuses
}
//other requiredly-overriden methods
}
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey("XXXXX");
configurationBuilder.setOAuthConsumerSecret("XXXXXXX");
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(new AccessToken("XXXXX", "XXXXXXX"));
twitterStream.addListener(statusListener);
//from here is different at your code
//you need to set up your user list, with their users id's!
long[] userslist = YOUR USER LIST;
//then you create a filter
FilterQuery filtre = new FilterQuery();
//and use that filter to follow the users that you want and to start the stream
filtre.follow(userslist);
twitterStream.filter(filtre);
使用过滤器,您最多可以关注 5000 个不同的用户,您将了解他们发布的推文、他们何时被转发以及何时被提及
我尝试使用 StatusListener 和 TwitterStream 方式来更简洁地执行此操作,但它似乎无法捕获我想要的用户的所有推文。
StatusListener statusListener = new StatusListener() {
@Override
public void onStatus(Status status) {
//my code
}
//other requiredly-overriden methods
}
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey("XXXXX");
configurationBuilder.setOAuthConsumerSecret("XXXXXXX");
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(new AccessToken("XXXXX", "XXXXXXX"));
twitterStream.addListener(statusListener);
twitterStream.sample();
所以我每 3 秒手动检查一次新推文是否已由我想要的用户发布。这工作得很好,但看起来不干净而且很老套。有没有更好的方法?
我不是很明白。您如何尝试从您感兴趣的用户那里获取推文?如果您只是简单地使用推特流,然后检查一条推文是否来自您想要的用户,您可能会从不同的来源获取推文(如果推文很多,可能会丢失一些推文)。
如果您想关注一些用户,您应该使用过滤器,如下所示:
StatusListener statusListener = new StatusListener() {
@Override
public void onStatus(Status status) {
//your code to manage the statuses
}
//other requiredly-overriden methods
}
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey("XXXXX");
configurationBuilder.setOAuthConsumerSecret("XXXXXXX");
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(new AccessToken("XXXXX", "XXXXXXX"));
twitterStream.addListener(statusListener);
//from here is different at your code
//you need to set up your user list, with their users id's!
long[] userslist = YOUR USER LIST;
//then you create a filter
FilterQuery filtre = new FilterQuery();
//and use that filter to follow the users that you want and to start the stream
filtre.follow(userslist);
twitterStream.filter(filtre);
使用过滤器,您最多可以关注 5000 个不同的用户,您将了解他们发布的推文、他们何时被转发以及何时被提及