我可以使用 Google 应用程序脚本获取 YouTube 视频标签吗?

时间:2021-02-11 01:03:47

标签: google-apps-script youtube-data-api

我想在新的 YouTube 视频发布后发送电子邮件。我需要以某种方式选择将触发电子邮件的视频。我以为我会使用标签。但我如何访问标签?

我正在使用此 sample script 列出我的所有视频。它运行良好,但我不知道如何访问标签。从我在 SO 和 Google 上进行的搜索来看,在我看来,使用 Google 应用程序脚本是不可能的,但文档 PlaylistItems... For example, in a playlist resource, the snippet property contains properties like author, title, description, **TAGS**, and timeCreated. As such, if you set part=snippet, the API response will contain all of those properties...

所以看起来我可以获取视频的标签。有人可以帮我吗?

我想要的是如图my_tag标签 enter image description here

function retrieveMyUploads() {
  var results = YouTube.Channels.list('contentDetails', {mine: true});
  for(var i in results.items) {
    var item = results.items[i];
    // Get the playlist ID, which is nested in contentDetails, as described in the
    // Channel resource: https://developers.google.com/youtube/v3/docs/channels
    var playlistId = item.contentDetails.relatedPlaylists.uploads;

    var nextPageToken = '';

    // This loop retrieves a set of playlist items and checks the nextPageToken in the
    // response to determine whether the list contains additional items. It repeats that process
    // until it has retrieved all of the items in the list.
    while (nextPageToken != null) {
      var playlistResponse = YouTube.PlaylistItems.list('snippet', {
        playlistId: playlistId,
        maxResults: 25,
        pageToken: nextPageToken
      });

      for (var j = 0; j < playlistResponse.items.length; j++) {
        var playlistItem = playlistResponse.items[j];
        Logger.log('[%s] Title: %s',
                   playlistItem.snippet.resourceId.videoId,
                   playlistItem.snippet.title);

      }
      nextPageToken = playlistResponse.nextPageToken;
    }
  }
}

1 个答案:

答案 0 :(得分:2)

修改点:

  • 当我看到“PlaylistItems”的官方文档时,我找不到“标签”。 Ref 所以在这种情况下,作为一种解决方法,我想建议使用“视频:列表”的方法来检索标签。

当以上几点反映到你的脚本中时,它变成如下。

修改后的脚本:

从:
for (var j = 0; j < playlistResponse.items.length; j++) {
  var playlistItem = playlistResponse.items[j];
  Logger.log('[%s] Title: %s',
             playlistItem.snippet.resourceId.videoId,
             playlistItem.snippet.title);

}
到:
for (var j = 0; j < playlistResponse.items.length; j++) {
  var playlistItem = playlistResponse.items[j];
  Logger.log('[%s] Title: %s',
             playlistItem.snippet.resourceId.videoId,
             playlistItem.snippet.title);
  
  // I added below script.
  var res = YouTube.Videos.list('snippet', {id: playlistItem.snippet.resourceId.videoId});
  var tagList = res.items.map(e => ({id: e.id, tags: e.snippet.tags}));
  Logger.log(tagList)
}

注意:

  • 这是一个简单的修改。所以请根据您的实际情况进行修改。

  • 如果不想在循环中使用YouTube.Videos.list,我认为您也可以使用以下脚本使用批处理请求。使用此脚本时,请安装 GAS 库进行批量请求。 Ref

      function myFcuntion() {
        var results = YouTube.Channels.list('contentDetails', {mine: true});
        for(var i in results.items) {
          var item = results.items[i];
          // Get the playlist ID, which is nested in contentDetails, as described in the
          // Channel resource: https://developers.google.com/youtube/v3/docs/channels
          var playlistId = item.contentDetails.relatedPlaylists.uploads;
    
          var nextPageToken = '';
    
          // This loop retrieves a set of playlist items and checks the nextPageToken in the
          // response to determine whether the list contains additional items. It repeats that process
          // until it has retrieved all of the items in the list.
    
          var videoIds = [];  // Added
          while (nextPageToken != null) {
            var playlistResponse = YouTube.PlaylistItems.list('snippet', {
              playlistId: playlistId,
              maxResults: 25,
              pageToken: nextPageToken,
              fields: "items"
            });
    
            for (var j = 0; j < playlistResponse.items.length; j++) {
              var playlistItem = playlistResponse.items[j];
              Logger.log('[%s] Title: %s',
                        playlistItem.snippet.resourceId.videoId,
                        playlistItem.snippet.title);
    
              videoIds.push(playlistItem.snippet.resourceId.videoId);  // Added
            }
            nextPageToken = playlistResponse.nextPageToken;
          }
    
          // I added below script.
          var requests = {
            batchPath: "batch/youtube/v3",
            requests: videoIds.map(id => ({
              method: "GET",
              endpoint: `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${id}`,
            })),
            accessToken: ScriptApp.getOAuthToken(),
          };
          var result = BatchRequest.EDo(requests); // Using GAS library
          var tagList = result.flatMap(({items}, i) => items.map(({snippet}) => ({id: videoIds[i], tags: snippet.tags})));
          Logger.log(tagList);
        }
      }
    

参考: