在 Google 应用程序脚本中与 YouTube.Search.list 一起使用时 "statistics" 属性 出现问题
trouble with "statistics" property when using with YouTube.Search.list in Google App Script
我似乎无法让此脚本为 'statistics' 工作:
function searchByKeyword2() {
var results = YouTube.Search.list('statistics', {q: 'dogs', maxResults: 5, });
for(var i in results.items) {
var item = results.items[i];
Logger.log(item);
}
}
我可以使用 'id'、'snippet' 或 'id, snippet',但我无法使用 'statistics'。几个小时以来,我一直在寻找答案,但我什么也没找到。有什么线索吗?
根据 API 文档,YouTube.Search
includes results for Video
s, Channel
s, and Playlist
s。并非所有这些资源都有统计节点,因此 YouTube.Search
端点不允许查询 statistics
节点 - 只有 id
和 snippet
.
对于跟踪 statistics
的集合,例如 Video
s,您可以直接查询它们以访问 statistics
。由于Videos.list
不搜索,您需要先搜索,然后提供相关的视频ID。请注意,您可以更改搜索顺序(以及许多其他搜索属性)- API 参考中提供了完整的详细信息 - 但默认排序是 'relevance'.
举个例子:
function getVideoStatistics(videoIds) {
const options = {
id: videoIds.join(","),
fields: "nextPageToken,items(id,statistics)"
};
const results = [];
do {
var search = YouTube.Videos.list('statistics', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken);
return results;
}
function getVideosFromQuery(query, maxResults) {
const options = {
q: query,
maxResults: maxResults,
type: 'video',
fields: "nextPageToken,pageInfo/totalResults,items(id/videoId,snippet(title,channelTitle))"
};
const results = [];
do {
var search = YouTube.Search.list('snippet', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken && results.length < search.pageInfo.totalResults && results.length < maxResults);
return results;
}
function foo() {
var someQuery = "something";
var searchResults = getVideosFromQuery(someQuery, 50);
var ids = searchResults.map(function (videoSearchResult) {
return videoSearchResult.id.videoId;
});
var stats = getVideoStatistics(ids);
console.log({message:"query video statistics", searchResults: searchResults, statistics: stats});
}
我似乎无法让此脚本为 'statistics' 工作:
function searchByKeyword2() {
var results = YouTube.Search.list('statistics', {q: 'dogs', maxResults: 5, });
for(var i in results.items) {
var item = results.items[i];
Logger.log(item);
}
}
我可以使用 'id'、'snippet' 或 'id, snippet',但我无法使用 'statistics'。几个小时以来,我一直在寻找答案,但我什么也没找到。有什么线索吗?
根据 API 文档,YouTube.Search
includes results for Video
s, Channel
s, and Playlist
s。并非所有这些资源都有统计节点,因此 YouTube.Search
端点不允许查询 statistics
节点 - 只有 id
和 snippet
.
对于跟踪 statistics
的集合,例如 Video
s,您可以直接查询它们以访问 statistics
。由于Videos.list
不搜索,您需要先搜索,然后提供相关的视频ID。请注意,您可以更改搜索顺序(以及许多其他搜索属性)- API 参考中提供了完整的详细信息 - 但默认排序是 'relevance'.
举个例子:
function getVideoStatistics(videoIds) {
const options = {
id: videoIds.join(","),
fields: "nextPageToken,items(id,statistics)"
};
const results = [];
do {
var search = YouTube.Videos.list('statistics', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken);
return results;
}
function getVideosFromQuery(query, maxResults) {
const options = {
q: query,
maxResults: maxResults,
type: 'video',
fields: "nextPageToken,pageInfo/totalResults,items(id/videoId,snippet(title,channelTitle))"
};
const results = [];
do {
var search = YouTube.Search.list('snippet', options);
if (search.items && search.items.length)
Array.prototype.push.apply(results, search.items);
options.pageToken = search.nextPageToken;
} while (options.pageToken && results.length < search.pageInfo.totalResults && results.length < maxResults);
return results;
}
function foo() {
var someQuery = "something";
var searchResults = getVideosFromQuery(someQuery, 50);
var ids = searchResults.map(function (videoSearchResult) {
return videoSearchResult.id.videoId;
});
var stats = getVideoStatistics(ids);
console.log({message:"query video statistics", searchResults: searchResults, statistics: stats});
}