From 4e8a3bb3d8188a25ddaefe13b7ac3a932f3a931b Mon Sep 17 00:00:00 2001 From: sohow <805124313@qq.com> Date: Wed, 1 Feb 2023 16:57:47 +0800 Subject: [PATCH] fix(route): youtube playlistId (#11739) * fix youtube playlistId * Update lib/v2/youtube/user.js * Update lib/v2/youtube/user.js * Update user.js * Update user.js --------- Co-authored-by: hanzongwen --- lib/v2/youtube/user.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/v2/youtube/user.js b/lib/v2/youtube/user.js index d722dbd08..6164e4c92 100644 --- a/lib/v2/youtube/user.js +++ b/lib/v2/youtube/user.js @@ -1,6 +1,8 @@ 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) { @@ -9,13 +11,23 @@ module.exports = async (ctx) => { const username = ctx.params.username; const embed = !ctx.params.embed; - const playlistId = (await utils.getChannelWithUsername(username, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads; + let playlistId; + let channelName; + if (username.startsWith('@')) { + const link = `https://www.youtube.com/${username}`; + const response = await got(link); + const $ = cheerio.load(response.data); + const channelId = $('meta[itemprop="channelId"]').attr('content'); + channelName = $('meta[itemprop="name"]').attr('content'); + playlistId = (await utils.getChannelWithId(channelId, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads; + } + playlistId = playlistId || (await utils.getChannelWithUsername(username, '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/user/${username}`, + title: `${channelName || username} - YouTube`, + link: username.startsWith('@') ? `https://www.youtube.com/${username}` : `https://www.youtube.com/user/${username}`, description: `YouTube user ${username}`, item: data .filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video')