From 4ffe081eb29512f269b2cb2d5a6c0bb702286e34 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 5 Nov 2024 00:27:55 +0800 Subject: [PATCH] fix(bilibili): video reply (#17440) * fix(bilibili): video reply * fix * update --- lib/routes/bilibili/reply.ts | 2 ++ lib/utils/got.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/routes/bilibili/reply.ts b/lib/routes/bilibili/reply.ts index cb63d4cc6..25a69415c 100644 --- a/lib/routes/bilibili/reply.ts +++ b/lib/routes/bilibili/reply.ts @@ -33,11 +33,13 @@ async function handler(ctx) { } const link = `https://www.bilibili.com/video/${bvid || `av${aid}`}`; + const cookie = await cache.getCookie(); const response = await got({ method: 'get', url: `https://api.bilibili.com/x/v2/reply?type=1&oid=${aid}&sort=0`, headers: { Referer: link, + Cookie: cookie, }, }); diff --git a/lib/utils/got.ts b/lib/utils/got.ts index 1f623d9d6..cf03e74b2 100644 --- a/lib/utils/got.ts +++ b/lib/utils/got.ts @@ -1,6 +1,10 @@ import { destr } from 'destr'; import ofetch from '@/utils/ofetch'; +function isObject(o) { + return o !== null && typeof o === 'object' && Array.isArray(o) === false; +} + const getFakeGot = (defaultOptions?: any) => { const fakeGot = (request, options?: any) => { if (!(typeof request === 'string' || request instanceof Request) && request.url) { @@ -35,7 +39,15 @@ const getFakeGot = (defaultOptions?: any) => { delete options.form; } if (options?.searchParams) { - request += '?' + new URLSearchParams(options.searchParams).toString(); + request += + '?' + + new URLSearchParams( + isObject(options.searchParams) + ? Object.entries(options.searchParams) + .filter(([, value]) => value !== undefined) + .map(([key, value]) => [key, String(value)]) + : options.searchParams + ).toString(); delete options.searchParams; }