fix: 改进VikaACG搜索解析并调整2dfan调试

*   修复VikaACG平台HTML解析正则表达式中的结束标签错误。
*   简化VikaACG API响应的处理流程,直接使用文本内容。
*   调整2dfan平台日志输出,在JSON解析前显示原始响应文本。
This commit is contained in:
Jurangren
2025-08-22 01:30:35 +08:00
parent 1a9db6a536
commit 6a59d03ac0
2 changed files with 30 additions and 47 deletions

View File

@@ -2,11 +2,7 @@ import { fetchClient } from "../../utils/httpClient";
import type { Platform, PlatformSearchResult, SearchResultItem } from "../../types";
const API_URL = "https://www.vikacg.com/wp-json/b2/v1/getPostList";
// The Python code suggests the response text itself might be a JSON string
// that contains escaped HTML. Let's try to parse it as JSON first.
// The regex is applied to the *unescaped* string.
const REGEX = /<h2><a target="_blank" href="(?<URL>.*?)">(?<NAME>.*?)<"/gs;
const REGEX = /<h2><a target="_blank" href="(?<URL>.*?)">(?<NAME>.*?)<\/a>/gs;
async function searchVikaACG(game: string): Promise<PlatformSearchResult> {
const searchResult: PlatformSearchResult = {
@@ -16,57 +12,44 @@ async function searchVikaACG(game: string): Promise<PlatformSearchResult> {
};
try {
const payload = {
paged: 1,
post_paged: 1,
post_count: 1000, // Corresponds to MAX_RESULTS
post_type: "post-1",
post_cat: [6],
post_order: "modified",
post_meta: [
"user", "date", "des", "cats", "like", "comment", "views", "video", "download", "hide",
],
metas: {},
search: game,
};
const response = await fetchClient(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
body: JSON.stringify({
paged: 1,
post_paged: 1,
post_count: 1000, // Hardcoded limit, larger values may cause timeouts
post_type: "post-1",
post_cat: [6],
post_order: "modified",
post_meta: [
"user",
"date",
"des",
"cats",
"like",
"comment",
"views",
"video",
"download",
"hide",
],
metas: {},
search: game,
}),
});
if (!response.ok) {
throw new Error(`资源平台 SearchAPI 响应异常状态码 ${response.status}`);
}
const rawText = await response.text();
let htmlContent: string;
// The response is a JSON-encoded string containing HTML.
// .json() will parse the JSON and unescape the string content.
const html: string = await response.text();
try {
// Attempt to parse as JSON first. If it's a JSON string containing HTML,
// JSON.parse will handle standard escapes like \uXXXX.
const parsedJson = JSON.parse(rawText);
// Assuming the HTML content is directly the value of the JSON, or a specific field.
// The Python code implies the entire response text, after unescaping, is the HTML.
// So, if parsedJson is a string, use it. Otherwise, stringify it.
htmlContent = typeof parsedJson === 'string' ? parsedJson : JSON.stringify(parsedJson);
} catch (jsonError) {
// If JSON.parse fails, it might be due to non-standard Python escapes like \\/ or \\\\
// Attempt a simple unescape for these specific cases.
// Note: This is a simplified unescape and might not cover all Python's unicode_escape nuances.
const unescapedText = rawText.replace(/\\(.)/g, '$1'); // Replaces \\/ with / and \\\\ with \
try {
htmlContent = JSON.parse(unescapedText); // Try parsing as JSON again
} catch (finalError) {
// If still fails, assume it's raw HTML that just needs basic unescaping
htmlContent = unescapedText;
}
}
const matches = htmlContent.matchAll(REGEX);
const matches = html.matchAll(REGEX);
const items: SearchResultItem[] = [];
for (const match of matches) {

View File

@@ -25,11 +25,11 @@ async function searchTWOdfan(game: string): Promise<PlatformSearchResult> {
throw new Error(`资源平台 SearchAPI 响应异常状态码 ${response.status}`);
}
console.log("2dfan API HTML Response:", response.text());
const data = await response.json() as TwoDFanResponse;
const html = data.subjects;
console.log("2dfan API HTML Response:", html);
const matches = html.matchAll(REGEX);
const items: SearchResultItem[] = [];