fix(route/sehuatang): get safe id (#19526)

* revert: "fix(route): sehuatang 抓取原帖失败问题 (#15993)"

This reverts commit 85fb1e8c17.

* revert: "fix(route): sehuatang 403报错 (#15915)"

This reverts commit 910f6b7c2e.

* fix: get safe id
This commit is contained in:
Tony
2025-07-03 01:08:22 +08:00
committed by GitHub
parent 6e202ac335
commit f7fd8074f4
+27 -32
View File
@@ -1,10 +1,10 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import logger from '@/utils/logger';
import puppeteer from '@/utils/puppeteer';
import { config } from '@/config';
const host = 'https://www.sehuatang.net/';
@@ -42,9 +42,6 @@ export const route: Route = {
path: ['/bt/:subforumid?', '/picture/:subforumid', '/:subforumid?/:type?', '/:subforumid?', ''],
name: 'Forum',
maintainers: ['qiwihui', 'junfengP', 'nczitzk'],
features: {
requirePuppeteer: true,
},
handler,
description: `**原创 BT 电影**
@@ -59,29 +56,34 @@ export const route: Route = {
| yczp | ztzp | hrjp | yzxa | omxa | ktdm | ttxz |`,
};
const getSafeId = () =>
cache.tryGet(
'sehuatang:safeid',
async () => {
const response = await ofetch(host);
const $ = load(response);
const safeId = $('script:contains("safeid")')
.text()
.match(/safeid\s*=\s*'(.+)';/)?.[1];
return safeId;
},
config.cache.routeExpire,
false
);
async function handler(ctx) {
const subformName = ctx.req.param('subforumid') ?? 'gqzwzm';
const subformId = subformName in forumIdMaps ? forumIdMaps[subformName] : subformName;
const type = ctx.req.param('type');
const typefilter = type ? `&filter=typeid&typeid=${type}` : '';
const link = `${host}forum.php?mod=forumdisplay&orderby=dateline&fid=${subformId}${typefilter}`;
const headers = {
Cookie: `_safe=${await getSafeId()};`,
};
const browser = await puppeteer();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort();
const response = await ofetch(link, {
headers,
});
logger.http(`Requesting ${link}`);
await page.goto(link, {
waitUntil: 'domcontentloaded',
});
await page.waitForSelector('a.enter-btn', { visible: true });
await Promise.all([page.click('a.enter-btn'), page.waitForNavigation({ waitUntil: 'domcontentloaded' })]);
const response = await page.content();
await page.close();
const $ = load(response);
const list = $('#threadlisttableid tbody[id^=normalthread]')
@@ -101,20 +103,12 @@ async function handler(ctx) {
const out = await Promise.all(
list.map((info) =>
cache.tryGet(info.link, async () => {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort();
const response = await ofetch(info.link, {
headers,
});
await page.goto(info.link, {
// 指定页面等待载入的时间
waitUntil: 'domcontentloaded',
});
const response = await page.content();
await page.close();
const $ = load(response);
const postMessage = $("td[id^='postmessage']").slice(0, 1);
const postMessage = $('div[id^="postmessage"]').slice(0, 1);
const images = $(postMessage).find('img');
for (const image of images) {
const file = $(image).attr('file');
@@ -151,11 +145,12 @@ async function handler(ctx) {
info.enclosure_url = enclosureUrl;
info.enclosure_type = isMag ? 'application/x-bittorrent' : 'application/octet-stream';
}
return info;
})
)
);
await browser.close();
return {
title: `色花堂 - ${$('#pt > div:nth-child(1) > a:last-child').text()}`,
link,