google-apps-script - 我可以使用 Google 应用程序脚本获取 YouTube 视频标签吗?
问题描述
我想在新的 YouTube 视频发布后发送电子邮件。我需要以某种方式选择将触发电子邮件的视频。我以为我会使用标签。但是我怎样才能访问标签?
我正在使用这个示例脚本来列出我的所有视频。它运作良好,但我不知道如何访问标签。从我在 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...
所以看起来我可以得到视频的标签。有人可以帮我怎么做吗?
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;
}
}
}
解决方案
修改点:
- 当我看到“PlaylistItems”的官方文档时,我找不到“标签”。参考所以在这种情况下,作为一种解决方法,我想建议使用“视频:列表”的方法来检索标签。
当以上几点反映到您的脚本时,它变成如下。
修改后的脚本:
从: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 库以进行批量请求。参考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); } }
参考:
推荐阅读
- python - 如何在熊猫数据框中找到连续值的最后一个值?
- git - 如何将本地更改从 master 提交到另一个分支
- c++ - 带有刻度的 QSlider 未正确绘制
- java - WUT-121 此文件传输已被管理员禁止
- php - HTML 到 PDF 第一页空白
- ios - 在谷歌上搜索 iOS 应用程序时,同一应用程序显示超过 1 个 URL?这是什么意思?
- python - PyCharm 在“通过”时没有达到快速和脏断点
- c# - 如何在带有 C# 的角度网站上使用 selenium 验证文本是否为粗体
- node.js - 添加两个数字将它们连接起来,而不是在亚马逊 alexa 技能中计算和 node.js
- mysql - 如何使用 gpkafka 更新和删除 greenplum 数据库中的数据?