Use gson for xpath

This commit is contained in:
FongMi 2022-08-22 18:55:27 +08:00
parent ffdbe8b88a
commit ce3aa70ea3
3 changed files with 123 additions and 73 deletions

View File

@ -16,6 +16,12 @@ public class Result {
private List<Vod> list; private List<Vod> list;
@SerializedName("filters") @SerializedName("filters")
private JSONObject filters; private JSONObject filters;
@SerializedName("parse")
private String parse;
@SerializedName("url")
private String url;
@SerializedName("ua")
private String ua;
public void setClasses(List<Class> classes) { public void setClasses(List<Class> classes) {
this.classes = classes; this.classes = classes;
@ -29,6 +35,18 @@ public class Result {
this.filters = filters; this.filters = filters;
} }
public void setParse(String parse) {
this.parse = parse;
}
public void setUrl(String url) {
this.url = url;
}
public void setUa(String ua) {
this.ua = ua;
}
@NotNull @NotNull
@Override @Override
public String toString() { public String toString() {

View File

@ -4,6 +4,8 @@ import com.google.gson.annotations.SerializedName;
public class Vod { public class Vod {
@SerializedName("type_name")
private String typeName;
@SerializedName("vod_id") @SerializedName("vod_id")
private String vodId; private String vodId;
@SerializedName("vod_name") @SerializedName("vod_name")
@ -12,6 +14,23 @@ public class Vod {
private String vodPic; private String vodPic;
@SerializedName("vod_remarks") @SerializedName("vod_remarks")
private String vodRemarks; private String vodRemarks;
@SerializedName("vod_year")
private String vodYear;
@SerializedName("vod_area")
private String vodArea;
@SerializedName("vod_actor")
private String vodActor;
@SerializedName("vod_director")
private String vodDirector;
@SerializedName("vod_content")
private String vodContent;
@SerializedName("vod_play_from")
private String vodPlayFrom;
@SerializedName("vod_play_url")
private String vodPlayUrl;
public Vod() {
}
public Vod(String vodId, String vodName, String vodPic, String vodRemarks) { public Vod(String vodId, String vodName, String vodPic, String vodRemarks) {
this.vodId = vodId; this.vodId = vodId;
@ -19,4 +38,52 @@ public class Vod {
this.vodPic = vodPic; this.vodPic = vodPic;
this.vodRemarks = vodRemarks; this.vodRemarks = vodRemarks;
} }
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public void setVodId(String vodId) {
this.vodId = vodId;
}
public void setVodName(String vodName) {
this.vodName = vodName;
}
public void setVodPic(String vodPic) {
this.vodPic = vodPic;
}
public void setVodRemarks(String vodRemarks) {
this.vodRemarks = vodRemarks;
}
public void setVodYear(String vodYear) {
this.vodYear = vodYear;
}
public void setVodArea(String vodArea) {
this.vodArea = vodArea;
}
public void setVodActor(String vodActor) {
this.vodActor = vodActor;
}
public void setVodDirector(String vodDirector) {
this.vodDirector = vodDirector;
}
public void setVodContent(String vodContent) {
this.vodContent = vodContent;
}
public void setVodPlayFrom(String vodPlayFrom) {
this.vodPlayFrom = vodPlayFrom;
}
public void setVodPlayUrl(String vodPlayUrl) {
this.vodPlayUrl = vodPlayUrl;
}
} }

View File

@ -10,7 +10,6 @@ import com.github.catvod.crawler.Spider;
import com.github.catvod.crawler.SpiderDebug; import com.github.catvod.crawler.SpiderDebug;
import com.github.catvod.net.OkHttpUtil; import com.github.catvod.net.OkHttpUtil;
import com.github.catvod.utils.Misc; import com.github.catvod.utils.Misc;
import com.github.catvod.utils.Trans;
import com.github.catvod.xpath.XPathRule; import com.github.catvod.xpath.XPathRule;
import org.json.JSONArray; import org.json.JSONArray;
@ -20,6 +19,7 @@ import org.seimicrawler.xpath.JXNode;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -123,7 +123,7 @@ public class XPath extends Spider {
try { try {
fetchRule(); fetchRule();
String webUrl = categoryUrl(tid, pg, filter, extend); String webUrl = categoryUrl(tid, pg, filter, extend);
JSONArray videos = new JSONArray(); List<Vod> videos = new ArrayList<>();
JXDocument doc = JXDocument.create(fetch(webUrl)); JXDocument doc = JXDocument.create(fetch(webUrl));
List<JXNode> vodNodes = doc.selN(rule.getCateVodNode()); List<JXNode> vodNodes = doc.selN(rule.getCateVodNode());
for (int i = 0; i < vodNodes.size(); i++) { for (int i = 0; i < vodNodes.size(); i++) {
@ -143,27 +143,18 @@ public class XPath extends Spider {
SpiderDebug.log(e); SpiderDebug.log(e);
} }
} }
JSONObject v = new JSONObject(); videos.add(new Vod(id, name, pic, mark));
v.put("vod_id", id);
v.put("vod_name", Trans.get(name));
v.put("vod_pic", pic);
v.put("vod_remarks", Trans.get(mark));
videos.put(v);
} }
JSONObject result = new JSONObject(); Result result = new Result();
result.put("page", pg); result.setList(videos);
result.put("pagecount", Integer.MAX_VALUE);
result.put("limit", 90);
result.put("total", Integer.MAX_VALUE);
result.put("list", videos);
return result.toString(); return result.toString();
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
return "";
} }
return "";
} }
protected void detailContentExt(String content, JSONObject vod) { protected void detailContentExt(String content, Vod vod) {
} }
@ -175,12 +166,9 @@ public class XPath extends Spider {
String webContent = fetch(webUrl); String webContent = fetch(webUrl);
JXDocument doc = JXDocument.create(webContent); JXDocument doc = JXDocument.create(webContent);
JXNode vodNode = doc.selNOne(rule.getDetailNode()); JXNode vodNode = doc.selNOne(rule.getDetailNode());
String cover = "", title = "", desc = "", category = "", area = "", year = "", remark = "", director = "", actor = ""; String cover = "", title = "", desc = "", category = "", area = "", year = "", remark = "", director = "", actor = "";
title = vodNode.selOne(rule.getDetailName()).asString().trim(); title = vodNode.selOne(rule.getDetailName()).asString().trim();
title = rule.getDetailNameR(title); title = rule.getDetailNameR(title);
if (!rule.getDetailImg().isEmpty()) { if (!rule.getDetailImg().isEmpty()) {
try { try {
cover = vodNode.selOne(rule.getDetailImg()).asString().trim(); cover = vodNode.selOne(rule.getDetailImg()).asString().trim();
@ -247,17 +235,17 @@ public class XPath extends Spider {
} }
} }
JSONObject vod = new JSONObject(); Vod vod = new Vod();
vod.put("vod_id", ids.get(0)); vod.setVodId(ids.get(0));
vod.put("vod_name", Trans.get(title)); vod.setVodName(title);
vod.put("vod_pic", cover); vod.setVodPic(cover);
vod.put("type_name", Trans.get(category)); vod.setTypeName(category);
vod.put("vod_year", year); vod.setVodYear(year);
vod.put("vod_area", Trans.get(area)); vod.setVodArea(area);
vod.put("vod_remarks", Trans.get(remark)); vod.setVodRemarks(remark);
vod.put("vod_actor", Trans.get(actor)); vod.setVodActor(actor);
vod.put("vod_director", Trans.get(director)); vod.setVodDirector(director);
vod.put("vod_content", Trans.get(desc)); vod.setVodContent(desc);
ArrayList<String> playFrom = new ArrayList<>(); ArrayList<String> playFrom = new ArrayList<>();
List<JXNode> fromNodes = doc.selN(rule.getDetailFromNode()); List<JXNode> fromNodes = doc.selN(rule.getDetailFromNode());
@ -277,15 +265,13 @@ public class XPath extends Spider {
name = rule.getDetailUrlNameR(name); name = rule.getDetailUrlNameR(name);
String id = urlNodes.get(j).selOne(rule.getDetailUrlId()).asString().trim(); String id = urlNodes.get(j).selOne(rule.getDetailUrlId()).asString().trim();
id = rule.getDetailUrlIdR(id); id = rule.getDetailUrlIdR(id);
vodItems.add(Trans.get(name) + "$" + id); vodItems.add(name + "$" + id);
} }
// 排除播放列表为空的播放源
if (vodItems.size() == 0 && playFrom.size() > i) { if (vodItems.size() == 0 && playFrom.size() > i) {
playFrom.set(i, ""); playFrom.set(i, "");
} }
playList.add(TextUtils.join("#", vodItems)); playList.add(TextUtils.join("#", vodItems));
} }
// 排除播放列表为空的播放源
for (int i = playFrom.size() - 1; i >= 0; i--) { for (int i = playFrom.size() - 1; i >= 0; i--) {
if (playFrom.get(i).isEmpty()) if (playFrom.get(i).isEmpty())
playFrom.remove(i); playFrom.remove(i);
@ -300,21 +286,16 @@ public class XPath extends Spider {
} }
String vod_play_from = TextUtils.join("$$$", playFrom); String vod_play_from = TextUtils.join("$$$", playFrom);
String vod_play_url = TextUtils.join("$$$", playList); String vod_play_url = TextUtils.join("$$$", playList);
vod.put("vod_play_from", Trans.get(vod_play_from)); vod.setVodPlayFrom(vod_play_from);
vod.put("vod_play_url", vod_play_url); vod.setVodPlayUrl(vod_play_url);
detailContentExt(webContent, vod); detailContentExt(webContent, vod);
JSONObject result = new JSONObject(); Result result = new Result();
JSONArray list = new JSONArray(); result.setList(Arrays.asList(vod));
list.put(vod);
result.put("list", list);
return result.toString(); return result.toString();
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e.getMessage()); SpiderDebug.log(e);
for (StackTraceElement traceEl : e.getStackTrace()) { return "";
SpiderDebug.log(traceEl.toString());
}
} }
return "";
} }
@Override @Override
@ -323,31 +304,26 @@ public class XPath extends Spider {
fetchRule(); fetchRule();
String webUrl = rule.getPlayUrl().isEmpty() ? id : rule.getPlayUrl().replace("{playUrl}", id); String webUrl = rule.getPlayUrl().isEmpty() ? id : rule.getPlayUrl().replace("{playUrl}", id);
SpiderDebug.log(webUrl); SpiderDebug.log(webUrl);
JSONObject result = new JSONObject(); Result result = new Result();
result.put("parse", "1"); result.setParse("1");
if (!rule.getPlayUa().isEmpty()) { if (!rule.getPlayUa().isEmpty()) result.setUa(rule.getPlayUa());
result.put("ua", rule.getPlayUa()); result.setUrl(webUrl);
}
result.put("url", webUrl);
return result.toString(); return result.toString();
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
return "";
} }
return "";
} }
@Override @Override
public String searchContent(String key, boolean quick) { public String searchContent(String key, boolean quick) {
try { try {
fetchRule(); fetchRule();
if (rule.getSearchUrl().isEmpty()) { if (rule.getSearchUrl().isEmpty()) return "";
return "";
}
String webUrl = rule.getSearchUrl().replace("{wd}", URLEncoder.encode(key)); String webUrl = rule.getSearchUrl().replace("{wd}", URLEncoder.encode(key));
String webContent = fetch(webUrl); String webContent = fetch(webUrl);
JSONObject result = new JSONObject(); Result result = new Result();
JSONArray videos = new JSONArray(); List<Vod> videos = new ArrayList<>();
// add maccms suggest search api support
if (rule.getSearchVodNode().startsWith("json:")) { if (rule.getSearchVodNode().startsWith("json:")) {
String[] node = rule.getSearchVodNode().substring(5).split(">"); String[] node = rule.getSearchVodNode().substring(5).split(">");
JSONObject data = new JSONObject(webContent); JSONObject data = new JSONObject(webContent);
@ -365,12 +341,7 @@ public class XPath extends Spider {
pic = Misc.fixUrl(webUrl, pic); pic = Misc.fixUrl(webUrl, pic);
String mark = vod.optString(rule.getSearchVodMark()).trim(); String mark = vod.optString(rule.getSearchVodMark()).trim();
mark = rule.getSearchVodMarkR(mark); mark = rule.getSearchVodMarkR(mark);
JSONObject v = new JSONObject(); videos.add(new Vod(id, name, pic, mark));
v.put("vod_id", id);
v.put("vod_name", Trans.get(name));
v.put("vod_pic", pic);
v.put("vod_remarks", Trans.get(mark));
videos.put(v);
} }
} else { } else {
data = data.getJSONObject(node[i]); data = data.getJSONObject(node[i]);
@ -396,21 +367,15 @@ public class XPath extends Spider {
SpiderDebug.log(e); SpiderDebug.log(e);
} }
} }
JSONObject v = new JSONObject(); videos.add(new Vod(id, name, pic, mark));
v.put("vod_id", id);
v.put("vod_name", Trans.get(name));
v.put("vod_pic", pic);
v.put("vod_remarks", Trans.get(mark));
videos.put(v);
} }
} }
result.put("list", videos); result.setList(videos);
return result.toString(); return result.toString();
} catch ( } catch (Exception e) {
Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
return "";
} }
return "";
} }
protected void fetchRule() { protected void fetchRule() {