From b36e26b7a1baa9cd40b78d89463ece47e252336e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 5 Feb 2020 16:39:31 +0800 Subject: [PATCH] feat: youtube cache --- lib/routes/youtube/channel.js | 2 +- lib/routes/youtube/playlist.js | 2 +- lib/routes/youtube/user.js | 2 +- lib/routes/youtube/utils.js | 45 ++++++++++++++++++---------------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/routes/youtube/channel.js b/lib/routes/youtube/channel.js index 757bd74615..4de629252a 100644 --- a/lib/routes/youtube/channel.js +++ b/lib/routes/youtube/channel.js @@ -8,7 +8,7 @@ module.exports = async (ctx) => { const id = ctx.params.id; const embed = !ctx.params.embed; - const playlistId = (await utils.getChannelWithId(id, 'contentDetails')).data.items[0].contentDetails.relatedPlaylists.uploads; + const playlistId = (await utils.getChannelWithId(id, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads; const data = (await utils.getPlaylistItems(playlistId, 'snippet,contentDetails')).data.items; diff --git a/lib/routes/youtube/playlist.js b/lib/routes/youtube/playlist.js index 0a69c41c70..f96b5d04d5 100644 --- a/lib/routes/youtube/playlist.js +++ b/lib/routes/youtube/playlist.js @@ -8,7 +8,7 @@ module.exports = async (ctx) => { const id = ctx.params.id; const embed = !ctx.params.embed; - const playlistTitle = (await utils.getPlaylist(id, 'snippet')).data.items[0].snippet.title; + const playlistTitle = (await utils.getPlaylist(id, 'snippet', ctx.cache)).data.items[0].snippet.title; const data = (await utils.getPlaylistItems(id, 'snippet,contentDetails')).data.items; diff --git a/lib/routes/youtube/user.js b/lib/routes/youtube/user.js index ebd8643e34..918799c47b 100644 --- a/lib/routes/youtube/user.js +++ b/lib/routes/youtube/user.js @@ -8,7 +8,7 @@ module.exports = async (ctx) => { const username = ctx.params.username; const embed = !ctx.params.embed; - const playlistId = (await utils.getChannelWithUsername(username, 'contentDetails')).data.items[0].contentDetails.relatedPlaylists.uploads; + const playlistId = (await utils.getChannelWithUsername(username, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads; const data = (await utils.getPlaylistItems(playlistId, 'snippet,contentDetails')).data.items; diff --git a/lib/routes/youtube/utils.js b/lib/routes/youtube/utils.js index f9467a647e..7a1840152b 100644 --- a/lib/routes/youtube/utils.js +++ b/lib/routes/youtube/utils.js @@ -14,27 +14,30 @@ const youtubeUtils = { }); return res; }, - getPlaylist: async (id, part) => { - const res = await youtube.playlists.list({ - part, - id: id, - }); - return res; - }, - getChannelWithId: async (id, part) => { - const res = await youtube.channels.list({ - part, - id: id, - }); - return res; - }, - getChannelWithUsername: async (username, part) => { - const res = await youtube.channels.list({ - part, - forUsername: username, - }); - return res; - }, + getPlaylist: async (id, part, cache) => + await cache.tryGet('getPlaylist' + id, async () => { + const res = await youtube.playlists.list({ + part, + id: id, + }); + return res; + }), + getChannelWithId: async (id, part, cache) => + await cache.tryGet('getChannelWithId' + id, async () => { + const res = await youtube.channels.list({ + part, + id: id, + }); + return res; + }), + getChannelWithUsername: async (username, part, cache) => + await cache.tryGet('getPlaylist' + username, async () => { + const res = await youtube.channels.list({ + part, + forUsername: username, + }); + return res; + }), getThumbnail: (thumbnails) => thumbnails.maxres || thumbnails.standard || thumbnails.high || thumbnails.medium || thumbnails.default, formatDescription: (description) => description.replace(/(?:\r\n|\r|\n)/g, '
'), };