From 20db6261d13a3c44e8eb3b5c75beb5c4e16f684d Mon Sep 17 00:00:00 2001 From: Gnosnay Date: Fri, 22 Nov 2024 11:15:00 +0800 Subject: [PATCH] fix(route/xueqiu): fix getting cookie logic (#17675) * fix(route/xueqiu): fix getting cookie logic * fix(route/xueqiu): fix according to review --- lib/routes/xueqiu/cookies.ts | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/routes/xueqiu/cookies.ts b/lib/routes/xueqiu/cookies.ts index a5b8d940c5e994..7e11d5218a3fa9 100644 --- a/lib/routes/xueqiu/cookies.ts +++ b/lib/routes/xueqiu/cookies.ts @@ -1,29 +1,24 @@ -import ofetch from '@/utils/ofetch'; import cache from '@/utils/cache'; import { config } from '@/config'; -import { getAcwScV2ByArg1 } from '@/routes/5eplay/utils'; +import puppeteer from '@/utils/puppeteer'; +import { getCookies } from '@/utils/puppeteer-utils'; export const parseToken = (link: string) => cache.tryGet( 'xueqiu:token', async () => { - const r = await ofetch(link); - - let acw_sc__v2 = ''; - const matches = r.match(/var arg1='(.*?)';/); - if (matches) { - acw_sc__v2 = getAcwScV2ByArg1(matches[1]); - } - const acw_sc__v2_cookie = `acw_sc__v2=${acw_sc__v2}`; - const res = await ofetch.raw(link, { - headers: { - Cookie: acw_sc__v2_cookie, - }, + const browser = await puppeteer({ stealth: true }); + const page = await browser.newPage(); + await page.setRequestInterception(true); + page.on('request', (request) => { + request.resourceType() === 'document' ? request.continue() : request.abort(); }); - const cookieArray = res.headers.getSetCookie(); - const xq_a_token_cookie = cookieArray.find((c) => c.startsWith('xq_a_token=')); - - return `${acw_sc__v2_cookie}; ${xq_a_token_cookie}`; + await page.goto(link, { + waitUntil: 'domcontentloaded', + }); + await page.evaluate(() => document.documentElement.innerHTML); + const cookies = await getCookies(page); + return cookies; }, config.cache.routeExpire, false