fix(route): xueqiu user_stock 雪球用户自选动态修改为使用登录后的cookie值,并限制自建使用 (#15653)

Co-authored-by: cztchoice <cztchoice+wsl@gmail.com>
This commit is contained in:
Zhitao Chen 2024-05-22 10:57:48 +08:00 committed by GitHub
parent 549993931e
commit 6b7f3006ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 19 additions and 10 deletions

View File

@ -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}`,
})),