diff --git a/lib/routes/xsijishe/rank.ts b/lib/routes/xsijishe/rank.ts index 6b20aa90d70821..b6672b40e445a5 100644 --- a/lib/routes/xsijishe/rank.ts +++ b/lib/routes/xsijishe/rank.ts @@ -1,9 +1,10 @@ import InvalidParameterError from '@/errors/types/invalid-parameter'; import { Route } from '@/types'; import cache from '@/utils/cache'; -import got from '@/utils/got'; import { load } from 'cheerio'; -import { config } from '@/config'; +import { puppeteerGet } from './utils'; +import puppeteer from '@/utils/puppeteer'; + const baseUrl = 'https://xsijishe.com'; export const route: Route = { @@ -20,18 +21,8 @@ export const route: Route = { }, }, features: { - requireConfig: [ - { - name: 'XSIJISHE_COOKIE', - description: '', - }, - { - name: 'XSIJISHE_USER_AGENT', - description: '', - }, - ], - requirePuppeteer: false, - antiCrawler: false, + requirePuppeteer: true, + antiCrawler: true, supportBT: false, supportPodcast: false, supportScihub: false, @@ -42,6 +33,8 @@ export const route: Route = { }; async function handler(ctx) { + const browser = await puppeteer(); + const rankType = ctx.req.param('type'); let title; let index; // 用于选择第几个 li @@ -57,28 +50,9 @@ async function handler(ctx) { } const url = `${baseUrl}/portal.php`; - const headers = { - 'Accept-Encoding': 'gzip, deflate, br', - 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', - Cookie: config.xsijishe.cookie, - 'User-Agent': config.xsijishe.user_agent, - }; - - const resp = await got(url, { - headers, - }); - - const redirectMatch = resp.data.match(/window\.location\.href\s*=\s*"([^"]+)"/); - if (redirectMatch) { - const redirectUrl = `${baseUrl}${redirectMatch[1]}`; - // 使用提取到的地址重新请求 - const realResp = await got(redirectUrl, { - headers, - }); - resp.data = realResp.data; - } + const data = await cache.tryGet(url, () => puppeteerGet(url, browser)); + const $ = load(data); - const $ = load(resp.data); // 根据 index 选择对应的 li,然后获取其中的 dd 元素 let items = $('.nex_recon_lists ul li') .eq(index) @@ -97,10 +71,8 @@ async function handler(ctx) { items = await Promise.all( items.map((item) => cache.tryGet(item.link, async () => { - const resp = await got(item.link, { - headers, - }); - const $ = load(resp.data); + const data = await puppeteerGet(item.link, browser); + const $ = load(data); const firstViewBox = $('.t_f').first(); firstViewBox.find('img').each((_, img) => { @@ -118,6 +90,9 @@ async function handler(ctx) { }) ) ); + + await browser.close(); + return { title, link: url, diff --git a/lib/routes/xsijishe/utils.ts b/lib/routes/xsijishe/utils.ts new file mode 100644 index 00000000000000..a8ee8431a96914 --- /dev/null +++ b/lib/routes/xsijishe/utils.ts @@ -0,0 +1,19 @@ +const puppeteerGet = async (url, browser) => { + const isIndex = url.endsWith('/portal.php'); + const page = await browser.newPage(); + const expectResourceTypes = new Set(['document', 'script']); + await page.setRequestInterception(true); + page.on('request', (request) => { + expectResourceTypes.has(request.resourceType()) ? request.continue() : request.abort(); + }); + await page.goto(url, { + waitUntil: 'domcontentloaded', + }); + + await (isIndex ? page.waitForSelector('.nex_recon_lists') : page.waitForSelector('.t_f')); + const html = await page.evaluate(() => document.documentElement.innerHTML); + await page.close(); + return html; +}; + +export { puppeteerGet };