feat: youtube playlist author

This commit is contained in:
DIYgod 2020-10-15 00:19:54 +08:00
parent 5743604804
commit e82d5d2e6c
No known key found for this signature in database
GPG Key ID: D159328F47A80DCA
2 changed files with 25 additions and 14 deletions

View File

@ -10,25 +10,26 @@ module.exports = async (ctx) => {
const playlistTitle = (await utils.getPlaylist(id, 'snippet', ctx.cache)).data.items[0].snippet.title;
const data = (await utils.getPlaylistItems(id, 'snippet,contentDetails')).data.items;
const data = (await utils.getPlaylistItems(id, 'snippet')).data.items.filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video');
const ids = data.map((item) => item.snippet.resourceId.videoId).join(',');
const authors = (await utils.getVideoAuthor(ids, 'snippet')).data.items;
ctx.state.data = {
title: `Playlist: ${playlistTitle} by ${data[0].snippet.channelTitle}`,
link: `https://www.youtube.com/playlist?list=${id}`,
description: `Playlist: ${playlistTitle} by ${data[0].snippet.channelTitle}`,
item: data
.filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video')
.map((item) => {
const snippet = item.snippet;
const videoId = snippet.resourceId.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
return {
title: snippet.title,
description: `${embed ? '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/' + videoId + '" frameborder="0" allowfullscreen></iframe>' : ''}
item: data.map((item, index) => {
const snippet = item.snippet;
const videoId = snippet.resourceId.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
return {
title: snippet.title,
description: `${embed ? '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/' + videoId + '" frameborder="0" allowfullscreen></iframe>' : ''}
${utils.formatDescription(snippet.description)}<img src="${img.url}">`,
pubDate: new Date(snippet.publishedAt).toUTCString(),
link: `https://www.youtube.com/watch?v=${videoId}`,
};
}),
pubDate: new Date(snippet.publishedAt).toUTCString(),
link: `https://www.youtube.com/watch?v=${videoId}`,
author: authors[index].snippet.channelTitle,
};
}),
};
};

View File

@ -78,6 +78,16 @@ const youtubeUtils = {
);
return res;
}),
getVideoAuthor: async (id, part) => {
const res = await exec(
async (youtube) =>
await youtube.videos.list({
part,
id,
})
);
return res;
},
getThumbnail: (thumbnails) => thumbnails.maxres || thumbnails.standard || thumbnails.high || thumbnails.medium || thumbnails.default,
formatDescription: (description) => description.replace(/(?:\r\n|\r|\n)/g, '<br>'),
};