|
|
@ -1,8 +1,34 @@ |
|
|
|
package xyz.fycz.myreader.model.analyzeRule; |
|
|
|
package xyz.fycz.myreader.model.analyzeRule; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Keep; |
|
|
|
import androidx.annotation.Keep; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.jsoup.nodes.Entities; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.regex.Matcher; |
|
|
|
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.script.SimpleBindings; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import xyz.fycz.myreader.greendao.entity.Book; |
|
|
|
|
|
|
|
import xyz.fycz.myreader.util.StringHelper; |
|
|
|
import xyz.fycz.myreader.util.help.JsExtensions; |
|
|
|
import xyz.fycz.myreader.util.help.JsExtensions; |
|
|
|
|
|
|
|
import xyz.fycz.myreader.util.utils.NetworkUtils; |
|
|
|
|
|
|
|
import xyz.fycz.myreader.util.utils.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static xyz.fycz.myreader.common.APPCONST.EXP_PATTERN; |
|
|
|
|
|
|
|
import static xyz.fycz.myreader.common.APPCONST.JS_PATTERN; |
|
|
|
|
|
|
|
import static xyz.fycz.myreader.common.APPCONST.MAP_STRING; |
|
|
|
|
|
|
|
import static xyz.fycz.myreader.common.APPCONST.SCRIPT_ENGINE; |
|
|
|
|
|
|
|
import static xyz.fycz.myreader.util.StringHelper.isEmpty; |
|
|
|
|
|
|
|
import static xyz.fycz.myreader.util.utils.NetworkUtils.headerPattern; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -12,10 +38,10 @@ import xyz.fycz.myreader.util.help.JsExtensions; |
|
|
|
@Keep |
|
|
|
@Keep |
|
|
|
@SuppressWarnings({"unused", "WeakerAccess"}) |
|
|
|
@SuppressWarnings({"unused", "WeakerAccess"}) |
|
|
|
public class AnalyzeRule implements JsExtensions { |
|
|
|
public class AnalyzeRule implements JsExtensions { |
|
|
|
/*private static final Pattern putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE); |
|
|
|
private static final Pattern putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE); |
|
|
|
private static final Pattern getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE); |
|
|
|
private static final Pattern getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE); |
|
|
|
|
|
|
|
|
|
|
|
private BaseBookBean book; |
|
|
|
private Book book; |
|
|
|
private Object object; |
|
|
|
private Object object; |
|
|
|
private Boolean isJSON = false; |
|
|
|
private Boolean isJSON = false; |
|
|
|
private String baseUrl = null; |
|
|
|
private String baseUrl = null; |
|
|
@ -28,11 +54,11 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
private boolean objectChangedJS = false; |
|
|
|
private boolean objectChangedJS = false; |
|
|
|
private boolean objectChangedJP = false; |
|
|
|
private boolean objectChangedJP = false; |
|
|
|
|
|
|
|
|
|
|
|
public AnalyzeRule(BaseBookBean bookBean) { |
|
|
|
public AnalyzeRule(Book bookBean) { |
|
|
|
book = bookBean; |
|
|
|
book = bookBean; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setBook(BaseBookBean book) { |
|
|
|
public void setBook(Book book) { |
|
|
|
this.book = book; |
|
|
|
this.book = book; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -61,9 +87,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return this.baseUrl; |
|
|
|
return this.baseUrl; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取XPath解析类 |
|
|
|
* 获取XPath解析类 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private AnalyzeByXPath getAnalyzeByXPath(Object o) { |
|
|
|
private AnalyzeByXPath getAnalyzeByXPath(Object o) { |
|
|
|
if (o != object) { |
|
|
|
if (o != object) { |
|
|
|
return new AnalyzeByXPath().parse(o); |
|
|
|
return new AnalyzeByXPath().parse(o); |
|
|
@ -80,9 +106,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return analyzeByXPath; |
|
|
|
return analyzeByXPath; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取JSOUP解析类 |
|
|
|
* 获取JSOUP解析类 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private AnalyzeByJSoup getAnalyzeByJSoup(Object o) { |
|
|
|
private AnalyzeByJSoup getAnalyzeByJSoup(Object o) { |
|
|
|
if (o != object) { |
|
|
|
if (o != object) { |
|
|
|
return new AnalyzeByJSoup().parse(o); |
|
|
|
return new AnalyzeByJSoup().parse(o); |
|
|
@ -99,9 +125,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return analyzeByJSoup; |
|
|
|
return analyzeByJSoup; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取JSON解析类 |
|
|
|
* 获取JSON解析类 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private AnalyzeByJSonPath getAnalyzeByJSonPath(Object o) { |
|
|
|
private AnalyzeByJSonPath getAnalyzeByJSonPath(Object o) { |
|
|
|
if (o != object) { |
|
|
|
if (o != object) { |
|
|
|
return new AnalyzeByJSonPath().parse(o); |
|
|
|
return new AnalyzeByJSonPath().parse(o); |
|
|
@ -118,9 +144,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return analyzeByJSonPath; |
|
|
|
return analyzeByJSonPath; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取文本列表 |
|
|
|
* 获取文本列表 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public List<String> getStringList(String rule) throws Exception { |
|
|
|
public List<String> getStringList(String rule) throws Exception { |
|
|
|
return getStringList(rule, false); |
|
|
|
return getStringList(rule, false); |
|
|
|
} |
|
|
|
} |
|
|
@ -180,9 +206,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return (List<String>) result; |
|
|
|
return (List<String>) result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取文本 |
|
|
|
* 获取文本 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public String getString(String rule) throws Exception { |
|
|
|
public String getString(String rule) throws Exception { |
|
|
|
return getString(rule, false); |
|
|
|
return getString(rule, false); |
|
|
|
} |
|
|
|
} |
|
|
@ -235,9 +261,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取Element |
|
|
|
* 获取Element |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public Object getElement(String ruleStr) throws Exception { |
|
|
|
public Object getElement(String ruleStr) throws Exception { |
|
|
|
List<SourceRule> ruleList = splitSourceRule(ruleStr); |
|
|
|
List<SourceRule> ruleList = splitSourceRule(ruleStr); |
|
|
|
Object result = object; |
|
|
|
Object result = object; |
|
|
@ -262,9 +288,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 获取列表 |
|
|
|
* 获取列表 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public List<Object> getElements(String ruleStr) throws Exception { |
|
|
|
public List<Object> getElements(String ruleStr) throws Exception { |
|
|
|
List<SourceRule> ruleList = splitSourceRule(ruleStr); |
|
|
|
List<SourceRule> ruleList = splitSourceRule(ruleStr); |
|
|
@ -294,9 +320,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return (List<Object>) result; |
|
|
|
return (List<Object>) result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 保存变量 |
|
|
|
* 保存变量 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private void putRule(Map<String, String> map) throws Exception { |
|
|
|
private void putRule(Map<String, String> map) throws Exception { |
|
|
|
for (Map.Entry<String, String> entry : map.entrySet()) { |
|
|
|
for (Map.Entry<String, String> entry : map.entrySet()) { |
|
|
|
if (book != null) { |
|
|
|
if (book != null) { |
|
|
@ -305,9 +331,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 分离并执行put规则 |
|
|
|
* 分离并执行put规则 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private String splitPutRule(String ruleStr) throws Exception { |
|
|
|
private String splitPutRule(String ruleStr) throws Exception { |
|
|
|
Matcher putMatcher = putPattern.matcher(ruleStr); |
|
|
|
Matcher putMatcher = putPattern.matcher(ruleStr); |
|
|
|
while (putMatcher.find()) { |
|
|
|
while (putMatcher.find()) { |
|
|
@ -318,9 +344,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return ruleStr; |
|
|
|
return ruleStr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 替换@get |
|
|
|
* 替换@get |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public String replaceGet(String ruleStr) { |
|
|
|
public String replaceGet(String ruleStr) { |
|
|
|
Matcher getMatcher = getPattern.matcher(ruleStr); |
|
|
|
Matcher getMatcher = getPattern.matcher(ruleStr); |
|
|
|
while (getMatcher.find()) { |
|
|
|
while (getMatcher.find()) { |
|
|
@ -334,9 +360,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return ruleStr; |
|
|
|
return ruleStr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 正则替换 |
|
|
|
* 正则替换 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private String replaceRegex(String result, SourceRule rule) { |
|
|
|
private String replaceRegex(String result, SourceRule rule) { |
|
|
|
if (!isEmpty(rule.replaceRegex)) { |
|
|
|
if (!isEmpty(rule.replaceRegex)) { |
|
|
|
if (rule.replaceFirst) { |
|
|
|
if (rule.replaceFirst) { |
|
|
@ -354,9 +380,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 替换JS |
|
|
|
* 替换JS |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
private String replaceJs(String ruleStr) throws Exception { |
|
|
|
private String replaceJs(String ruleStr) throws Exception { |
|
|
|
if (ruleStr.contains("{{") && ruleStr.contains("}}")) { |
|
|
|
if (ruleStr.contains("{{") && ruleStr.contains("}}")) { |
|
|
@ -379,9 +405,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return ruleStr; |
|
|
|
return ruleStr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 分解规则生成规则列表 |
|
|
|
* 分解规则生成规则列表 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public List<SourceRule> splitSourceRule(String ruleStr) throws Exception { |
|
|
|
public List<SourceRule> splitSourceRule(String ruleStr) throws Exception { |
|
|
|
List<SourceRule> ruleList = new ArrayList<>(); |
|
|
|
List<SourceRule> ruleList = new ArrayList<>(); |
|
|
|
if (isEmpty(ruleStr)) return ruleList; |
|
|
|
if (isEmpty(ruleStr)) return ruleList; |
|
|
@ -429,9 +455,9 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return ruleList; |
|
|
|
return ruleList; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 规则类 |
|
|
|
* 规则类 |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
public static class SourceRule { |
|
|
|
public static class SourceRule { |
|
|
|
Mode mode; |
|
|
|
Mode mode; |
|
|
|
String rule; |
|
|
|
String rule; |
|
|
@ -501,15 +527,15 @@ public class AnalyzeRule implements JsExtensions { |
|
|
|
return book.getVariableMap().get(key); |
|
|
|
return book.getVariableMap().get(key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*//**
|
|
|
|
/** |
|
|
|
* 执行JS |
|
|
|
* 执行JS |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
private Object evalJS(String jsStr, Object result) throws Exception { |
|
|
|
private Object evalJS(String jsStr, Object result) throws Exception { |
|
|
|
SimpleBindings bindings = new SimpleBindings(); |
|
|
|
SimpleBindings bindings = new SimpleBindings(); |
|
|
|
bindings.put("java", this); |
|
|
|
bindings.put("java", this); |
|
|
|
bindings.put("result", result); |
|
|
|
bindings.put("result", result); |
|
|
|
bindings.put("baseUrl", baseUrl); |
|
|
|
bindings.put("baseUrl", baseUrl); |
|
|
|
return SCRIPT_ENGINE.eval(jsStr, bindings); |
|
|
|
return SCRIPT_ENGINE.eval(jsStr, bindings); |
|
|
|
}*/ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|