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