feat(route): add router YouTube Live (#13110)

* feat(route): add router YouTube Live

* add docs

* Insert new entries in alphabetical order.

---------
This commit is contained in:
Sussurr
2023-08-25 03:54:43 +08:00
committed by GitHub
parent eebd99c85a
commit 71046fc4c5
6 changed files with 75 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
const utils = require('./utils');
const config = require('@/config').value;
const { parseDate } = require('@/utils/parse-date');
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
if (!config.youtube || !config.youtube.key) {
throw Error('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.username;
const embed = !ctx.params.embed;
let channelName;
let channelId;
const link = `https://www.youtube.com/${username}`;
const response = await got(link);
const $ = cheerio.load(response.data);
channelId = $('meta[itemprop="identifier"]').attr('content');
channelName = $('meta[itemprop="name"]').attr('content');
if (!channelId) {
const channelInfo = (await utils.getChannelWithUsername(username, 'snippet', ctx.cache)).data.items[0];
channelId = channelInfo.id;
channelName = channelInfo.snippet.title;
}
const data = (await utils.getLive(channelId, ctx.cache)).data.items;
ctx.state.data = {
title: `$${channelName || username}'s Live Status`,
link: `https://www.youtube.com/channel/${channelId}`,
description: `$${channelName || username}'s live streaming status`,
item: data.map((item) => {
const snippet = item.snippet;
const liveVideoId = item.id.videoId;
const img = utils.getThumbnail(snippet.thumbnails);
return {
title: snippet.title,
description: utils.renderDescription(embed, liveVideoId, img, utils.formatDescription(snippet.description)),
pubDate: parseDate(snippet.publishedAt),
guid: liveVideoId,
link: `https://www.youtube.com/watch?v=${liveVideoId}`,
};
}),
allowEmpty: true,
};
};
+1
View File
@@ -3,6 +3,7 @@ module.exports = {
'/channel/:id/:embed?': ['DIYgod'],
'/charts/:category?/:country?/:embed?': ['TonyRL'],
'/community/:handle': ['TonyRL'],
'/live/:username/:embed?': ['sussurr127'],
'/playlist/:id/:embed?': ['HenryQW'],
'/subscriptions/:embed?': ['TonyRL'],
'/user/:username/:embed?': ['DIYgod'],
+6
View File
@@ -52,6 +52,12 @@ module.exports = {
source: ['/feed/subscriptions', '/feed/channels'],
target: '/youtube/subscriptions',
},
{
title: 'Live',
docs: 'https://docs.rsshub.app/routes/live#youtube-live',
source: '/:handle',
target: (params) => (params.handle.startsWith('@') ? `/youtube/live/${params.handle}` : ''),
},
],
},
};
+1
View File
@@ -3,6 +3,7 @@ module.exports = (router) => {
router.get('/channel/:id/:embed?', require('./channel'));
router.get('/charts/:category?/:country?/:embed?', require('./charts'));
router.get('/community/:handle', require('./community'));
router.get('/live/:username/:embed?', require('./live'));
router.get('/playlist/:id/:embed?', require('./playlist'));
router.get('/subscriptions/:embed?', require('./subscriptions'));
router.get('/user/:username/:embed?', require('./user'));
+12
View File
@@ -138,6 +138,18 @@ const youtubeUtils = {
},
// taken from https://webapps.stackexchange.com/a/101153
isYouTubeChannelId: (id) => /^UC[\w-]{21}[AQgw]$/.test(id),
getLive: (id, cache) =>
cache.tryGet(`youtube:getLive:${id}`, async () => {
const res = await exec((youtube) =>
youtube.search.list({
part: 'snippet',
channelId: id,
eventType: 'live',
type: 'video',
})
);
return res;
}),
};
module.exports = youtubeUtils;
+6
View File
@@ -37,6 +37,12 @@
<Route author="Kiotlin" example="/yoasobi-music/media" path="/yoasobi-music/media" radar="1" rssbud="1" />
## YouTube Live {#youtube-live}
### Live {#youtube-live-live}
<Route author="sussurr127" path="/youtube/live/:username/:embed?" example="/youtube/live/@GawrGura" paramsDesc={['YouTuber id', 'Default to embed the video, set to any value to disable embedding']} radar="1"/>
## 哔哩哔哩直播 {#bi-li-bi-li-zhi-bo}
### 直播开播 {#bi-li-bi-li-zhi-bo-zhi-bo-kai-bo}