-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(route/xueqiu): fix xueqiu user & stock comments
- Loading branch information
cztchoice
committed
Dec 13, 2024
1 parent
352cac9
commit ce805c2
Showing
3 changed files
with
48 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
import cache from '@/utils/cache'; | ||
import { config } from '@/config'; | ||
import puppeteer from '@/utils/puppeteer'; | ||
import { getCookies } from '@/utils/puppeteer-utils'; | ||
import { getCookies, setCookies } from '@/utils/puppeteer-utils'; | ||
|
||
export const parseToken = (link: string) => | ||
cache.tryGet( | ||
'xueqiu:token', | ||
async () => { | ||
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(); | ||
}); | ||
await page.goto(link, { | ||
waitUntil: 'domcontentloaded', | ||
}); | ||
const page = await getPage(link, null); | ||
await page.evaluate(() => document.documentElement.innerHTML); | ||
const cookies = await getCookies(page); | ||
|
||
return cookies; | ||
}, | ||
config.cache.routeExpire, | ||
false | ||
); | ||
|
||
export const getPage = async (url: string, cookie: string | Record<string, any> | null = null) => { | ||
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(); | ||
}); | ||
|
||
if (cookie) { | ||
await setCookies(page, cookie, 'xueqiu.com'); | ||
} | ||
return page; | ||
}; | ||
|
||
export const getJson = async (url: string, cookie: string | Record<string, any> | null = null) => { | ||
const page = await getPage(url, cookie); | ||
|
||
const data = await page.goto(url, { | ||
waitUntil: 'domcontentloaded', | ||
}); | ||
const res = await data?.json(); | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters