【问题标题】:Determine a YouTube channel's upload rate using YouTube Data API v3使用 YouTube Data API v3 确定 YouTube 频道的上传速率
【发布时间】:2021-01-15 06:36:00
【问题描述】:

我正在编写一个使用 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 开发者频道,但我希望能够对任何频道进行此操作。

【问题讨论】:

    标签: java youtube youtube-api youtube-data-api youtube-channels


    【解决方案1】:

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

    statistics.videoCount(无符号长)
    上传到频道的公开视频数量。

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


    现在,对于您上面的代码,您应该首先摆脱:

    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属性的值可以在ChannelStatistics类的getVideoCount方法下访问:

    response.getItems().get(0).getStatistics().getVideoCount().

    当然,既然只向 API 询问真正有用的信息总是好的,我还建议您以以下形式使用参数fields(方法setFields):

    request.setFields("items(statistics(videoCount))"),

    插入,例如,在request.setKey(apiKey)之后。

    这样,API 只会将您需要的属性发回给您。


    附录

    我还必须提到,只有当您传递到 API 端点时(正如您当前在上面的代码中所做的那样),上述断言才是正确的只有一个通道 ID。如果将来您希望一次性计算N 频道(使用N &lt;= 50)的上传速率,那么上面的条件将类似于size() &lt;= N

    可以在多个通道上一次性调用Channels.list,因为允许将此端点的id 属性指定为以逗号分隔的通道ID 列表。

    【讨论】:

    • 我如何知道频道的年龄以便计算费率?我事先不知道我要检查哪个频道。我需要能够即时计算。
    • @ap 检查Channels.list 的响应 - 我现在无法检查,但是,我记得有一个频道创建日期的值,有些像createdAt - 检查此答案中链接的文档。您也可以尝试获取已上传视频的发布日期,并计算出 YT 用户每周上传的视频数量,然后,平均...
    • @MarcoAurelioFernandezReyes 我认为snippet.publishedAt 可能是我正在寻找的。谢谢。
    • 实际上snippet.publishedAt 属性提供了频道的创建日期和时间。
    猜你喜欢
    • 2015-06-27
    • 2013-12-16
    • 2013-03-24
    • 2016-03-23
    • 2015-08-11
    • 2015-04-30
    • 1970-01-01
    • 2021-01-23
    • 2017-04-15
    相关资源
    最近更新 更多