gpt4 book ai didi

java - twitter4j 搜索完整 7 天范围

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:02 25 4
gpt4 key购买 nike

我尝试用关键字保存推文,我知道免费 API 只提供 7 天的结果,但它永远不会获得任何大于几个小时的时间线集合,有时它甚至给我一个小时的范围。我确实将since()和until()设置为搜索查询。我单次运行得到的推文最大数量不到 400 条。谁能告诉我为什么它会自动停止,结果却这么少?谢谢。

public static void main(String[] args) throws TwitterException {

String KEY_word;
String Exclude;
String Since;
String Until;
String OPT_dir;
String time;
int x;
Propertyloader confg = new Propertyloader();
KEY_word = confg.getProperty("KEY_word");
Exclude = confg.getProperty("Exclude");
Since = confg.getProperty("Since");
Until = confg.getProperty("Until");
OPT_dir = confg.getProperty("OPT_dir");


Twitter twitter = new TwitterFactory().getInstance();
try {
time = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
x = 1;
Query query = new Query(KEY_word + Exclude);
query.since(Since);
query.until(Until);
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();

for (Status tweet : tweets) {


try {
String filedir = OPT_dir + KEY_word + time;
writeStringToFile(filedir, x + ". " + "@" + tweet.getUser().getScreenName() + ", At: " + tweet.getCreatedAt() + ", Rt= " + tweet.getRetweetCount() + ", Text: " + tweet.getText());
x += 1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while ((query = result.nextQuery()) != null);
System.exit(0);



} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}

}


public static void writeStringToFile(String filePathAndName, String stringToBeWritten) throws IOException{
try
{
String filename= filePathAndName;
boolean append = true;
FileWriter fw = new FileWriter(filename,append);
fw.write(stringToBeWritten);//appends the string to the file
fw.write("\n" +"\n");
fw.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}

最佳答案

您可以使用 setMaxId 获取更多推文。这是一个例子:

            long lowestTweetId = Long.MAX_VALUE;
x = 1;
Query query = new Query("stackoverflow");
query.since("2018-08-10");
query.until("2018-08-16");
query.setCount(100); //The number of tweets to return per page, up to a maximum of 100. Defaults to 15. https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
query.setResultType(Query.RECENT); // to get an order

int searchResultCount=100;

QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();

for (Status tweet : tweets) {


try {

System.out.println( "@" + tweet.getUser().getScreenName() + ", At: " + tweet.getCreatedAt() );
x += 1;
if (tweet.getId() < lowestTweetId) {

lowestTweetId = tweet.getId();
query.setMaxId(lowestTweetId-1);

}
else {// each new maxid should be smaller than the other one so break here
//do whatever you want to handle it ex: break from two loops

}



} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while (searchResultCount != 0 );

关于java - twitter4j 搜索完整 7 天范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461872/

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