From 4e8c8fb27a1a8915839f59f1fb61d804fa14d862 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Thu, 14 Aug 2025 10:53:41 +0800 Subject: [PATCH] fix(route/youtube): cache getSubtitles (#19811) * fix(route/youtube): cache getSubtitles * fix null description --- eslint.config.mjs | 2 +- lib/routes/youtube/api/youtubei.ts | 14 ++++++++++---- lib/routes/youtube/utils.ts | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 01a81faf6..6106ed5ee 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -146,7 +146,7 @@ unicorn.configs.recommended, 'unicorn/no-hex-escape': 'warn', 'unicorn/no-null': 'off', 'unicorn/no-object-as-default-parameter': 'warn', - 'unicorn/no-nested-ternary': 'warn', + 'unicorn/no-nested-ternary': 'off', 'unicorn/no-process-exit': 'off', 'unicorn/no-useless-switch-case': 'off', diff --git a/lib/routes/youtube/api/youtubei.ts b/lib/routes/youtube/api/youtubei.ts index ef750167b..213c98225 100644 --- a/lib/routes/youtube/api/youtubei.ts +++ b/lib/routes/youtube/api/youtubei.ts @@ -51,6 +51,13 @@ export const getDataByUsername = async ({ username, embed, filterShorts }: { use return getDataByChannelId({ channelId, embed, filterShorts }); }; +const getSubtitlesByVideoId = (videoId: string) => + cache.tryGet(`youtube:getSubtitlesByVideoId:${videoId}`, async () => { + const subtitles = await getSubtitles({ videoID: videoId }); + const srt = convertToSrt(subtitles); + return srt; + }); + export const getDataByChannelId = async ({ channelId, embed }: { channelId: string; embed: boolean; filterShorts: boolean }): Promise => { const innertube = await innertubePromise; const channel = await innertube.getChannel(channelId); @@ -66,17 +73,16 @@ export const getDataByChannelId = async ({ channelId, embed }: { channelId: stri videos.videos .filter((video) => 'video_id' in video) .map(async (video) => { - const subtitles = await getSubtitles({ videoID: video.video_id }); - const srt = convertToSrt(subtitles); + const srt = await getSubtitlesByVideoId(video.video_id); const dataUrl = `data:text/plain;charset=utf-8,${encodeURIComponent(srt)}`; - const img = 'best_thumbnail' in video ? video.best_thumbnail?.url : ('thumbnails' in video ? video.thumbnails?.[0]?.url : undefined); + const img = 'best_thumbnail' in video ? video.best_thumbnail?.url : 'thumbnails' in video ? video.thumbnails?.[0]?.url : undefined; return { title: video.title.text || `YouTube Video ${video.video_id}`, description: 'description_snippet' in video ? utils.renderDescription(embed, video.video_id, img, utils.formatDescription(video.description_snippet?.toHTML())) : null, link: `https://www.youtube.com/watch?v=${video.video_id}`, - author: typeof video.author === 'string' ? video.author : (video.author.name === 'N/A' ? undefined : video.author.name), + author: typeof video.author === 'string' ? video.author : video.author.name === 'N/A' ? undefined : video.author.name, image: img, pubDate: 'published' in video && video.published?.text ? parseRelativeDate(video.published.text) : undefined, attachments: [ diff --git a/lib/routes/youtube/utils.ts b/lib/routes/youtube/utils.ts index e9365048d..114d3574f 100644 --- a/lib/routes/youtube/utils.ts +++ b/lib/routes/youtube/utils.ts @@ -61,7 +61,7 @@ export const getVideos = (id, part, cache) => return res; }); export const getThumbnail = (thumbnails) => thumbnails.maxres || thumbnails.standard || thumbnails.high || thumbnails.medium || thumbnails.default; -export const formatDescription = (description) => description.replaceAll(/\r\n|\r|\n/g, '
'); +export const formatDescription = (description) => description?.replaceAll(/\r\n|\r|\n/g, '
'); export const renderDescription = (embed, videoId, img, description) => art(path.join(__dirname, 'templates/description.art'), { embed,