diff --git a/app/src/main/java/com/github/catvod/bean/Filter.java b/app/src/main/java/com/github/catvod/bean/Filter.java new file mode 100644 index 0000000..70d2b52 --- /dev/null +++ b/app/src/main/java/com/github/catvod/bean/Filter.java @@ -0,0 +1,46 @@ +package com.github.catvod.bean; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class Filter { + + @SerializedName("key") + private String key; + @SerializedName("name") + private String name; + @SerializedName("value") + private List value; + + public Filter(String key, String name, List value) { + this.key = key; + this.name = name; + this.value = value; + } + + public void setKey(String key) { + this.key = key; + } + + public void setName(String name) { + this.name = name; + } + + public void setValue(List value) { + this.value = value; + } + + public static class Value { + + @SerializedName("n") + private String n; + @SerializedName("v") + private String v; + + public Value(String n, String v) { + this.n = n; + this.v = v; + } + } +} diff --git a/app/src/main/java/com/github/catvod/bean/Result.java b/app/src/main/java/com/github/catvod/bean/Result.java index eb5c2cb..ab061f4 100644 --- a/app/src/main/java/com/github/catvod/bean/Result.java +++ b/app/src/main/java/com/github/catvod/bean/Result.java @@ -2,11 +2,14 @@ package com.github.catvod.bean; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; +import com.google.gson.reflect.TypeToken; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; +import java.lang.reflect.Type; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; public class Result { @@ -16,7 +19,7 @@ public class Result { @SerializedName("list") private List list; @SerializedName("filters") - private JSONObject filters; + private LinkedHashMap> filters; @SerializedName("header") private String header; @SerializedName("parse") @@ -38,10 +41,15 @@ public class Result { return list == null ? Collections.emptyList() : list; } - public void setFilters(JSONObject filters) { + public void setFilters(LinkedHashMap> filters) { this.filters = filters; } + public void setFilters(JSONObject object) { + Type listType = new TypeToken>>() {}.getType(); + this.filters = new Gson().fromJson(object.toString(), listType); + } + public void setHeader(String header) { this.header = header; } diff --git a/app/src/main/java/com/github/catvod/spider/Dm84.java b/app/src/main/java/com/github/catvod/spider/Dm84.java index ccf3983..8c6bd7e 100644 --- a/app/src/main/java/com/github/catvod/spider/Dm84.java +++ b/app/src/main/java/com/github/catvod/spider/Dm84.java @@ -3,6 +3,7 @@ package com.github.catvod.spider; import android.text.TextUtils; import com.github.catvod.bean.Class; +import com.github.catvod.bean.Filter; import com.github.catvod.bean.Result; import com.github.catvod.bean.Vod; import com.github.catvod.crawler.Spider; @@ -10,9 +11,6 @@ import com.github.catvod.net.OkHttpUtil; import com.github.catvod.utils.Misc; import com.google.gson.Gson; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -38,20 +36,15 @@ public class Dm84 extends Spider { return headers; } - private JSONObject addFilter(String name, String key, List texts) throws JSONException { - JSONObject object = new JSONObject(); - JSONArray array = new JSONArray(); + private Filter addFilter(String name, String key, List texts) { + List values = new ArrayList<>(); for (String text : texts) { if (text.isEmpty()) continue; - JSONObject o = new JSONObject(); - o.put("n", text.replace("按", "")); - o.put("v", key.equals("by") ? replaceBy(text) : text); - array.put(o); + String n = text.replace("按", ""); + String v = key.equals("by") ? replaceBy(text) : text; + values.add(new Filter.Value(n, v)); } - object.put("key", key); - object.put("name", name); - object.put("value", array); - return object; + return new Filter(key, name, values); } private String replaceBy(String text) { @@ -60,9 +53,9 @@ public class Dm84 extends Spider { @Override public String homeContent(boolean filter) { - List classes = new ArrayList<>(); List videos = new ArrayList<>(); - JSONObject filters = new JSONObject(); + List classes = new ArrayList<>(); + LinkedHashMap> filters = new LinkedHashMap<>(); Document doc = Jsoup.parse(OkHttpUtil.string(siteUrl, getHeaders())); for (Element element : doc.select("ul.nav_row > li > a")) { if (element.attr("href").startsWith("/list")) { @@ -72,16 +65,13 @@ public class Dm84 extends Spider { } } for (Class item : classes) { - try { - JSONArray array = new JSONArray(); - doc = Jsoup.parse(OkHttpUtil.string(siteUrl + "/list-" + item.getTypeId() + ".html", getHeaders())); - Elements elements = doc.select("ul.list_filter > li > div"); - array.put(addFilter("類型", "type", elements.get(0).select("a").eachText())); - array.put(addFilter("時間", "year", elements.get(1).select("a").eachText())); - array.put(addFilter("排序", "by", elements.get(2).select("a").eachText())); - filters.put(item.getTypeId(), array); - } catch (Exception ignored) { - } + doc = Jsoup.parse(OkHttpUtil.string(siteUrl + "/list-" + item.getTypeId() + ".html", getHeaders())); + Elements elements = doc.select("ul.list_filter > li > div"); + List array = new ArrayList<>(); + array.add(addFilter("類型", "type", elements.get(0).select("a").eachText())); + array.add(addFilter("時間", "year", elements.get(1).select("a").eachText())); + array.add(addFilter("排序", "by", elements.get(2).select("a").eachText())); + filters.put(item.getTypeId(), array); } for (Element element : doc.select("div.item")) { String img = element.select("a.cover").attr("data-bg");