我可以使用 Google 应用程序脚本获取 YouTube 视频标签吗?
Can I get YouTube video tag using Google Application Script?
我想在新的 YouTube 视频发布后发送电子邮件。我需要以某种方式 select 什么视频会触发电子邮件。我以为我会使用标签。但是如何访问标签?
我正在使用这个 sample script to list all my videos. It works well but I do not know how to access the tags. From the searches I have done here on SO and Google it seems to me that is not possible using Google Application Script but the documentation 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;
}
}
}
修改点:
- 当我看到“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);
}
}
参考文献:
我想在新的 YouTube 视频发布后发送电子邮件。我需要以某种方式 select 什么视频会触发电子邮件。我以为我会使用标签。但是如何访问标签?
我正在使用这个 sample script to list all my videos. It works well but I do not know how to access the tags. From the searches I have done here on SO and Google it seems to me that is not possible using Google Application Script but the documentation 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;
}
}
}
修改点:
- 当我看到“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库进行批量请求。 Reffunction 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); } }