【发布时间】:2022-01-14 17:51:51
【问题描述】:
我正在尝试使用 youtube 的 api 通过使用 node.js 的频道名称来获取频道的订阅者数量。我该怎么做?
【问题讨论】:
-
检查channels.list method检查零件统计检查node.js quickstart
标签: node.js youtube youtube-api
我正在尝试使用 youtube 的 api 通过使用 node.js 的频道名称来获取频道的订阅者数量。我该怎么做?
【问题讨论】:
标签: node.js youtube youtube-api
您可以使用official YouTube API channels.list 方法获取如下统计信息:subscriberCount、videoCount 和 viewCount。
| Get statistics from a YouTube channel | Run in Fusebit |
|---|
const youtube = googleClient.youtube('v3');
const channelName = 'GoogleDevelopers';
const channelsResponse = await youtube.channels.list({
part: 'id,statistics',
forUsername: channelName,
});
if (channelsResponse.data.items && channelsResponse.data.items.length) {
const { statistics: { subscriberCount, videoCount, viewCount } } = channelsResponse.data.items[0];
console.log(`The channel ${channelName} has ????? ${subscriberCount} subscribers, ? ${videoCount} videos and ? ${viewCount} views.`);
} else {
console.log = `Channel not found: ${channelName}`;
}
[更新]
基于 cmets,这里是一个如何根据搜索词搜索频道的示例,然后您将使用频道 id 从前面的代码中获取统计信息,而不是发送 forUserName 参数您将发送 id See search API docs here
const channelsResponse = await youtube.search.list({
part: 'snippet',
q: searchTerm,
maxResults: 10,
type: 'channel',
order: 'viewCount' // show more popular first
});
这是搜索中每个项目返回的response object
{
"kind": "youtube#searchResult",
"etag": etag,
"id": {
"kind": string,
"videoId": string,
"channelId": string,
"playlistId": string
},
"snippet": {
"publishedAt": datetime,
"channelId": string,
"title": string,
"description": string,
"thumbnails": {
(key): {
"url": string,
"width": unsigned integer,
"height": unsigned integer
}
},
"channelTitle": string,
"liveBroadcastContent": string
}
}
【讨论】: