refactor: parse(content,pageKey)替代URL路由,新增resolvePageKey

This commit is contained in:
mediabot-pt
2026-07-09 15:53:56 +08:00
parent b0be61669c
commit e0f3772275
4 changed files with 61 additions and 15 deletions

View File

@@ -27,6 +27,7 @@ public class AdapterController {
private final AccessResultMapper accessResultMapper;
private final CachedStorageService storageService;
private final com.par.core.mapper.SiteMapper siteMapper;
private final com.par.core.service.SitePagesService sitePagesService;
/**
* 用指定适配配置解析指定访问结果
@@ -61,7 +62,8 @@ public class AdapterController {
catch (Exception e) { return ApiResponse.error(500, "读取内容失败: " + e.getMessage()); }
if (content == null) return ApiResponse.error(404, "内容为空");
return ApiResponse.success(groovyEngine.execute(script, content, accessResult.getUrl()));
String pageKey = resolvePageKey(siteConfig.getSiteId(), accessResult.getUrl());
return ApiResponse.success(groovyEngine.execute(script, content, accessResult.getUrl(), pageKey));
}
/**
@@ -135,7 +137,9 @@ public class AdapterController {
if (content == null) {
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
} else {
item.put("parsed", groovyEngine.execute(script, content, url));
String pageKey = resolvePageKey(siteConfig.getSiteId(), url);
item.put("pageKey", pageKey);
item.put("parsed", groovyEngine.execute(script, content, url, pageKey));
}
}
} catch (Exception e) {
@@ -156,4 +160,29 @@ public class AdapterController {
}
return null;
}
/** 根据页面配置的 URL 模板反查出 pageKey(如 sync/signIn/search) */
@SuppressWarnings("unchecked")
private String resolvePageKey(String siteId, String url) {
if (url == null || url.isBlank()) return null;
try {
Map<String, Object> pages = sitePagesService.getPages(siteId);
Object entries = pages != null ? pages.get("entries") : null;
if (entries instanceof List) {
String path = url.replaceAll("^https?://[^/]+", "").replaceAll("\\?.*", "");
for (Object e : (List<?>) entries) {
if (e instanceof Map) {
Map<String, Object> entry = (Map<String, Object>) e;
String entryUrl = (String) entry.get("url");
if (entryUrl == null) continue;
String entryPath = entryUrl.replaceAll("\\?.*", "").replaceAll("\\{.*?\\}", "");
if (path.trim().equals(entryPath.trim()) || path.contains(entryPath)) {
return (String) entry.get("pageKey");
}
}
}
}
} catch (Exception ignored) {}
return null;
}
}

View File

@@ -156,7 +156,7 @@ public class SitePagesController {
// 3. 用 Groovy 脚本解析内容
try {
Map<String, Object> parsed = groovyEngine.execute(script, content, url);
Map<String, Object> parsed = groovyEngine.execute(script, content, url, pageKey);
boolean matched = Boolean.TRUE.equals(parsed.get("matched"));
result.put("parsed", parsed);
result.put("matched", matched);

View File

@@ -127,19 +127,29 @@ public class GroovyScriptEngine {
@SuppressWarnings("unchecked")
public Map<String, Object> execute(String groovyScript, String content, String url) {
return execute(groovyScript, content, url, null);
}
@SuppressWarnings("unchecked")
public Map<String, Object> execute(String groovyScript, String content, String url, String pageKey) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("engine", "groovy");
try {
Binding binding = new Binding();
binding.setVariable("content", content);
binding.setVariable("url", url);
binding.setVariable("pageKey", pageKey != null ? pageKey : "");
binding.setVariable("jsoup", new JsoupHelper());
binding.setVariable("json", objectMapper);
binding.setVariable("utils", new ScriptUtils());
binding.setVariable("http", HttpUtils.INSTANCE);
GroovyShell shell = new GroovyShell(classLoader, binding);
String fullScript = SCRIPT_PREFIX + groovyScript + "\n// PAR 调用入口\nreturn parse(content, url)";
// 优先用 pageKey 路由,无则回退 url 路由
String call = pageKey != null && !pageKey.isEmpty()
? "return parse(content, pageKey)"
: "return parse(content, url)";
String fullScript = SCRIPT_PREFIX + groovyScript + "\n// PAR 调用入口\n" + call;
Object scriptResult = shell.evaluate(fullScript);
return buildResult(result, scriptResult);

View File

@@ -1,18 +1,25 @@
// NexusPHP 站点默认脚本
// 适用: PThome / HDSky / HDFans / OurBits 等
Map parse(String content, String url) {
Map parse(String content, String pageKey) {
def doc = Jsoup.parse(content)
if (url.contains('/torrents.php')) return parseSearch(doc)
if (url.contains('/details.php')) return parseDetail(doc)
if (url.contains('/attendance.php')) return parseSign(doc)
if (url.contains('/download.php')) return parseDownload(content)
if (url.contains('/index.php') && !url.contains('?')) return parseUserInfo(doc)
if (url.contains('/usercp.php')) return parseUserPanel(doc)
if (url.contains('/torrentrss.php') || url.contains('/rss')) return parseRss(content)
return [pageName: '未知页面', message: "未配置: ${url}"]
switch (pageKey) {
case 'search': return parseSearch(doc)
case 'detail': return parseDetail(doc)
case 'signIn': return parseSign(doc)
case 'download': return parseDownload(content)
case 'sync': return parseUserInfo(doc)
case 'rss': return parseRss(content)
default:
// fallback: pageKey 未知时用 URL 兜底
String u = url ?: ''
if (u.contains('/attendance.php')) return parseSign(doc)
if (u.contains('/download.php')) return parseDownload(content)
if (u.contains('/index.php') && !u.contains('?')) return parseUserInfo(doc)
if (u.contains('/usercp.php')) return parseUserPanel(doc)
if (u.contains('/torrentrss.php') || u.contains('/rss')) return parseRss(content)
return [pageName: '未知页面', message: "未配置: pageKey=${pageKey}"]
}
}
private Map parseDownload(String content) {