gpt4 book ai didi

java - 如何管理来自不同线程的不同 TwitterStream

转载 作者:行者123 更新时间:2023-11-30 11:35:39 30 4
gpt4 key购买 nike

我正在尝试通过单一身份验证使用不同的 TwitterStream 对象执行不同的线程:

public class Support {
private static final String accessToken = "xxxxxxx";
private static final String accessTokenSecret = "xxxxxxx";
public static final AccessToken token = new AccessToken(accessToken, accessTokenSecret);
private static final String consumerKey = "xxxxxxx";
private static final String consumerSecret = "xxxxxxx";
private static final Configuration conf = new ConfigurationBuilder().setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).build();
public static TwitterStreamFactory factory = new TwitterStreamFactory(conf);
}

在我做的每个线程中:

public MyThread1(){
this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
StatusListener listener = ... ;
twitterStream.addListener(listener);
FilterQuery fq = new FilterQuery();
fq.track(new String[]{"hashtag1","hashtag2"});
twitterStream.filter(fq);
}

public MyThread2(){
this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
StatusListener listener = ... ;
twitterStream.addListener(listener);
FilterQuery fq = new FilterQuery();
fq.track(new String[]{"hashtag3","hashtag4"});
twitterStream.filter(fq);
}

但它给我身份验证错误.. 同一身份验证的多个请求。我该如何解决?

最佳答案

这是我的做法:

public class MyTwitterApp implements {

private Twitter twitter;
private Query query;

public MyTwitterApp (){
twitter = TwitterFactory.getSingleton();
}

public static void main(String[] args) {
MyTwitterApp twitterApp = new MyTwitterApp();
twitterApp.getStreamingTweets();

}

public void getStreamingTweets(){
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
handleStatus(status);
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {ex.printStackTrace(); }
public void onScrubGeo(long arg0, long arg1) {}
public void onStallWarning(StallWarning arg0) {}
};
twitter.addListener(listener);
FilterQuery fq = new FilterQuery();
fq.count(0);
fq.track(new String[]{"#MyHashTag"});
twitter.filter(fq);
}

protected void handleStatus(Status tweet) {
if(tweet.isRetweet()){
return;
}
if(isMyHashTagTweet(tweet)){
//do something with tweet here
}
}

private boolean isMyHashTagTweet(Status tweet) {
HashtagEntity[] htes = tweet.getHashtagEntities();
for(HashtagEntity hte : htes){
if(hte.getText().equalsIgnoreCase("myhashtag")) {
return true;
}
}
return false;
}
}

每个线程都会包含这样的内容。

twitter = TwitterFactory.getSingleton();

将确保您每次都重复使用相同的连接。

twitter.addListener(listener);

将添加一个监听器,以便您将被回调到该线程(但您将从添加的每个查询中回调)

twitter.filter(fq);

将添加一个新的搜索查询。

isMyHashTagTweet(tweet)

将检查以确保您的 twitterStream 中所有查询返回的推文与您当前的线程相关

关于java - 如何管理来自不同线程的不同 TwitterStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986357/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com