gpt4 book ai didi

java - 如何使用 Twitter4j 检索超过 100 个结果

转载 作者:搜寻专家 更新时间:2023-10-30 21:40:30 24 4
gpt4 key购买 nike

我正在使用 Twitter4j 库检索推文,但我的目的还远远不够。目前,我从一页中获得最多 100 个。我如何在 Processing 中的以下代码中实现 maxId 和 sinceId 以便从 Twitter 搜索 API 检索超过 100 个结果?我对 Processing(和一般编程)完全陌生,所以任何关于这方面的指导都会很棒!谢谢!

void setup() {

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#peace");
query.setCount(100);

try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();

for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);

GeoLocation loc = t.getGeoLocation();

if (loc!=null) {
tweets.get(i++);

String user = t.getUser().getScreenName();
String msg = t.getText();

Double lat = t.getGeoLocation().getLatitude();
Double lon = t.getGeoLocation().getLongitude();

println("USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon);

}
}
}

catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}

void draw() {
}

最佳答案

不幸的是你不能,至少不能以直接的方式,比如做

query.setCount(101);

作为javadoc说它最多只允许 100 条推文。

为了克服这个问题,您只需要分批请求它们,并且在每批中将您获得的最大 ID 设置为比您从上一个获得的最后一个 ID 小 1。总结一下,您将流程中的每条推文收集到一个 ArrayList 中(顺便说一句,它不应该保持通用,但其类型定义为 ArrayList<Status> - 一个包含 Status 对象的 ArrayList),然后打印所有内容!这是一个实现:

void setup() {

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#peace");
int numberOfTweets = 512;
long lastID = Long.MAX_VALUE;
ArrayList<Status> tweets = new ArrayList<Status>();
while (tweets.size () < numberOfTweets) {
if (numberOfTweets - tweets.size() > 100)
query.setCount(100);
else
query.setCount(numberOfTweets - tweets.size());
try {
QueryResult result = twitter.search(query);
tweets.addAll(result.getTweets());
println("Gathered " + tweets.size() + " tweets");
for (Status t: tweets)
if(t.getId() < lastID) lastID = t.getId();

}

catch (TwitterException te) {
println("Couldn't connect: " + te);
};
query.setMaxId(lastID-1);
}

for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);

GeoLocation loc = t.getGeoLocation();

String user = t.getUser().getScreenName();
String msg = t.getText();
String time = "";
if (loc!=null) {
Double lat = t.getGeoLocation().getLatitude();
Double lon = t.getGeoLocation().getLongitude();
println(i + " USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon);
}
else
println(i + " USER: " + user + " wrote: " + msg);
}
}

注:行

ArrayList<Status> tweets = new ArrayList<Status>();

应该是:

List<Status> tweets = new ArrayList<Status>();

因为你should always use the interface in case you want to add a different implementation .当然,如果您使用的是 Processing 2.x,那么一开始就需要这样做:

import java.util.List;

关于java - 如何使用 Twitter4j 检索超过 100 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18800610/

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