Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Sep 28, 2021
1 parent 9240a9b commit 7acccc0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/data/BookSourceXxl.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"ruleChapterName": "$.chapterTitle",
"ruleChapterVip": "$.isFree@js:var vip = false; if (result == '0') { vip = true } vip",
"ruleChapterPay": "$.isPay",
"ruleContentUrl": "$.id@js:\"https://www.shuidi.online:9021/fileMgt/getAudioByChapterId?bookId=\"\n + java.getString(\"$.bookId\") + \"&chapterId=\" + result\n + \"&pageNum=1&pageSize=50&{{var header = JSON.parse(source.getLoginHeader());'Authorization=' + header.Authorization}}\"",
"ruleContentUrl": "$.id@js:\"https://www.shuidi.online:9021/fileMgt/getAudioByChapterId?bookId=\" + java.getString(\"$.bookId\") + \"&chapterId=\" + result + \"&pageNum=1&pageSize=50&{{var header = JSON.parse(source.getLoginHeader());var reg = /&chapterId=(.*)&pageNum/;var chapterId = reg.exec(result)[1];var keyId = '1632746188011002';var ks = java.md5Encode(keyId + chapterId + header.Authorization);'Authorization=' + header.Authorization + '&keyId=' + keyId + '&keySecret=' + ks}\" + \"}\"",
"ruleBookContent": "",
"payAction": "var chapterUrl = chapter.getDurChapterUrl();var reg = /&chapterId=(.*)&pageNum/;'https://www.kaixin7days.com/book-service/bookMgt/createOrder?type=2&bookId=' + book.getVariableMap().get('bookId') + '&chapterIds=' + reg.exec(chapterUrl)[1]"
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/kunfei/bookshelf/help/JsExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.kunfei.bookshelf.bean.CookieBean;
import com.kunfei.bookshelf.model.analyzeRule.AnalyzeHeaders;
import com.kunfei.bookshelf.model.analyzeRule.AnalyzeUrl;
import com.kunfei.bookshelf.utils.MD5Utils;
import com.kunfei.bookshelf.utils.StringUtils;

import org.jsoup.Connection;
Expand Down Expand Up @@ -109,4 +110,8 @@ default String getCache(String key) {
return cookie.getCookie();
}

default String md5Encode(String text) {
return MD5Utils.strToMd5By32(text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,31 @@ private String replaceRegex(String result, SourceRule rule) {
return result;
}

/**
* 替换JS
*/
@SuppressLint("DefaultLocale")
private String replaceJs(String ruleStr) throws Exception {
if (ruleStr.contains("{{") && ruleStr.contains("}}")) {
Object jsEval;
StringBuffer sb = new StringBuffer(ruleStr.length());
Matcher expMatcher = EXP_PATTERN.matcher(ruleStr);
while (expMatcher.find()) {
jsEval = evalJS(expMatcher.group(1), object);
if (jsEval instanceof String) {
expMatcher.appendReplacement(sb, (String) jsEval);
} else if (jsEval instanceof Double && ((Double) jsEval) % 1.0 == 0) {
expMatcher.appendReplacement(sb, String.format("%.0f", (Double) jsEval));
} else {
expMatcher.appendReplacement(sb, String.valueOf(jsEval));
}
}
expMatcher.appendTail(sb);
ruleStr = sb.toString();
}
return ruleStr;
}

/**
* 分解规则生成规则列表
*/
Expand All @@ -406,6 +431,8 @@ public List<SourceRule> splitSourceRule(String ruleStr) throws Exception {
ruleStr = splitPutRule(ruleStr);
//替换get值
ruleStr = replaceGet(ruleStr);
//替换js
ruleStr = replaceJs(ruleStr);
//拆分为列表
int start = 0;
String tmp;
Expand All @@ -432,14 +459,14 @@ public List<SourceRule> splitSourceRule(String ruleStr) throws Exception {
/**
* 规则类
*/
public class SourceRule {
public static class SourceRule {
Mode mode;
String rule;
String replaceRegex = "";
String replacement = "";
boolean replaceFirst = false;

SourceRule(String ruleStr, Mode mainMode) throws Exception {
SourceRule(String ruleStr, Mode mainMode) {
this.mode = mainMode;
if (mode == Mode.Js) {
if (ruleStr.startsWith("<js>")) {
Expand All @@ -463,8 +490,6 @@ public class SourceRule {
} else {
rule = ruleStr;
}
//替换js
rule = replaceJs(rule);
//分离正则表达式
String[] ruleStrS = rule.trim().split("##");
rule = ruleStrS[0];
Expand All @@ -480,31 +505,6 @@ public class SourceRule {
}
}

/**
* 替换JS
*/
@SuppressLint("DefaultLocale")
private String replaceJs(String ruleStr) throws Exception {
if (ruleStr.contains("{{") && ruleStr.contains("}}")) {
Object jsEval;
StringBuffer sb = new StringBuffer(ruleStr.length());
Matcher expMatcher = EXP_PATTERN.matcher(ruleStr);
while (expMatcher.find()) {
jsEval = evalJS(expMatcher.group(1), object);
if (jsEval instanceof String) {
expMatcher.appendReplacement(sb, (String) jsEval);
} else if (jsEval instanceof Double && ((Double) jsEval) % 1.0 == 0) {
expMatcher.appendReplacement(sb, String.format("%.0f", (Double) jsEval));
} else {
expMatcher.appendReplacement(sb, String.valueOf(jsEval));
}
}
expMatcher.appendTail(sb);
ruleStr = sb.toString();
}
return ruleStr;
}

}

private enum Mode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private String replaceJs(String ruleUrl) throws Exception {
StringBuffer sb = new StringBuffer(ruleUrl.length());
Matcher expMatcher = EXP_PATTERN.matcher(ruleUrl);
while (expMatcher.find()) {
jsEval = evalJS(expMatcher.group(1), null);
jsEval = evalJS(expMatcher.group(1), ruleUrl);
if (jsEval instanceof String) {
expMatcher.appendReplacement(sb, (String) jsEval);
} else if (jsEval instanceof Double && ((Double) jsEval) % 1.0 == 0) {
Expand Down Expand Up @@ -279,6 +279,7 @@ private void generateUrlPath(String ruleUrl) {
*/
private Object evalJS(String jsStr, Object result) throws Exception {
SimpleBindings bindings = new SimpleBindings();
bindings.put("java", this);
bindings.put("baseUrl", baseUrl);
bindings.put("searchPage", searchPage);
bindings.put("searchKey", searchKey);
Expand Down

0 comments on commit 7acccc0

Please sign in to comment.