java - 使用 YouTube Data API v3 确定 YouTube 频道的上传速率
问题描述
我正在编写一个使用 YouTube Data API v3 的 Java 应用程序。我希望能够确定频道的上传速率。例如,如果一个频道成立一周,并且发布了 2 个视频,我想通过某种方式确定该频道的上传速率是 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 开发者频道,但我希望能够对任何频道执行此操作。
解决方案
根据官方文档,一旦你调用Channels.list
API 端点——返回指定通道的元数据,a—— Channels resource
,你就可以使用以下属性:
statistics.videoCount (unsigned long)
上传到频道的公共视频数量。
因此,事情几乎是显而易见的:使该属性返回的值持久化(例如,将其保存到文件中)并安排您的程序,以便每周发布一次,以计算您所需的上传速率。
现在,对于上面的代码,您应该首先摆脱:
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 只会将您需要的属性发回给您。
附录
我还必须提到,仅当您将一个通道 ID传递给 API 端点(正如您当前在上面的代码中所做的那样)时,上述断言才是正确的。如果将来您想一次性N
计算频道的上传速率(使用N <= 50
),那么上面的条件将类似于size() <= N
。
可以一次性调用Channels.list
多个通道,因为id
允许将此端点的属性指定为以逗号分隔的通道 ID 列表。
推荐阅读
- javascript - 映射 json 并在 React 中添加图像
- java - Spring 存储库将 HQL 发送到数据库服务器
- java - 如何转换 ArrayList
到包含十进制值的双[]? - javascript - 查看每个调用旁边的 javascript 代码的执行时间
- azure - Azure CDN 终结点中的裸域具有错误的 HTTPS 证书名称
- php - 根据另一个输入值(0-100 之间的数字)将 DIV ID(字母)传递/回显到输入值
- python - 在没有scipy的python中将COO转换为CSR格式
- php - HTML 显示为文本
- python - 如何从列表中获取方括号中的唯一值
- html - 可以
在没有父级的情况下使用