From 5cc315f6c2c28cbf45f1d7cd454449674b8c01ac Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 27 May 2025 13:36:48 +0800 Subject: [PATCH] feat: bilibili check cookie api --- lib/routes/bilibili/cache.ts | 4 +--- lib/routes/bilibili/check-cookie.ts | 31 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 lib/routes/bilibili/check-cookie.ts diff --git a/lib/routes/bilibili/cache.ts b/lib/routes/bilibili/cache.ts index 0bd54c3bac..f567a8cc73 100644 --- a/lib/routes/bilibili/cache.ts +++ b/lib/routes/bilibili/cache.ts @@ -7,10 +7,8 @@ import logger from '@/utils/logger'; import puppeteer from '@/utils/puppeteer'; import { JSDOM } from 'jsdom'; -const disableConfigCookie = false; - const getCookie = () => { - if (!disableConfigCookie && Object.keys(config.bilibili.cookies).length > 0) { + if (Object.keys(config.bilibili.cookies).length > 0) { // Update b_lsid in cookies for (const key of Object.keys(config.bilibili.cookies)) { const cookie = config.bilibili.cookies[key]; diff --git a/lib/routes/bilibili/check-cookie.ts b/lib/routes/bilibili/check-cookie.ts new file mode 100644 index 0000000000..97bd1ad3ea --- /dev/null +++ b/lib/routes/bilibili/check-cookie.ts @@ -0,0 +1,31 @@ +import { APIRoute } from '@/types'; +import cacheIn from './cache'; +import ofetch from '@/utils/ofetch'; + +export const apiRoute: APIRoute = { + path: '/check-cookie', + description: '检查 bilibili cookie 是否有效', + maintainers: ['DIYgod'], + handler, +}; + +async function handler() { + const cookie = await cacheIn.getCookie(); + + if (!cookie) { + return { + code: -1, + }; + } + + const response = await ofetch(`https://api.bilibili.com/x/web-interface/nav`, { + headers: { + Referer: `https://space.bilibili.com/1/`, + Cookie: cookie as string, + }, + }); + + return { + code: response.code === 0 && !!response.data.mid ? 0 : -1, + }; +}