feat: support multiple youtube keys

This commit is contained in:
DIYgod
2020-03-09 01:57:24 +08:00
parent 3c3da25c6f
commit aadb6ba0e5
3 changed files with 28 additions and 10 deletions

View File

@@ -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: [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) - telegram: [Bot application](https://telegram.org/blog/bot-revolution)

View File

@@ -373,7 +373,7 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
- youtube 全部路由: [申请地址](https://console.developers.google.com/) - 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) - telegram - 贴纸包路由: [Telegram 机器人](https://telegram.org/blog/bot-revolution)

View File

@@ -1,14 +1,32 @@
const { google } = require('googleapis'); const { google } = require('googleapis');
const config = require('@/config').value; const config = require('@/config').value;
const youtube = google.youtube({ let getYoutube = () => null;
version: 'v3', if (config.youtube.key) {
auth: 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 = { const youtubeUtils = {
getPlaylistItems: async (id, part) => { getPlaylistItems: async (id, part) => {
const res = await youtube.playlistItems.list({ const res = await getYoutube().playlistItems.list({
part, part,
playlistId: id, playlistId: id,
}); });
@@ -16,7 +34,7 @@ const youtubeUtils = {
}, },
getPlaylist: async (id, part, cache) => getPlaylist: async (id, part, cache) =>
await cache.tryGet('getPlaylist' + id, async () => { await cache.tryGet('getPlaylist' + id, async () => {
const res = await youtube.playlists.list({ const res = await getYoutube().playlists.list({
part, part,
id: id, id: id,
}); });
@@ -24,7 +42,7 @@ const youtubeUtils = {
}), }),
getChannelWithId: async (id, part, cache) => getChannelWithId: async (id, part, cache) =>
await cache.tryGet('getChannelWithId' + id, async () => { await cache.tryGet('getChannelWithId' + id, async () => {
const res = await youtube.channels.list({ const res = await getYoutube().channels.list({
part, part,
id: id, id: id,
}); });
@@ -32,7 +50,7 @@ const youtubeUtils = {
}), }),
getChannelWithUsername: async (username, part, cache) => getChannelWithUsername: async (username, part, cache) =>
await cache.tryGet('getPlaylist' + username, async () => { await cache.tryGet('getPlaylist' + username, async () => {
const res = await youtube.channels.list({ const res = await getYoutube().channels.list({
part, part,
forUsername: username, forUsername: username,
}); });