NodeJS 如何通过频道名称从YouTube API获取订阅者计数

kjthegm6  于 2023-10-17  发布在  Node.js
关注(0)|答案(2)|浏览(139)

我试图使用youtube的API来获取一个频道的订阅者计数,通过它的频道名称使用node.js。我该怎么做?

ymzxtsji

ymzxtsji1#

您可以使用official YouTube APIchannels.list方法获取统计信息,如:subscriberCount、videoCount和viewCount。
| 从YouTube频道获取统计数据 |在Fusebit x1c 0d1x中运行|
| --|--|

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

[更新]
根据评论,这里是一个如何根据搜索词搜索频道的例子,然后你将使用频道id来获取统计信息,从前面的代码中,而不是发送forerName参数,你将发送idSee search API docs here

const channelsResponse = await youtube.search.list({
    part: 'snippet',
    q: searchTerm,
    maxResults: 10,
    type: 'channel',
    order: 'viewCount' // show more popular first
  });

这是搜索中每个项目返回的响应对象

{
  "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
  }
}
jutyujz0

jutyujz02#

如果你能得到频道ID,它会更准确:然后,您需要做的就是向Youtube Data V3 API发出请求:
请求:https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UC1gi4ynIYAcd_BmFlJsLOWg&key=your_api_key
回复格式:

{
    "kind": "youtube#channelListResponse",
    "etag": "vdXkdxBKA5DKypndZTEWUM_tBPQ",
    "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 5
    },
    "items": [
        {
            "kind": "youtube#channel",
            "etag": "R5sb6hhxom_vkPLE9izol0WBeVg",
            "id": "UC1gi4ynIYAcd_BmFlJsLOWg",
            "statistics": {
                "viewCount": "35960",
                "subscriberCount": "242",
                "hiddenSubscriberCount": false,
                "videoCount": "26"
            }
        }
    ]
}

相关问题