Added playlist support. Refactored code (#795)

Close #728 

1. 重复代码精简
1. 可选内嵌视频,测试 Reeder iOS/mac 以及 TTRSS Chrome 完美播放。
1. Description 换行处理
This commit is contained in:
Henry Wang
2018-09-26 17:20:22 +08:00
committed by DIYgod
parent c7b3eb3be6
commit 72bf98117a
7 changed files with 101 additions and 46 deletions
+4 -2
View File
@@ -450,9 +450,11 @@ RSSHub 提供下列 API 接口:
### Youtube
<route name="用户" author="DIYgod" example="/youtube/user/JFlaMusic" path="/youtube/user/:username" :paramsDesc="['用户名']"/>
<route name="用户" author="DIYgod" example="/youtube/user/JFlaMusic/" path="/youtube/user/:username/:embed?" :paramsDesc="['用户名', '默认为开启内嵌视频, 任意值为关闭']"/>
<route name="频道" author="DIYgod" example="/youtube/channel/UCDwDMPOZfxVV0x_dz0eQ8KQ>" path="/youtube/channel/:id" :paramsDesc="['频道 id']"/>
<route name="频道" author="DIYgod" example="/youtube/channel/UCDwDMPOZfxVV0x_dz0eQ8KQ" path="/youtube/channel/:id/:embed?" :paramsDesc="['频道 id', '默认为开启内嵌视频, 任意值为关闭']"/>
<route name="播放列表" author="HenryQW" example="/youtube/playlist/PLqQ1RwlxOgeLTJ1f3fNMSwhjVgaWKo_9Z" path="/youtube/playlist/:id/:embed?" :paramsDesc="['播放列表 id', '默认为开启内嵌视频, 任意值为关闭']"/>
### Dribbble
+4 -2
View File
@@ -218,9 +218,11 @@ If no matching results were found, the server returns only a HTTP status code `2
### Youtube
<routeEn name="User" path="/youtube/user/:username" example="/youtube/user/JFlaMusic" :paramsDesc="['YouTuber id']" />
<routeEn name="User" path="/youtube/user/:username/:embed?" example="/youtube/user/JFlaMusic" :paramsDesc="['YouTuber id', 'Default to embed the video, set to any value to disable embedding']" />
<routeEn name="Channel" path="/youtube/channel/:id" example="/youtube/channel/UCDwDMPOZfxVV0x_dz0eQ8KQ" :paramsDesc="['YouTube channel id']" />
<routeEn name="Channel" path="/youtube/channel/:id/:embed?" example="/youtube/channel/UCDwDMPOZfxVV0x_dz0eQ8KQ" :paramsDesc="['YouTube channel id', 'Default to embed the video, set to any value to disable embedding']" />
<routeEn name="Playlist" path="/youtube/playlist/:id/:embed?" example="/youtube/playlist/PLqQ1RwlxOgeLTJ1f3fNMSwhjVgaWKo_9Z" :paramsDesc="['YouTube playlist id', 'Default to embed the video, set to any value to disable embedding']" />
### Telegram
+3 -2
View File
@@ -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.');
}
+10 -20
View File
@@ -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}<img referrerpolicy="no-referrer" src="${img.url}">`,
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 referrerpolicy="no-referrer" src="${img.url}">`,
pubDate: new Date(snippet.publishedAt).toUTCString(),
link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}`,
link: `https://www.youtube.com/watch?v=${videoId}`,
};
}),
};
+28
View File
@@ -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 ? '<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 referrerpolicy="no-referrer" src="${img.url}">`,
pubDate: new Date(snippet.publishedAt).toUTCString(),
link: `https://www.youtube.com/watch?v=${videoId}`,
};
}),
};
};
+10 -20
View File
@@ -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}<img referrerpolicy="no-referrer" src="${img.url}">`,
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 referrerpolicy="no-referrer" src="${img.url}">`,
pubDate: new Date(snippet.publishedAt).toUTCString(),
link: `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}`,
link: `https://www.youtube.com/watch?v=${videoId}`,
};
}),
};
+42
View File
@@ -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, '<br>'),
};
module.exports = youtubeUtils;