From aadb6ba0e5a1b2d7d200fc104d36a5018eed921c Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 9 Mar 2020 01:57:24 +0800 Subject: [PATCH] feat: support multiple youtube keys --- docs/en/install/README.md | 2 +- docs/install/README.md | 2 +- lib/routes/youtube/utils.js | 34 ++++++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/docs/en/install/README.md b/docs/en/install/README.md index 8b7a4e3574..077f30635b 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -369,7 +369,7 @@ Access control includes a whitelist and a blacklist, support IP and route, use ` - youtube: [API Key application](https://console.developers.google.com/) - - `YOUTUBE_KEY`: YouTube API Key + - `YOUTUBE_KEY`: YouTube API Key, support multiple keys, split them with `,` - telegram: [Bot application](https://telegram.org/blog/bot-revolution) diff --git a/docs/install/README.md b/docs/install/README.md index 9a523c5efe..561ef2904d 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -373,7 +373,7 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 - youtube 全部路由: [申请地址](https://console.developers.google.com/) - - `YOUTUBE_KEY`: YouTube API Key + - `YOUTUBE_KEY`: YouTube API Key,支持多个 key,用英文逗号 `,` 隔开 - telegram - 贴纸包路由: [Telegram 机器人](https://telegram.org/blog/bot-revolution) diff --git a/lib/routes/youtube/utils.js b/lib/routes/youtube/utils.js index 7a1840152b..0a9c1d67b9 100644 --- a/lib/routes/youtube/utils.js +++ b/lib/routes/youtube/utils.js @@ -1,14 +1,32 @@ const { google } = require('googleapis'); const config = require('@/config').value; -const youtube = google.youtube({ - version: 'v3', - auth: config.youtube.key, -}); +let getYoutube = () => null; +if (config.youtube.key) { + const keys = config.youtube.key.split(','); + const youtube = {}; + let count = 0; + let index = -1; + + keys.forEach((key, index) => { + if (key) { + youtube[index] = google.youtube({ + version: 'v3', + auth: key, + }); + count = index + 1; + } + }); + + getYoutube = () => { + index++; + return youtube[index % count]; + }; +} const youtubeUtils = { getPlaylistItems: async (id, part) => { - const res = await youtube.playlistItems.list({ + const res = await getYoutube().playlistItems.list({ part, playlistId: id, }); @@ -16,7 +34,7 @@ const youtubeUtils = { }, getPlaylist: async (id, part, cache) => await cache.tryGet('getPlaylist' + id, async () => { - const res = await youtube.playlists.list({ + const res = await getYoutube().playlists.list({ part, id: id, }); @@ -24,7 +42,7 @@ const youtubeUtils = { }), getChannelWithId: async (id, part, cache) => await cache.tryGet('getChannelWithId' + id, async () => { - const res = await youtube.channels.list({ + const res = await getYoutube().channels.list({ part, id: id, }); @@ -32,7 +50,7 @@ const youtubeUtils = { }), getChannelWithUsername: async (username, part, cache) => await cache.tryGet('getPlaylist' + username, async () => { - const res = await youtube.channels.list({ + const res = await getYoutube().channels.list({ part, forUsername: username, });