diff --git a/app/src/main/java/com/github/catvod/spider/TgSearchBaidu.java b/app/src/main/java/com/github/catvod/spider/TgSearchBaidu.java index c81372e0..93cfa549 100644 --- a/app/src/main/java/com/github/catvod/spider/TgSearchBaidu.java +++ b/app/src/main/java/com/github/catvod/spider/TgSearchBaidu.java @@ -11,19 +11,15 @@ import com.github.catvod.utils.Util; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; + +import java.util.*; public class TgSearchBaidu extends Cloud { private static final String KEY_API_URLS = "api_urls"; private static final String KEY_DOMAIN_MAP = "siteurl"; private static final String KEY_SOURCES = "sources"; private static final int DEFAULT_PAGE_SIZE = 10; - + private List apiUrls = new ArrayList<>(); private Map domainMap = new HashMap() {{ put("alipan", "阿里"); @@ -42,15 +38,15 @@ public class TgSearchBaidu extends Cloud { @Override public synchronized void init(Context context, String extend) throws Exception { super.init(context, extend); - + this.apiUrls.clear(); - + if (!TextUtils.isEmpty(extend)) { try { if (extend.contains("###")) { String[] infos = extend.split("###"); this.extInfos = infos; - + if (infos.length > 0 && !TextUtils.isEmpty(infos[0])) { processExtendConfig(infos[0]); } @@ -58,11 +54,12 @@ public class TgSearchBaidu extends Cloud { this.extInfos = new String[]{extend}; processExtendConfig(extend); } - } catch (Exception e) {} + } catch (Exception e) { + } } this.extInfos = null; // 重置,避免内存泄漏 } - + private void processExtendConfig(String config) { try { // 检查是否为HTTP URL @@ -72,23 +69,23 @@ public class TgSearchBaidu extends Cloud { } return; } - + // 尝试JSON格式解析 JsonObject ext = Json.safeObject(config); - + // 处理API URLs配置 processApiUrls(ext); - + // 处理域名映射配置 processDomainMap(ext); - + // 处理来源过滤配置 processSources(ext, true); } catch (Exception e) { // 可以添加日志记录 } } - + private void processApiUrls(JsonObject config) { if (config.has(KEY_API_URLS) && config.get(KEY_API_URLS).isJsonArray()) { JsonArray urlsArray = config.getAsJsonArray(KEY_API_URLS); @@ -102,30 +99,34 @@ public class TgSearchBaidu extends Cloud { } } } - + private void processDomainMap(JsonObject config) { if (config.has(KEY_DOMAIN_MAP) && config.get(KEY_DOMAIN_MAP).isJsonObject()) { JsonObject customDomains = config.getAsJsonObject(KEY_DOMAIN_MAP); for (Map.Entry entry : customDomains.entrySet()) { if (entry == null || entry.getValue() == null) continue; - + String domain = entry.getKey(); String sourceName = ""; - try { sourceName = entry.getValue().getAsString(); } catch (Exception e) { continue; } - + try { + sourceName = entry.getValue().getAsString(); + } catch (Exception e) { + continue; + } + if (!TextUtils.isEmpty(domain) && !TextUtils.isEmpty(sourceName) && !domainMap.containsKey(domain)) { domainMap.put(domain, sourceName); } } } } - + private void processSources(JsonObject config, boolean clearExisting) { if (config.has(KEY_SOURCES) && config.get(KEY_SOURCES).isJsonArray()) { if (clearExisting) { this.sources.clear(); } - + JsonArray sourcesArray = config.getAsJsonArray(KEY_SOURCES); for (JsonElement element : sourcesArray) { if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) { @@ -141,7 +142,6 @@ public class TgSearchBaidu extends Cloud { private boolean isValidUrl(String url) { return !TextUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://")); } - private Map getHeader() { @@ -155,74 +155,75 @@ public class TgSearchBaidu extends Cloud { if (key == null || key.trim().isEmpty()) { return Result.error("关键词不能为空"); } - + return performSearch(key, 1, DEFAULT_PAGE_SIZE); } private String performSearch(String key, int page, int pageSize) throws Exception { List list = new ArrayList<>(); int total = 0; - + Map params = new HashMap<>(); params.put("kw", key); params.put("page", String.valueOf(page)); params.put("size", String.valueOf(pageSize)); - + for (String apiUrl : apiUrls) { if (!isValidUrl(apiUrl)) continue; - + try { OkResult result = OkHttp.get(apiUrl, params, getHeader()); if (result.getCode() == 500 || TextUtils.isEmpty(result.getBody())) continue; - + JsonObject jsonObject = Json.safeObject(result.getBody()); - if (!jsonObject.has("code") || jsonObject.get("code").getAsInt() != 0 || !jsonObject.has("data")) continue; - + if (!jsonObject.has("code") || jsonObject.get("code").getAsInt() != 0 || !jsonObject.has("data")) + continue; + JsonObject data = jsonObject.getAsJsonObject("data"); - + total = data.has("total") && !data.get("total").isJsonNull() ? data.get("total").getAsInt() : 0; - + // 直接检查merged_by_type字段,无需三重判断 if (!data.has("merged_by_type") || !data.get("merged_by_type").isJsonObject()) continue; - + JsonObject mergedByType = data.getAsJsonObject("merged_by_type"); - + for (Map.Entry categoryEntry : mergedByType.entrySet()) { if (!categoryEntry.getValue().isJsonArray()) continue; - + JsonArray items = categoryEntry.getValue().getAsJsonArray(); - + for (JsonElement item : items) { if (!item.isJsonObject()) continue; - + JsonObject entry = item.getAsJsonObject(); - + String vodUrl = entry.has("url") && !entry.get("url").isJsonNull() ? entry.get("url").getAsString() : ""; if (TextUtils.isEmpty(vodUrl)) continue; - + // 获取来源信息并映射 String originalSource = entry.has("source") && !entry.get("source").isJsonNull() ? entry.get("source").getAsString() : "未知来源"; String sourceName = mapSource(vodUrl, originalSource); - + // 获取标题 String title = entry.has("note") && !entry.get("note").isJsonNull() ? entry.get("note").getAsString() : "未命名资源"; - + // 获取图片 String pic = getFirstImage(entry); - + // 简化VodPlayBuilder的使用 - Vod vod = new Vod("push://" + vodUrl, title, pic, sourceName + " (" + originalSource + ")"); + Vod vod = new Vod(vodUrl, title, pic, sourceName + " (" + originalSource + ")"); // 由于只有一个播放源,直接设置播放信息 vod.setVodPlayFrom(sourceName); vod.setVodPlayUrl("播放源$" + vodUrl); - + // 来源过滤 if (sources.isEmpty() || sources.contains(sourceName)) { list.add(vod); } } } - + int pageCount = total > 0 && pageSize > 0 ? (total + pageSize - 1) / pageSize : 1; return Result.string(page, pageCount, pageSize, total, list); } catch (Exception e) { @@ -230,10 +231,10 @@ public class TgSearchBaidu extends Cloud { continue; } } - + return Result.error("无法连接到任何搜索API"); } - + // 提取来源映射逻辑为单独方法 private String mapSource(String vodUrl, String originalSource) { for (Map.Entry domainEntry : domainMap.entrySet()) { @@ -243,7 +244,7 @@ public class TgSearchBaidu extends Cloud { } return originalSource; } - + // 提取图片获取逻辑为单独方法 private String getFirstImage(JsonObject entry) { if (entry.has("images") && !entry.get("images").isJsonNull() && entry.get("images").isJsonArray()) { diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index 8e4db34d..c0269d11 100644 Binary files a/jar/custom_spider.jar and b/jar/custom_spider.jar differ diff --git a/jar/custom_spider.jar.md5 b/jar/custom_spider.jar.md5 index 5ab23b8f..a3a8c332 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -3a9716ee845abbc8cc2daf9b812128f0 +1b4f17a721470f8b074d883d9d4c4839 diff --git a/json/index.json b/json/index.json index 2482fed5..577b04fc 100644 --- a/json/index.json +++ b/json/index.json @@ -1,5 +1,5 @@ { - "spider": "https://andoridspidermt.netlify.app/jar/custom_spider.jar;md5;3a9716ee845abbc8cc2daf9b812128f0", + "spider": "https://andoridspidermt.netlify.app/jar/custom_spider.jar;md5;1b4f17a721470f8b074d883d9d4c4839", "lives": [ { "name": "电视直播",