fix(route/xueqiu): get token from cookies (#15432)
* fix(route/xueqiu): get token from cookie * fix(route/xueqiu): update lib/routes/xueqiu/cookies.ts ---------
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import cache from '@/utils/cache';
|
||||
import { config } from '@/config';
|
||||
|
||||
export const parseToken = () =>
|
||||
cache.tryGet(
|
||||
'xueqiu:token',
|
||||
async () => {
|
||||
const res = await ofetch.raw(`https://xueqiu.com`);
|
||||
const cookieArray = res.headers.getSetCookie();
|
||||
return cookieArray.find((c) => c.startsWith('xq_a_token='));
|
||||
},
|
||||
config.cache.routeExpire,
|
||||
false
|
||||
);
|
||||
@@ -2,6 +2,7 @@ import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import queryString from 'query-string';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { parseToken } from '@/routes/xueqiu/cookies';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/favorite/:id',
|
||||
@@ -28,13 +29,7 @@ export const route: Route = {
|
||||
|
||||
async function handler(ctx) {
|
||||
const id = ctx.req.param('id');
|
||||
|
||||
const res1 = await got({
|
||||
method: 'get',
|
||||
url: 'https://xueqiu.com/',
|
||||
});
|
||||
const token = res1.headers['set-cookie'].find((s) => s.startsWith('xq_a_token=')).split(';')[0];
|
||||
|
||||
const token = await parseToken();
|
||||
const res2 = await got({
|
||||
method: 'get',
|
||||
url: 'https://xueqiu.com/favorites.json',
|
||||
|
||||
@@ -3,6 +3,7 @@ import got from '@/utils/got';
|
||||
import queryString from 'query-string';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
import { parseToken } from '@/routes/xueqiu/cookies';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/hots',
|
||||
@@ -29,12 +30,7 @@ export const route: Route = {
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const res1 = await got({
|
||||
method: 'get',
|
||||
url: 'https://xueqiu.com/',
|
||||
});
|
||||
const token = res1.headers['set-cookie'].find((s) => s.startsWith('xq_a_token=')).split(';')[0];
|
||||
|
||||
const token = await parseToken();
|
||||
const res2 = await got({
|
||||
method: 'get',
|
||||
url: 'https://xueqiu.com/statuses/hots.json',
|
||||
|
||||
@@ -4,6 +4,7 @@ import { load } from 'cheerio';
|
||||
import queryString from 'query-string';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
import { parseToken } from '@/routes/xueqiu/cookies';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/stock_info/:id/:type?',
|
||||
@@ -49,8 +50,8 @@ async function handler(ctx) {
|
||||
method: 'get',
|
||||
url: `https://xueqiu.com/S/${id}`,
|
||||
});
|
||||
const token = res1.headers['set-cookie'].find((s) => s.startsWith('xq_a_token=')).split(';')[0];
|
||||
|
||||
const token = await parseToken();
|
||||
const $ = load(res1.data); // 使用 cheerio 加载返回的 HTML
|
||||
const stock_name = $('.stock-name').text().split('(')[0];
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { parseToken } from '@/routes/xueqiu/cookies';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/today',
|
||||
@@ -35,13 +36,7 @@ async function handler(ctx) {
|
||||
const currentUrl = `${rootUrl}/today`;
|
||||
const apiUrl = `${rootUrl}/statuses/hot/listV2.json?since_id=-1&size=${size}`;
|
||||
|
||||
const firstResponse = await got({
|
||||
method: 'get',
|
||||
url: rootUrl,
|
||||
});
|
||||
|
||||
const token = firstResponse.headers['set-cookie'].join(',').match(/(xq_a_token=.*?;)/)[1];
|
||||
|
||||
const token = await parseToken();
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: apiUrl,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import queryString from 'query-string';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
import { parseToken } from '@/routes/xueqiu/cookies';
|
||||
|
||||
const rootUrl = 'https://xueqiu.com';
|
||||
|
||||
@@ -48,12 +48,7 @@ async function handler(ctx) {
|
||||
11: '交易',
|
||||
};
|
||||
|
||||
const res1 = await ofetch.raw(rootUrl, {
|
||||
method: 'get',
|
||||
});
|
||||
const cookieArray = res1.headers.getSetCookie();
|
||||
const token = cookieArray.find((c) => c.startsWith('xq_a_token='));
|
||||
|
||||
const token = await parseToken();
|
||||
const res2 = await got({
|
||||
method: 'get',
|
||||
url: `${rootUrl}/v4/statuses/user_timeline.json`,
|
||||
|
||||
Reference in New Issue
Block a user