diff --git a/lib/v2/youtube/live.js b/lib/v2/youtube/live.js new file mode 100644 index 000000000..186cfb82a --- /dev/null +++ b/lib/v2/youtube/live.js @@ -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 relevant config'); + } + 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, + }; +}; diff --git a/lib/v2/youtube/maintainer.js b/lib/v2/youtube/maintainer.js index a27b6416b..e9db0b305 100644 --- a/lib/v2/youtube/maintainer.js +++ b/lib/v2/youtube/maintainer.js @@ -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'], diff --git a/lib/v2/youtube/radar.js b/lib/v2/youtube/radar.js index 68f44cb41..8d5d6e019 100644 --- a/lib/v2/youtube/radar.js +++ b/lib/v2/youtube/radar.js @@ -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}` : ''), + }, ], }, }; diff --git a/lib/v2/youtube/router.js b/lib/v2/youtube/router.js index 7eb188500..3e95f3667 100644 --- a/lib/v2/youtube/router.js +++ b/lib/v2/youtube/router.js @@ -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')); diff --git a/lib/v2/youtube/utils.js b/lib/v2/youtube/utils.js index 498c1ad64..bd73b58de 100644 --- a/lib/v2/youtube/utils.js +++ b/lib/v2/youtube/utils.js @@ -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; diff --git a/website/docs/routes/live.md b/website/docs/routes/live.md index 1443d7eae..ff2693785 100644 --- a/website/docs/routes/live.md +++ b/website/docs/routes/live.md @@ -37,6 +37,12 @@ +## YouTube Live {#youtube-live} + +### Live {#youtube-live-live} + + + ## 哔哩哔哩直播 {#bi-li-bi-li-zhi-bo} ### 直播开播 {#bi-li-bi-li-zhi-bo-zhi-bo-kai-bo}