feat(route): youtube custom channel url (#11085)

* feat(route): youtube custom url

* docs: fix typo
This commit is contained in:
Tony
2022-10-14 00:45:22 +04:00
committed by GitHub
parent 934efb840b
commit f1d696ed21
5 changed files with 60 additions and 0 deletions
+4
View File
@@ -490,6 +490,10 @@ YouTube provides official RSS feeds for channels, for instance <https://www.yout
<RouteEn author="DIYgod" 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']" radar="1" rssbud="1"/>
### Custom URL
<RouteEn author="TonyRL" path="/youtube/c/:id/:embed?" example="/youtube/c/YouTubeCreators" :paramsDesc="['YouTube custom URL', 'Default to embed the video, set to any value to disable embedding']" radar="1" rssbud="1"/>
### Playlist
<RouteEn author="HenryQW" 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']" radar="1" rssbud="1"/>
+4
View File
@@ -867,6 +867,10 @@ YouTube 官方亦有提供频道 RSS,形如 <https://www.youtube.com/feeds/vid
<Route author="DIYgod" example="/youtube/channel/UCDwDMPOZfxVV0x_dz0eQ8KQ" path="/youtube/channel/:id/:disableEmbed?" :paramsDesc="['频道 id', '默认为开启内嵌视频, 任意值为关闭']" radar="1" rssbud="1"/>
### 自定义网址
<Route author="TonyRL" path="/youtube/c/:id/:embed?" example="/youtube/c/YouTubeCreators" :paramsDesc="['YouTube 自定义网址', '默认为开启内嵌视频, 任意值为关闭']" radar="1" rssbud="1"/>
### 播放列表
<Route author="HenryQW" example="/youtube/playlist/PLqQ1RwlxOgeLTJ1f3fNMSwhjVgaWKo_9Z" path="/youtube/playlist/:id/:disableEmbed?" :paramsDesc="['播放列表 id', '默认为开启内嵌视频, 任意值为关闭']" radar="1" rssbud="1"/>
+45
View File
@@ -0,0 +1,45 @@
const utils = require('./utils');
const config = require('@/config').value;
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
if (!config.youtube || !config.youtube.key) {
throw 'YouTube RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#pei-zhi-bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
}
const { username } = ctx.params;
const embed = !ctx.params.embed;
const { data: response } = await got(`https://www.youtube.com/c/${username}`);
const $ = cheerio.load(response);
const ytInitialData = JSON.parse(
$('script')
.text()
.match(/ytInitialData = ({.*?});/)[1]
);
const externalId = ytInitialData.metadata.channelMetadataRenderer.externalId;
const playlistId = (await utils.getChannelWithId(externalId, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads;
const data = (await utils.getPlaylistItems(playlistId, 'snippet', ctx.cache)).data.items;
ctx.state.data = {
title: `${username} - YouTube`,
link: `https://www.youtube.com/c/${username}`,
description: ytInitialData.metadata.channelMetadataRenderer.description,
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: utils.renderDescription(embed, videoId, img, utils.formatDescription(snippet.description)),
pubDate: parseDate(snippet.publishedAt),
link: `https://www.youtube.com/watch?v=${videoId}`,
author: snippet.videoOwnerChannelTitle,
};
}),
};
};
+6
View File
@@ -14,6 +14,12 @@ module.exports = {
source: '/channel/:id',
target: '/youtube/channel/:id',
},
{
title: '自定义网址',
docs: 'https://docs.rsshub.app/social-media.html#youtube',
source: '/c/:id',
target: '/youtube/c/:id',
},
{
title: '播放列表',
docs: 'https://docs.rsshub.app/social-media.html#youtube',
+1
View File
@@ -1,4 +1,5 @@
module.exports = (router) => {
router.get('/c/:username/:embed?', require('./custom'));
router.get('/channel/:id/:embed?', require('./channel'));
router.get('/playlist/:id/:embed?', require('./playlist'));
router.get('/subscriptions/:embed?', require('./subscriptions'));