Skip to content

Commit

Permalink
fix(route/xueqiu): get token from cookies (DIYgod#15432)
Browse files Browse the repository at this point in the history
* fix(route/xueqiu): get token from cookie

* fix(route/xueqiu): update lib/routes/xueqiu/cookies.ts

---------
  • Loading branch information
zhoukuncheng authored May 1, 2024
1 parent d4a2633 commit e030be0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
15 changes: 15 additions & 0 deletions lib/routes/xueqiu/cookies.ts
Original file line number Diff line number Diff line change
@@ -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
);
9 changes: 2 additions & 7 deletions lib/routes/xueqiu/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
8 changes: 2 additions & 6 deletions lib/routes/xueqiu/hots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/xueqiu/stock-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
Expand Down Expand Up @@ -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];

Expand Down
9 changes: 2 additions & 7 deletions lib/routes/xueqiu/today.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 2 additions & 7 deletions lib/routes/xueqiu/user.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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`,
Expand Down

0 comments on commit e030be0

Please sign in to comment.