feat: youtube cache

This commit is contained in:
DIYgod
2020-02-05 16:39:31 +08:00
parent f6baa8d2cc
commit b36e26b7a1
4 changed files with 27 additions and 24 deletions
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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;
+24 -21
View File
@@ -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, '<br>'),
};