fix(route/bilibili): retry get subtitle with new cookie

This commit is contained in:
Stephen Zhou
2025-10-10 23:18:09 +08:00
parent aeec1eda5d
commit 04a17c0b05
+20 -8
View File
@@ -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 || [];