gpt4 book ai didi

java - 使用 YouTube Data API v3 确定 YouTube channel 的上传速率

转载 作者:行者123 更新时间:2023-12-03 05:28:28 24 4
gpt4 key购买 nike

我正在编写一个使用 YouTube Data API v3 的 Java 应用程序。我希望能够确定 channel 的上传速率。例如,如果一个 channel 成立一周,并且发布了 2 个视频,我想通过某种方式确定该 channel 的上传速率是 2 个视频/周。我将如何使用 YouTube API 做到这一点?

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Channel;
import com.google.api.services.youtube.model.ChannelListResponse;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

public class ApiExample {
public static void main(String[] args)
throws GeneralSecurityException, IOException, GoogleJsonResponseException {
Properties properties = new Properties();
try {
InputStream in = ApiExample.class.getResourceAsStream("/" + "youtube.properties");
properties.load(in);

} catch (IOException e) {
System.err.println("There was an error reading " + "youtube.properties" + ": " + e.getCause()
+ " : " + e.getMessage());
System.exit(1);
}
YouTube youtubeService = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("API Demo").build();
// Define and execute the API request
YouTube.Channels.List request = youtubeService.channels()
.list("snippet,contentDetails,statistics");
String apiKey = properties.getProperty("youtube.apikey");
request.setKey(apiKey);
ChannelListResponse response = request.setId("UC_x5XG1OV2P6uZZ5FSM9Ttw").execute();
for (Channel channel : response.getItems()) {
/* What do I do here to get the individual channel's upload rate? /
}
}
}
上面的示例使用 YouTube 开发者 channel ,但我希望能够对任何 channel 执行此操作。

最佳答案

根据官方文档,一旦你调用 Channels.list API 端点——返回指定 channel 的元数据,一个 Channels resource ——你就可以使用以下属性:

statistics.videoCount (unsigned long)
The number of public videos uploaded to the channel.


因此,事情几乎是显而易见的:使该属性返回的值持久化(例如,将其保存到文件中)并安排您的程序,以便每周发布一次,以计算您所需的上传速率。

现在,对于上面的代码,您应该首先摆脱:
for (Channel channel : response.getItems()) {
/* What do I do here to get the individual channel's upload rate? /
}
因为 items 属性将最多包含一项。一个好的做法是断言这个条件:
assert response.getItems().size() <= 1;
所需的 videoCount 属性的值可以在 getVideoCount 类的 ChannelStatistics 方法下访问: response.getItems().get(0).getStatistics().getVideoCount()
当然,由于只向 API 询问真正有用的信息总是好的,我还建议您以以下形式使用参数 fields (方法 setFields ): request.setFields("items(statistics(videoCount))") ,
例如,在 request.setKey(apiKey) 之后插入。
这样,API 只会将您需要的属性发回给您。

附录
我还必须提到,仅当您将一个 channel ID 传递给 API 端点(正如您当前在上面的代码中所做的那样)时,上述断言才是正确的。如果将来您想一次性计算 N channel 的上传速率(使用 N <= 50 ),那么上面的条件将类似于 size() <= N
可以在多个 channel 上一次性调用 Channels.list,因为允许将此端点的 id 属性指定为以逗号分隔的 channel ID 列表。

关于java - 使用 YouTube Data API v3 确定 YouTube channel 的上传速率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64123167/

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