From d7c23dcff5c906052544bb68c5ed2185f8304c29 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 7 Apr 2024 22:15:17 +0800 Subject: [PATCH] fix(bilibili): vsearch, close #15105 --- lib/routes/bilibili/vsearch.ts | 57 ++++++++++++++++------------------ 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/lib/routes/bilibili/vsearch.ts b/lib/routes/bilibili/vsearch.ts index 76fcba44b8b3bf..63b601cdceb6fd 100644 --- a/lib/routes/bilibili/vsearch.ts +++ b/lib/routes/bilibili/vsearch.ts @@ -1,12 +1,9 @@ import { Route } from '@/types'; -import cache from '@/utils/cache'; -import { config } from '@/config'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import utils from './utils'; -import { CookieJar } from 'tough-cookie'; -const cookieJar = new CookieJar(); import { queryToBoolean } from '@/utils/readable-social'; +import cacheIn from './cache'; export const route: Route = { path: '/vsearch/:kw/:order?/:disableEmbed?/:tid?', @@ -14,7 +11,17 @@ export const route: Route = { example: '/bilibili/vsearch/RSSHub', parameters: { kw: '检索关键字', order: '排序方式, 综合:totalrank 最多点击:click 最新发布:pubdate(缺省) 最多弹幕:dm 最多收藏:stow', disableEmbed: '默认为开启内嵌视频, 任意值为关闭', tid: '分区 id' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'BILIBILI_COOKIE_*', + optional: true, + description: `如果没有此配置,那么必须开启 puppeteer 支持;BILIBILI_COOKIE_{uid}: 用于用户关注动态系列路由,对应 uid 的 b 站用户登录后的 Cookie 值,\`{uid}\` 替换为 uid,如 \`BILIBILI_COOKIE_2267573\`,获取方式: +1. 打开 [https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=0&type=8](https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=0&type=8) +2. 打开控制台,切换到 Network 面板,刷新 +3. 点击 dynamic_new 请求,找到 Cookie +4. 视频和专栏,UP 主粉丝及关注只要求 \`SESSDATA\` 字段,动态需复制整段 Cookie`, + }, + ], requirePuppeteer: false, antiCrawler: false, supportBT: false, @@ -22,7 +29,7 @@ export const route: Route = { supportScihub: false, }, name: '视频搜索', - maintainers: ['Symty'], + maintainers: ['Symty', 'DIYgod'], handler, description: `分区 id 的取值请参考下表: @@ -37,32 +44,22 @@ async function handler(ctx) { const disableEmbed = queryToBoolean(ctx.req.param('disableEmbed')); const kw_url = encodeURIComponent(kw); const tids = ctx.req.param('tid') ?? 0; + const cookie = await cacheIn.getCookie(); - const data = await cache.tryGet( - `bilibili:vsearch:${tids}:${kw}:${order}`, - async () => { - await got('https://passport.bilibili.com/login', { - cookieJar, - }); - - const response = await got('https://api.bilibili.com/x/web-interface/search/type', { - headers: { - Referer: `https://search.bilibili.com/all?keyword=${kw_url}`, - }, - cookieJar, - searchParams: { - search_type: 'video', - highlight: 1, - keyword: kw, - order, - tids, - }, - }); - return response.data.data.result; + const response = await got('https://api.bilibili.com/x/web-interface/search/type', { + headers: { + Referer: `https://search.bilibili.com/all?keyword=${kw_url}`, + Cookie: cookie, + }, + searchParams: { + search_type: 'video', + highlight: 1, + keyword: kw, + order, + tids, }, - config.cache.routeExpire, - false - ); + }); + const data = response.data.data.result; return { title: `${kw} - bilibili`,