From 6b7f3006eef2a21d7e927e9550b8c8ef6889b8d5 Mon Sep 17 00:00:00 2001 From: Zhitao Chen Date: Wed, 22 May 2024 10:57:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20xueqiu=20user=5Fstock=20=E9=9B=AA?= =?UTF-8?q?=E7=90=83=E7=94=A8=E6=88=B7=E8=87=AA=E9=80=89=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=90=8E=E7=9A=84cookie=E5=80=BC=EF=BC=8C=E5=B9=B6=E9=99=90?= =?UTF-8?q?=E5=88=B6=E8=87=AA=E5=BB=BA=E4=BD=BF=E7=94=A8=20(#15653)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: cztchoice --- lib/routes/xueqiu/user-stock.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/routes/xueqiu/user-stock.ts b/lib/routes/xueqiu/user-stock.ts index 02e6490b3..f3039beea 100644 --- a/lib/routes/xueqiu/user-stock.ts +++ b/lib/routes/xueqiu/user-stock.ts @@ -1,5 +1,7 @@ import { Route } from '@/types'; import ofetch from '@/utils/ofetch'; +import { config } from '@/config'; +import ConfigNotFoundError from '@/errors/types/config-not-found'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { @@ -8,7 +10,12 @@ export const route: Route = { example: '/xueqiu/user_stock/1247347556', parameters: { id: '用户 id, 可在用户主页 URL 中找到' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'XUEQIU_COOKIES', + description: '', + }, + ], requirePuppeteer: false, antiCrawler: false, supportBT: false, @@ -23,22 +30,24 @@ export const route: Route = { name: '用户自选动态', maintainers: ['hillerliao'], handler, + description: `:::warning + 用户自选动态需要登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。 +:::`, }; async function handler(ctx) { - const id = ctx.req.param('id'); + const cookie = config.xueqiu.cookies; + if (cookie === undefined) { + throw new ConfigNotFoundError('缺少雪球用户登录后的 Cookie 值'); + } - const { headers } = await ofetch.raw('https://xueqiu.com/'); - const token = headers - ?.getSetCookie() - .find((s) => s.startsWith('xq_a_token=')) - ?.split(';')[0] as string; + const id = ctx.req.param('id'); const { data: { stocks: data }, } = await ofetch(`https://stock.xueqiu.com/v5/stock/portfolio/stock/list.json?category=1&size=1000&uid=${id}`, { headers: { - Cookie: token, + Cookie: cookie, Referer: `https://xueqiu.com/u/${id}`, }, }); @@ -50,7 +59,7 @@ async function handler(ctx) { user_id: id, }, headers: { - Cookie: token, + Cookie: cookie, Referer: `https://xueqiu.com/u/${id}`, }, }); @@ -61,7 +70,7 @@ async function handler(ctx) { description: `@${screen_name} 的雪球自选动态`, item: data.map((item) => ({ title: `@${screen_name} 关注了股票 ${item.name}`, - description: `@${screen_name} 在${parseDate(item.created).toLocaleString()} 关注了 ${item.name}(${item.exchange}:${item.symbol})。`, + description: `@${screen_name} 在${parseDate(item.created).toLocaleString()} 关注了 ${item.marketplace} ${item.name}(${item.exchange}:${item.symbol})。`, pubDate: parseDate(item.created), link: `https://xueqiu.com/s/${item.symbol}`, })),