Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route/xsijishe): update rank list parsing logic with puppeteer support #17519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 14 additions & 39 deletions lib/routes/xsijishe/rank.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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) => {
Expand All @@ -118,6 +90,9 @@ async function handler(ctx) {
})
)
);

await browser.close();

return {
title,
link: url,
Expand Down
19 changes: 19 additions & 0 deletions lib/routes/xsijishe/utils.ts
Original file line number Diff line number Diff line change
@@ -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 };
Loading