第110批:联网兜底接入中文Minecraft Wiki(zh.minecraft.wiki)——CF放行Java UA拦浏览器UA;mcWiki搜索+文章抓取双链路;encodeWikiUrl修复首段斜杠丢失;stripTags清wiki残留
Auto Release / build (push) Successful in 2m21s
Auto Release / build (push) Successful in 2m21s
This commit is contained in:
@@ -42,8 +42,21 @@ public class WebSearchTool {
|
||||
private static final String DEEPSEEK_SEARCH_URL = "https://api.deepseek.com/v1/tools/search";
|
||||
private static final String DEEPSEEK_FETCH_URL = "https://api.deepseek.com/v1/tools/fetch";
|
||||
|
||||
// ★ 第109批:公开搜索兜底后端(按实测质量排序:好搜摘要最全 → Bing → 搜狗)
|
||||
private static final String[] FALLBACK_BACKENDS = {"so", "bing", "sogou"};
|
||||
// ★ 第110批:中文 Minecraft Wiki(zh.minecraft.wiki)——MC 话题权威源,质量最高。
|
||||
// ⚠️ Cloudflare 对「浏览器 UA」发 JS 挑战(403),对「Java 默认 UA」放行(200)——
|
||||
// 实测 curl 默认 UA / Java/17 / Java/21 / Apache-HttpClient 均 200,Chrome UA 403。
|
||||
// 故 wiki 请求必须用 WIKI_UA,绝不能复用 USER_AGENT。
|
||||
private static final String WIKI_UA = "Java/21.0.1";
|
||||
private static final String WIKI_SEARCH_URL = "https://zh.minecraft.wiki/?search=";
|
||||
private static final String WIKI_SEARCH_PARAMS =
|
||||
"&title=Special%3ASearch&profile=advanced&fulltext=1"
|
||||
+ "&ns0=1&ns9994=1&ns9996=1&ns9998=1&ns10000=1"
|
||||
+ "&ns10002=1&ns10004=1&ns10006=1&ns10010=1&ns10014=1";
|
||||
private static final String WIKI_ARTICLE_URL = "https://zh.minecraft.wiki/w/";
|
||||
|
||||
// ★ 第109批:公开搜索兜底后端(按实测质量排序)
|
||||
// 第110批:mcWiki 排最前——MC 话题命中时质量最高(官方 wiki 正文摘要)
|
||||
private static final String[] FALLBACK_BACKENDS = {"wiki", "so", "bing", "sogou"};
|
||||
|
||||
/**
|
||||
* 联网搜索,返回摘要结果文本。
|
||||
@@ -75,6 +88,7 @@ public class WebSearchTool {
|
||||
/** 公开搜索后端路由 */
|
||||
private static String publicSearch(String backend, String query) {
|
||||
switch (backend) {
|
||||
case "wiki": return mcWikiSearch(query);
|
||||
case "so": return soSearch(query);
|
||||
case "bing": return bingSearch(query);
|
||||
case "sogou": return sogouSearch(query);
|
||||
@@ -170,7 +184,18 @@ public class WebSearchTool {
|
||||
* Response: {"title":"...","content":"Markdown..."}
|
||||
*/
|
||||
public static String fetchUrl(String url, String apiKey) {
|
||||
if (url == null || url.isEmpty() || apiKey == null || apiKey.isEmpty()) return null;
|
||||
if (url == null || url.isEmpty()) return null;
|
||||
// ★ 第110批:wiki 域名走直抓(无需 key,CF 放行 Java UA)——DeepSeek 不可用时仍能抓 wiki
|
||||
if (url.contains("zh.minecraft.wiki")) {
|
||||
String wikiResult = mcWikiFetch(url);
|
||||
if (wikiResult != null) return wikiResult;
|
||||
}
|
||||
if (apiKey == null || apiKey.isEmpty()) return null;
|
||||
return deepseekFetch(url, apiKey);
|
||||
}
|
||||
|
||||
/** DeepSeek Fetch(原 fetchUrl 的 DeepSeek 分支,改名为内部方法) */
|
||||
private static String deepseekFetch(String url, String apiKey) {
|
||||
try {
|
||||
String body = "{\"url\":\"" + escapeJson(url) + "\"}";
|
||||
|
||||
@@ -218,6 +243,103 @@ public class WebSearchTool {
|
||||
|
||||
// ==================== 公开搜索后端(第109批) ====================
|
||||
|
||||
/**
|
||||
* ★ 第110批:中文 Minecraft Wiki 搜索(zh.minecraft.wiki)。
|
||||
* 接口:?search=关键词&title=Special%3ASearch&profile=advanced&fulltext=1&ns0=1...
|
||||
* 结构:li.mw-search-result > div.mw-search-result-heading > a(href,标题) + div.searchresult(摘要)
|
||||
* ⚠️ 必须用 WIKI_UA(Java UA)——Cloudflare 对浏览器 UA 发 JS 挑战返回 403。
|
||||
*/
|
||||
private static String mcWikiSearch(String query) {
|
||||
try {
|
||||
String url = WIKI_SEARCH_URL
|
||||
+ URLEncoder.encode(query, StandardCharsets.UTF_8.name()).replace("+", "%20")
|
||||
+ WIKI_SEARCH_PARAMS;
|
||||
String html = httpGet(url, WIKI_UA);
|
||||
if (html == null) return null;
|
||||
|
||||
Pattern tp = Pattern.compile(
|
||||
"<li class=\"mw-search-result[^\"]*\"[^>]*>.*?<div class=\"mw-search-result-heading\">\\s*<a href=\"([^\"]+)\"[^>]*>(.*?)</a>",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
||||
Matcher tm = tp.matcher(html);
|
||||
List<String> titles = new ArrayList<>();
|
||||
List<String> urls = new ArrayList<>();
|
||||
while (tm.find() && titles.size() < MAX_RESULTS) {
|
||||
String t = stripTags(tm.group(2));
|
||||
if (!t.isEmpty()) {
|
||||
titles.add(t);
|
||||
urls.add(tm.group(1));
|
||||
}
|
||||
}
|
||||
Pattern sp = Pattern.compile(
|
||||
"<div class=\"searchresult\">(.*?)</div>",
|
||||
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
||||
Matcher sm = sp.matcher(html);
|
||||
List<String> snippets = new ArrayList<>();
|
||||
while (sm.find() && snippets.size() < MAX_RESULTS) {
|
||||
snippets.add(stripTags(sm.group(1)));
|
||||
}
|
||||
|
||||
return buildResult("Minecraft Wiki", titles, snippets, urls);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[WebSearchTool] Minecraft Wiki异常: " + e.getClass().getSimpleName() + ": " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ★ 第110批:抓取中文 Minecraft Wiki 文章页正文(zh.minecraft.wiki/w/标题)。
|
||||
* 提取 mw-content-ltr mw-parser-output 容器内正文(到 printfooter 为止),
|
||||
* 去注释/style/script/标签,截断到 4000 字(防上下文爆炸)。
|
||||
* ⚠️ 同 mcWikiSearch:必须用 WIKI_UA。
|
||||
*/
|
||||
private static String mcWikiFetch(String url) {
|
||||
try {
|
||||
String encoded = encodeWikiUrl(url);
|
||||
String html = httpGet(encoded, WIKI_UA);
|
||||
if (html == null) return null;
|
||||
|
||||
int s = html.indexOf("<div class=\"mw-content-ltr mw-parser-output\"");
|
||||
if (s < 0) return null;
|
||||
s = html.indexOf('>', s) + 1;
|
||||
int e = html.indexOf("<div class=\"printfooter\"");
|
||||
if (e < 0 || e <= s) e = html.length();
|
||||
String body = html.substring(s, e);
|
||||
body = body.replaceAll("(?s)<!--.*?-->", "");
|
||||
body = body.replaceAll("(?s)<style[^>]*>.*?</style>", "");
|
||||
body = body.replaceAll("(?s)<script[^>]*>.*?</script>", "");
|
||||
body = stripTags(body);
|
||||
if (body.isEmpty()) return null;
|
||||
return body.length() > 4000 ? body.substring(0, 4000) + "…" : body;
|
||||
} catch (Exception e) {
|
||||
System.out.println("[WebSearchTool] Minecraft Wiki抓取异常: " + e.getClass().getSimpleName() + ": " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** wiki URL 路径逐段编码(模型输出的 URL 可能含未编码中文,Java URL 不接受) */
|
||||
private static String encodeWikiUrl(String url) {
|
||||
if (url == null) return null;
|
||||
int schemeEnd = url.indexOf("://");
|
||||
if (schemeEnd < 0) return url;
|
||||
int pathStart = url.indexOf('/', schemeEnd + 3);
|
||||
if (pathStart < 0) return url;
|
||||
String base = url.substring(0, pathStart);
|
||||
String path = url.substring(pathStart);
|
||||
// ★ 修复:path 以 '/' 开头时首段为空串,必须保留段间斜杠,
|
||||
// 否则 base 尾部 '/' 丢失 → "zh.minecraft.wiki" + "w/..." = "zh.minecraft.wikiw/..."
|
||||
String[] segs = path.split("/", -1);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < segs.length; i++) {
|
||||
if (i > 0) sb.append('/');
|
||||
try {
|
||||
sb.append(URLEncoder.encode(segs[i], StandardCharsets.UTF_8.name()));
|
||||
} catch (Exception e) {
|
||||
sb.append(segs[i]);
|
||||
}
|
||||
}
|
||||
return base + sb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 好搜(so.com) 搜索。实测摘要最全(含百科/金字塔等级等正文片段),质量最高,排第一。
|
||||
* 页面结构:<h3 class="res-title"><a href="...">标题</a></h3> + <p class="res-desc">摘要</p>
|
||||
@@ -351,9 +473,14 @@ public class WebSearchTool {
|
||||
|
||||
/** GET 请求拉整页;非 200 或异常返回 null(调用方决定下一个后端) */
|
||||
private static String httpGet(String urlStr) throws Exception {
|
||||
return httpGet(urlStr, USER_AGENT);
|
||||
}
|
||||
|
||||
/** GET 请求拉整页(自定义 UA——wiki 必须用 Java UA 过 Cloudflare) */
|
||||
private static String httpGet(String urlStr, String ua) throws Exception {
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("User-Agent", USER_AGENT);
|
||||
conn.setRequestProperty("User-Agent", ua);
|
||||
conn.setRequestProperty("Accept", "text/html");
|
||||
conn.setConnectTimeout(TIMEOUT_MS);
|
||||
conn.setReadTimeout(TIMEOUT_MS);
|
||||
@@ -406,15 +533,19 @@ public class WebSearchTool {
|
||||
|
||||
/** 去除 HTML 标签和转义字符 */
|
||||
private static String stripTags(String html) {
|
||||
return html.replaceAll("<[^>]+>", "")
|
||||
.replaceAll("&", "&")
|
||||
String s = html.replaceAll("<[^>]+>", "");
|
||||
// ★ 第110批:wiki 残留清理——数字实体(  “)、wiki 链接语法([[ ]])
|
||||
s = s.replaceAll("&#\\d+;", " ");
|
||||
s = s.replace("[[", "").replace("]]", "");
|
||||
s = s.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll(""", "\"")
|
||||
.replaceAll("'", "'")
|
||||
.replaceAll("'", "'")
|
||||
.replaceAll(" ", " ")
|
||||
.replaceAll("\\s+", " ")
|
||||
.trim();
|
||||
.replaceAll(" ", " ");
|
||||
// 实体还原后可能再生出标签(如 <dataTag> → <dataTag>),再去一次
|
||||
s = s.replaceAll("<[^>]+>", " ");
|
||||
return s.replaceAll("\\s+", " ").trim();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user