【问题标题】:Can I get YouTube video tag using Google Application Script?我可以使用 Google 应用程序脚本获取 YouTube 视频标签吗?
【发布时间】:2021-05-14 18:14:42
【问题描述】:

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

我正在使用这个sample script 来列出我的所有视频。它运作良好,但我不知道如何访问标签。从我在 SO 和 Google 上所做的搜索来看,在我看来,使用 Google Application Script 是不可能的,但文档 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 如图所示

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;
    }
  }
}

【问题讨论】:

  • 我无法理解您当前的脚本和您的目标之间的关系。我为此道歉。可以问一下具体情况吗?
  • 脚本列出​​了我所有的视频,特别是视频 ID 和标题。我也想得到那个标签。
  • 我更新了我的问题并添加了我想要的图片。
  • 感谢您的回复。根据您的回复,我提出了一个修改后的脚本作为答案。你能确认一下吗?如果这不是您期望的方向,我深表歉意。

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


【解决方案1】:

修改点:

  • 当我看到“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);
        }
      }
    

参考资料:

【讨论】:

  • 完美运行,谢谢。我不明白为什么这个网址有“标签”developers.google.com/youtube/v3/docs/playlists/list 但这个没有developers.google.cn/youtube/v3/docs/playlistItems/list
  • 你知道YouTube.PlaylistItems.list结果是否按publishAt排序吗?以及如何发现?
  • @Radek 感谢您的回复。我很高兴你的问题得到了解决。关于您的附加问题,不幸的是,我找不到用于对结果值进行排序的 API 参数。我为此道歉。这样,在当前阶段,当您要对结果值进行排序时,需要在检索到值后使用脚本对值进行排序。对于这种情况,我深表歉意。
  • @Radek:确实,PlaylistItems.list 端点返回一个按videoPublishedAt 排序的结果集,当将其playlistId 参数设置为频道上传播放列表的ID 进行查询时我>。端点的这个属性没有记录,但是看看my argument in favor of its existence
  • @stvar 感谢您提供更多信息。而且,我为我的拙劣技能深表歉意。
猜你喜欢
  • 2013-04-23
  • 2018-10-29
  • 2014-04-06
  • 2021-06-30
  • 2017-03-03
  • 1970-01-01
  • 2022-10-17
  • 2015-05-08
  • 2013-03-25
相关资源
最近更新 更多