From 213b3e772db16db08b29841631ec06b25f3f7122 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 23 Oct 2019 12:33:18 +0800 Subject: [PATCH] up --- .../app/model/analyzeRule/AnalyzeRule.kt | 79 +++++++++-------- .../io/legado/app/model/webbook/BookList.kt | 85 ++++--------------- 2 files changed, 59 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 2e1b854fc..5a5cda401 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -6,6 +6,7 @@ import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.Pattern.JS_PATTERN import io.legado.app.data.entities.BaseBook import io.legado.app.utils.* +import org.mozilla.javascript.NativeObject import java.util.* import java.util.regex.Pattern import javax.script.SimpleBindings @@ -185,27 +186,32 @@ class AnalyzeRule(private var book: BaseBook? = null) { @JvmOverloads fun getString(ruleList: List, isUrl: Boolean = false): String { var result: Any? = null - content?.let { o -> - if (ruleList.isNotEmpty()) result = o - for (sourceRule in ruleList) { - putRule(sourceRule.putMap) - sourceRule.makeUpRule(result) - result?.let { - if (sourceRule.rule.isNotBlank()) { - result = when (sourceRule.mode) { - Mode.Js -> evalJS(sourceRule.rule, it) - Mode.Json -> getAnalyzeByJSonPath(it).getString(sourceRule.rule) - Mode.XPath -> getAnalyzeByXPath(it).getString(sourceRule.rule) - Mode.Default -> if (isUrl) { - getAnalyzeByJSoup(it).getString0(sourceRule.rule) - } else { - getAnalyzeByJSoup(it).getString(sourceRule.rule) + val content = this.content + if (content != null && ruleList.isNotEmpty()) { + result = content + if (content is NativeObject) { + result = content[ruleList[0].rule]?.toString() + } else { + for (sourceRule in ruleList) { + putRule(sourceRule.putMap) + sourceRule.makeUpRule(result) + result?.let { + if (sourceRule.rule.isNotBlank()) { + result = when (sourceRule.mode) { + Mode.Js -> evalJS(sourceRule.rule, it) + Mode.Json -> getAnalyzeByJSonPath(it).getString(sourceRule.rule) + Mode.XPath -> getAnalyzeByXPath(it).getString(sourceRule.rule) + Mode.Default -> if (isUrl) { + getAnalyzeByJSoup(it).getString0(sourceRule.rule) + } else { + getAnalyzeByJSoup(it).getString(sourceRule.rule) + } + else -> sourceRule.rule } - else -> sourceRule.rule } - } - if (sourceRule.replaceRegex.isNotEmpty()) { - result = replaceRegex(result.toString(), sourceRule) + if (sourceRule.replaceRegex.isNotEmpty()) { + result = replaceRegex(result.toString(), sourceRule) + } } } } @@ -231,7 +237,10 @@ class AnalyzeRule(private var book: BaseBook? = null) { putRule(sourceRule.putMap) result?.let { result = when (sourceRule.mode) { - Mode.Regex -> AnalyzeByRegex.getElement(result.toString(), sourceRule.rule.splitNotBlank("&&")) + Mode.Regex -> AnalyzeByRegex.getElement( + result.toString(), + sourceRule.rule.splitNotBlank("&&") + ) Mode.Js -> evalJS(sourceRule.rule, it) Mode.Json -> getAnalyzeByJSonPath(it).getObject(sourceRule.rule) Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) @@ -260,7 +269,10 @@ class AnalyzeRule(private var book: BaseBook? = null) { putRule(sourceRule.putMap) result?.let { result = when (sourceRule.mode) { - Mode.Regex -> AnalyzeByRegex.getElements(result.toString(), sourceRule.rule.splitNotBlank("&&")) + Mode.Regex -> AnalyzeByRegex.getElements( + result.toString(), + sourceRule.rule.splitNotBlank("&&") + ) Mode.Js -> evalJS(sourceRule.rule, result) Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule) Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) @@ -331,32 +343,28 @@ class AnalyzeRule(private var book: BaseBook? = null) { * 分解规则生成规则列表 */ @Throws(Exception::class) - fun splitSourceRule(ruleStr: String): List { + fun splitSourceRule(ruleStr: String, mode: Mode = Mode.Default): List { var vRuleStr = ruleStr val ruleList = ArrayList() if (TextUtils.isEmpty(vRuleStr)) return ruleList //检测Mode - val mode: Mode + var mMode: Mode = mode when { vRuleStr.startsWith("@XPath:", true) -> { - mode = Mode.XPath + mMode = Mode.XPath vRuleStr = vRuleStr.substring(7) } vRuleStr.startsWith("@Json:", true) -> { - mode = Mode.Json + mMode = Mode.Json vRuleStr = vRuleStr.substring(6) } vRuleStr.startsWith(":") -> { - mode = Mode.Regex + mMode = Mode.Regex isRegex = true vRuleStr = vRuleStr.substring(1) } - else -> mode = - when { - isRegex -> Mode.Regex - isJSON -> Mode.Json - else -> Mode.Default - } + isRegex -> mMode = Mode.Regex + isJSON -> mMode = Mode.Json } //拆分为规则列表 var start = 0 @@ -364,9 +372,10 @@ class AnalyzeRule(private var book: BaseBook? = null) { val jsMatcher = JS_PATTERN.matcher(vRuleStr) while (jsMatcher.find()) { if (jsMatcher.start() > start) { - tmp = vRuleStr.substring(start, jsMatcher.start()).replace("\n", "").trim { it <= ' ' } + tmp = vRuleStr.substring(start, jsMatcher.start()).replace("\n", "") + .trim { it <= ' ' } if (!TextUtils.isEmpty(tmp)) { - ruleList.add(SourceRule(tmp, mode)) + ruleList.add(SourceRule(tmp, mMode)) } } ruleList.add(SourceRule(jsMatcher.group(), Mode.Js)) @@ -375,7 +384,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (vRuleStr.length > start) { tmp = vRuleStr.substring(start).replace("\n", "").trim { it <= ' ' } if (!TextUtils.isEmpty(tmp)) { - ruleList.add(SourceRule(tmp, mode)) + ruleList.add(SourceRule(tmp, mMode)) } } return ruleList diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt index b3f15f1e4..a896aa3c0 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -4,12 +4,10 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook -import io.legado.app.data.entities.rule.BookListRule import io.legado.app.help.BookHelp import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils -import org.mozilla.javascript.NativeObject import retrofit2.Response object BookList { @@ -77,31 +75,21 @@ object BookList { val ruleWordCount = analyzeRule.splitSourceRule(bookListRule.wordCount ?: "") SourceDebug.printLog(bookSource.bookSourceUrl, "列表数为${collections.size}") for ((index, item) in collections.withIndex()) { - if (item is NativeObject) { - getSearchItemAllInOne( - item, - bookSource, - bookListRule, - baseUrl, - index == 0 - ) - } else { - getSearchItem( - item, - analyzeRule, - bookSource, - baseUrl, - index == 0, - ruleName = ruleName, - ruleBookUrl = ruleBookUrl, - ruleAuthor = ruleAuthor, - ruleCoverUrl = ruleCoverUrl, - ruleIntro = ruleIntro, - ruleKind = ruleKind, - ruleLastChapter = ruleLastChapter, - ruleWordCount = ruleWordCount - ) - }?.let { searchBook -> + getSearchItem( + item, + analyzeRule, + bookSource, + baseUrl, + index == 0, + ruleName = ruleName, + ruleBookUrl = ruleBookUrl, + ruleAuthor = ruleAuthor, + ruleCoverUrl = ruleCoverUrl, + ruleIntro = ruleIntro, + ruleKind = ruleKind, + ruleLastChapter = ruleLastChapter, + ruleWordCount = ruleWordCount + )?.let { searchBook -> if (baseUrl == searchBook.bookUrl) { searchBook.infoHtml = body } @@ -215,47 +203,4 @@ object BookList { return null } - private fun getSearchItemAllInOne( - item: NativeObject, - bookSource: BookSource, - bookListRule: BookListRule, - baseUrl: String, - printLog: Boolean - ): SearchBook? { - val searchBook = SearchBook() - searchBook.origin = bookSource.bookSourceUrl - searchBook.originName = bookSource.bookSourceName - searchBook.originOrder = bookSource.customOrder - SourceDebug.printLog(bookSource.bookSourceUrl, "获取书名", printLog) - searchBook.name = item[bookListRule.name]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.name, printLog) - if (searchBook.name.isNotEmpty()) { - SourceDebug.printLog(bookSource.bookSourceUrl, "获取书籍Url", printLog) - searchBook.bookUrl = item[bookListRule.bookUrl]?.toString() ?: "" - if (searchBook.bookUrl.isEmpty()) { - searchBook.bookUrl = baseUrl - } - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.bookUrl, printLog) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取作者", printLog) - searchBook.author = BookHelp.formatAuthor(item[bookListRule.author]?.toString() ?: "") - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.author, printLog) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取分类", printLog) - searchBook.kind = item[bookListRule.kind]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.kind, printLog) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取简介", printLog) - searchBook.intro = item[bookListRule.intro]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.intro, printLog, true) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取字数", printLog) - searchBook.wordCount = item[bookListRule.wordCount]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.wordCount, printLog) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取封面Url", printLog) - searchBook.coverUrl = item[bookListRule.coverUrl]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.coverUrl, printLog) - SourceDebug.printLog(bookSource.bookSourceUrl, "获取最新章节", printLog) - searchBook.latestChapterTitle = item[bookListRule.lastChapter]?.toString() ?: "" - SourceDebug.printLog(bookSource.bookSourceUrl, searchBook.latestChapterTitle, printLog) - return searchBook - } - return null - } } \ No newline at end of file