From 04a17c0b05d9d95afc38ef1872fe59176996aac8 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Fri, 10 Oct 2025 23:18:09 +0800 Subject: [PATCH] fix(route/bilibili): retry get subtitle with new cookie --- lib/routes/bilibili/cache.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/routes/bilibili/cache.ts b/lib/routes/bilibili/cache.ts index a3828fab0..befd2fb61 100644 --- a/lib/routes/bilibili/cache.ts +++ b/lib/routes/bilibili/cache.ts @@ -29,7 +29,7 @@ const getCookie = (disableConfig = false) => { } } - return config.bilibili.cookies[Object.keys(config.bilibili.cookies)[Math.floor(Math.random() * Object.keys(config.bilibili.cookies).length)]]; + return config.bilibili.cookies[Object.keys(config.bilibili.cookies)[Math.floor(Math.random() * Object.keys(config.bilibili.cookies).length)]] || ''; } const key = 'bili-cookie'; return cache.tryGet(key, async () => { @@ -259,15 +259,27 @@ const getVideoSubtitle = async ( return []; } - const cookie = await getCookie(); return cache.tryGet(`bili-video-subtitle-${bvid}`, async () => { await subtitleLimiterQueue.removeTokens(1); - const response = await got(`https://api.bilibili.com/x/player/wbi/v2?bvid=${bvid}&cid=${cid}`, { - headers: { - Referer: `https://www.bilibili.com/video/${bvid}`, - Cookie: cookie, - }, - }); + + const getSubtitleData = async (cookie: string) => { + const response = await got(`https://api.bilibili.com/x/player/wbi/v2?bvid=${bvid}&cid=${cid}`, { + headers: { + Referer: `https://www.bilibili.com/video/${bvid}`, + Cookie: cookie, + }, + }); + return response; + }; + + let response; + const cookie = await getCookie(); + response = await getSubtitleData(cookie); + + if (response?.data?.data?.subtitle?.permission === '0') { + const newCookie = await getCookie(true); + response = await getSubtitleData(newCookie); + } const subtitles = response?.data?.data?.subtitle?.subtitles || [];