diff --git a/docs/README.md b/docs/README.md index 663fd648b..eaa53104b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -450,9 +450,11 @@ RSSHub 提供下列 API 接口: ### Youtube - + - + + + ### Dribbble diff --git a/docs/en/README.md b/docs/en/README.md index c5c6b21ee..2a9718228 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -218,9 +218,11 @@ If no matching results were found, the server returns only a HTTP status code `2 ### Youtube - + - + + + ### Telegram diff --git a/router.js b/router.js index 4da4bf24e..ff19cc536 100644 --- a/router.js +++ b/router.js @@ -226,8 +226,9 @@ router.get('/instagram/user/:id', require('./routes/instagram/user')); // Youtube if (config.youtube && config.youtube.key) { - router.get('/youtube/user/:username', require('./routes/youtube/user')); - router.get('/youtube/channel/:id', require('./routes/youtube/channel')); + router.get('/youtube/user/:username/:embed?', require('./routes/youtube/user')); + router.get('/youtube/channel/:id/:embed?', require('./routes/youtube/channel')); + router.get('/youtube/playlist/:id/:embed?', require('./routes/youtube/playlist')); } else { logger.warn('Youtube RSS is disabled for lacking config.'); } diff --git a/routes/youtube/channel.js b/routes/youtube/channel.js index 3bd8e4fb2..08a777096 100644 --- a/routes/youtube/channel.js +++ b/routes/youtube/channel.js @@ -1,25 +1,12 @@ -const { google } = require('googleapis'); -const config = require('../../config'); - -const youtube = google.youtube({ - version: 'v3', - auth: config.youtube.key, -}); +const utils = require('./utils'); module.exports = async (ctx) => { const id = ctx.params.id; + const embed = !ctx.params.embed; - const playlistIdResponst = await youtube.channels.list({ - part: 'contentDetails', - id: id, - }); - const playlistId = playlistIdResponst.data.items[0].contentDetails.relatedPlaylists.uploads; + const playlistId = (await utils.getChannelWithId(id, 'contentDetails')).data.items[0].contentDetails.relatedPlaylists.uploads; - const responst = await youtube.playlistItems.list({ - part: 'snippet,contentDetails,status', - playlistId: playlistId, - }); - const data = responst.data.items; + const data = (await utils.getPlaylistItems(playlistId, 'snippet,contentDetails')).data.items; ctx.state.data = { title: `${data[0].snippet.channelTitle} 的 Youtube 视频`, @@ -27,12 +14,15 @@ module.exports = async (ctx) => { description: `${data[0].snippet.channelTitle} 的 Youtube 视频`, item: data.map((item) => { const snippet = item.snippet; - const img = snippet.thumbnails.maxres || snippet.thumbnails.standard || snippet.thumbnails.high || snippet.thumbnails.medium || snippet.thumbnails.default; + const videoId = snippet.resourceId.videoId; + const img = utils.getThumbnail(snippet.thumbnails); return { title: snippet.title, - description: `${snippet.description}`, + description: `${embed ? '' : ''}${utils.formatDescription( + snippet.description + )}`, pubDate: new Date(snippet.publishedAt).toUTCString(), - link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}`, + link: `https://www.youtube.com/watch?v=${videoId}`, }; }), }; diff --git a/routes/youtube/playlist.js b/routes/youtube/playlist.js new file mode 100644 index 000000000..2cd9d05bc --- /dev/null +++ b/routes/youtube/playlist.js @@ -0,0 +1,28 @@ +const utils = require('./utils'); + +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 data = (await utils.getPlaylistItems(id, 'snippet,contentDetails')).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.map((item) => { + const snippet = item.snippet; + const videoId = snippet.resourceId.videoId; + const img = utils.getThumbnail(snippet.thumbnails); + return { + title: snippet.title, + description: `${embed ? '' : ''} + ${utils.formatDescription(snippet.description)}`, + pubDate: new Date(snippet.publishedAt).toUTCString(), + link: `https://www.youtube.com/watch?v=${videoId}`, + }; + }), + }; +}; diff --git a/routes/youtube/user.js b/routes/youtube/user.js index 10dbf6262..7a0df544f 100644 --- a/routes/youtube/user.js +++ b/routes/youtube/user.js @@ -1,25 +1,12 @@ -const { google } = require('googleapis'); -const config = require('../../config'); - -const youtube = google.youtube({ - version: 'v3', - auth: config.youtube.key, -}); +const utils = require('./utils'); module.exports = async (ctx) => { const username = ctx.params.username; + const embed = !ctx.params.embed; - const playlistIdResponst = await youtube.channels.list({ - part: 'contentDetails', - forUsername: username, - }); - const playlistId = playlistIdResponst.data.items[0].contentDetails.relatedPlaylists.uploads; + const playlistId = (await utils.getChannelWithUsername(username, 'contentDetails')).data.items[0].contentDetails.relatedPlaylists.uploads; - const responst = await youtube.playlistItems.list({ - part: 'snippet,contentDetails,status', - playlistId: playlistId, - }); - const data = responst.data.items; + const data = (await utils.getPlaylistItems(playlistId, 'snippet,contentDetails')).data.items; ctx.state.data = { title: `${username} 的 Youtube 视频`, @@ -27,12 +14,15 @@ module.exports = async (ctx) => { description: `${username} 的 Youtube 视频`, item: data.map((item) => { const snippet = item.snippet; - const img = snippet.thumbnails.maxres || snippet.thumbnails.standard || snippet.thumbnails.high || snippet.thumbnails.medium || snippet.thumbnails.default; + const videoId = snippet.resourceId.videoId; + const img = utils.getThumbnail(snippet.thumbnails); return { title: snippet.title, - description: `${snippet.description}`, + description: `${embed ? '' : ''}${utils.formatDescription( + snippet.description + )}`, pubDate: new Date(snippet.publishedAt).toUTCString(), - link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}`, + link: `https://www.youtube.com/watch?v=${videoId}`, }; }), }; diff --git a/routes/youtube/utils.js b/routes/youtube/utils.js new file mode 100644 index 000000000..cf34c2ca6 --- /dev/null +++ b/routes/youtube/utils.js @@ -0,0 +1,42 @@ +const { google } = require('googleapis'); +const config = require('../../config'); + +const youtube = google.youtube({ + version: 'v3', + auth: config.youtube.key, +}); + +const youtubeUtils = { + getPlaylistItems: async (id, part) => { + const res = await youtube.playlistItems.list({ + part, + playlistId: id, + }); + 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; + }, + getThumbnail: (thumbnails) => thumbnails.maxres || thumbnails.standard || thumbnails.high || thumbnails.medium || thumbnails.default, + formatDescription: (description) => description.replace(/(?:\r\n|\r|\n)/g, '
'), +}; + +module.exports = youtubeUtils;