refactor: parse(content,pageKey)替代URL路由,新增resolvePageKey
This commit is contained in:
@@ -27,6 +27,7 @@ public class AdapterController {
|
|||||||
private final AccessResultMapper accessResultMapper;
|
private final AccessResultMapper accessResultMapper;
|
||||||
private final CachedStorageService storageService;
|
private final CachedStorageService storageService;
|
||||||
private final com.par.core.mapper.SiteMapper siteMapper;
|
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()); }
|
catch (Exception e) { return ApiResponse.error(500, "读取内容失败: " + e.getMessage()); }
|
||||||
if (content == null) return ApiResponse.error(404, "内容为空");
|
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) {
|
if (content == null) {
|
||||||
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
|
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
|
||||||
} else {
|
} 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) {
|
} catch (Exception e) {
|
||||||
@@ -156,4 +160,29 @@ public class AdapterController {
|
|||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ public class SitePagesController {
|
|||||||
|
|
||||||
// 3. 用 Groovy 脚本解析内容
|
// 3. 用 Groovy 脚本解析内容
|
||||||
try {
|
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"));
|
boolean matched = Boolean.TRUE.equals(parsed.get("matched"));
|
||||||
result.put("parsed", parsed);
|
result.put("parsed", parsed);
|
||||||
result.put("matched", matched);
|
result.put("matched", matched);
|
||||||
|
|||||||
@@ -127,19 +127,29 @@ public class GroovyScriptEngine {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Map<String, Object> execute(String groovyScript, String content, String url) {
|
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<>();
|
Map<String, Object> result = new LinkedHashMap<>();
|
||||||
result.put("engine", "groovy");
|
result.put("engine", "groovy");
|
||||||
try {
|
try {
|
||||||
Binding binding = new Binding();
|
Binding binding = new Binding();
|
||||||
binding.setVariable("content", content);
|
binding.setVariable("content", content);
|
||||||
binding.setVariable("url", url);
|
binding.setVariable("url", url);
|
||||||
|
binding.setVariable("pageKey", pageKey != null ? pageKey : "");
|
||||||
binding.setVariable("jsoup", new JsoupHelper());
|
binding.setVariable("jsoup", new JsoupHelper());
|
||||||
binding.setVariable("json", objectMapper);
|
binding.setVariable("json", objectMapper);
|
||||||
binding.setVariable("utils", new ScriptUtils());
|
binding.setVariable("utils", new ScriptUtils());
|
||||||
binding.setVariable("http", HttpUtils.INSTANCE);
|
binding.setVariable("http", HttpUtils.INSTANCE);
|
||||||
|
|
||||||
GroovyShell shell = new GroovyShell(classLoader, binding);
|
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);
|
Object scriptResult = shell.evaluate(fullScript);
|
||||||
|
|
||||||
return buildResult(result, scriptResult);
|
return buildResult(result, scriptResult);
|
||||||
|
|||||||
@@ -1,18 +1,25 @@
|
|||||||
// NexusPHP 站点默认脚本
|
// NexusPHP 站点默认脚本
|
||||||
// 适用: PThome / HDSky / HDFans / OurBits 等
|
// 适用: PThome / HDSky / HDFans / OurBits 等
|
||||||
|
|
||||||
Map parse(String content, String url) {
|
Map parse(String content, String pageKey) {
|
||||||
def doc = Jsoup.parse(content)
|
def doc = Jsoup.parse(content)
|
||||||
|
switch (pageKey) {
|
||||||
if (url.contains('/torrents.php')) return parseSearch(doc)
|
case 'search': return parseSearch(doc)
|
||||||
if (url.contains('/details.php')) return parseDetail(doc)
|
case 'detail': return parseDetail(doc)
|
||||||
if (url.contains('/attendance.php')) return parseSign(doc)
|
case 'signIn': return parseSign(doc)
|
||||||
if (url.contains('/download.php')) return parseDownload(content)
|
case 'download': return parseDownload(content)
|
||||||
if (url.contains('/index.php') && !url.contains('?')) return parseUserInfo(doc)
|
case 'sync': return parseUserInfo(doc)
|
||||||
if (url.contains('/usercp.php')) return parseUserPanel(doc)
|
case 'rss': return parseRss(content)
|
||||||
if (url.contains('/torrentrss.php') || url.contains('/rss')) return parseRss(content)
|
default:
|
||||||
|
// fallback: pageKey 未知时用 URL 兜底
|
||||||
return [pageName: '未知页面', message: "未配置: ${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) {
|
private Map parseDownload(String content) {
|
||||||
|
|||||||
Reference in New Issue
Block a user