pull/38/head
kunfei 5 years ago
parent ff5db43db4
commit 213b3e772d
  1. 79
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  2. 85
      app/src/main/java/io/legado/app/model/webbook/BookList.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.constant.Pattern.JS_PATTERN
import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BaseBook
import io.legado.app.utils.* import io.legado.app.utils.*
import org.mozilla.javascript.NativeObject
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import javax.script.SimpleBindings import javax.script.SimpleBindings
@ -185,27 +186,32 @@ class AnalyzeRule(private var book: BaseBook? = null) {
@JvmOverloads @JvmOverloads
fun getString(ruleList: List<SourceRule>, isUrl: Boolean = false): String { fun getString(ruleList: List<SourceRule>, isUrl: Boolean = false): String {
var result: Any? = null var result: Any? = null
content?.let { o -> val content = this.content
if (ruleList.isNotEmpty()) result = o if (content != null && ruleList.isNotEmpty()) {
for (sourceRule in ruleList) { result = content
putRule(sourceRule.putMap) if (content is NativeObject) {
sourceRule.makeUpRule(result) result = content[ruleList[0].rule]?.toString()
result?.let { } else {
if (sourceRule.rule.isNotBlank()) { for (sourceRule in ruleList) {
result = when (sourceRule.mode) { putRule(sourceRule.putMap)
Mode.Js -> evalJS(sourceRule.rule, it) sourceRule.makeUpRule(result)
Mode.Json -> getAnalyzeByJSonPath(it).getString(sourceRule.rule) result?.let {
Mode.XPath -> getAnalyzeByXPath(it).getString(sourceRule.rule) if (sourceRule.rule.isNotBlank()) {
Mode.Default -> if (isUrl) { result = when (sourceRule.mode) {
getAnalyzeByJSoup(it).getString0(sourceRule.rule) Mode.Js -> evalJS(sourceRule.rule, it)
} else { Mode.Json -> getAnalyzeByJSonPath(it).getString(sourceRule.rule)
getAnalyzeByJSoup(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()) {
if (sourceRule.replaceRegex.isNotEmpty()) { result = replaceRegex(result.toString(), sourceRule)
result = replaceRegex(result.toString(), sourceRule) }
} }
} }
} }
@ -231,7 +237,10 @@ class AnalyzeRule(private var book: BaseBook? = null) {
putRule(sourceRule.putMap) putRule(sourceRule.putMap)
result?.let { result?.let {
result = when (sourceRule.mode) { 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.Js -> evalJS(sourceRule.rule, it)
Mode.Json -> getAnalyzeByJSonPath(it).getObject(sourceRule.rule) Mode.Json -> getAnalyzeByJSonPath(it).getObject(sourceRule.rule)
Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule)
@ -260,7 +269,10 @@ class AnalyzeRule(private var book: BaseBook? = null) {
putRule(sourceRule.putMap) putRule(sourceRule.putMap)
result?.let { result?.let {
result = when (sourceRule.mode) { 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.Js -> evalJS(sourceRule.rule, result)
Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule) Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule)
Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule)
@ -331,32 +343,28 @@ class AnalyzeRule(private var book: BaseBook? = null) {
* 分解规则生成规则列表 * 分解规则生成规则列表
*/ */
@Throws(Exception::class) @Throws(Exception::class)
fun splitSourceRule(ruleStr: String): List<SourceRule> { fun splitSourceRule(ruleStr: String, mode: Mode = Mode.Default): List<SourceRule> {
var vRuleStr = ruleStr var vRuleStr = ruleStr
val ruleList = ArrayList<SourceRule>() val ruleList = ArrayList<SourceRule>()
if (TextUtils.isEmpty(vRuleStr)) return ruleList if (TextUtils.isEmpty(vRuleStr)) return ruleList
//检测Mode //检测Mode
val mode: Mode var mMode: Mode = mode
when { when {
vRuleStr.startsWith("@XPath:", true) -> { vRuleStr.startsWith("@XPath:", true) -> {
mode = Mode.XPath mMode = Mode.XPath
vRuleStr = vRuleStr.substring(7) vRuleStr = vRuleStr.substring(7)
} }
vRuleStr.startsWith("@Json:", true) -> { vRuleStr.startsWith("@Json:", true) -> {
mode = Mode.Json mMode = Mode.Json
vRuleStr = vRuleStr.substring(6) vRuleStr = vRuleStr.substring(6)
} }
vRuleStr.startsWith(":") -> { vRuleStr.startsWith(":") -> {
mode = Mode.Regex mMode = Mode.Regex
isRegex = true isRegex = true
vRuleStr = vRuleStr.substring(1) vRuleStr = vRuleStr.substring(1)
} }
else -> mode = isRegex -> mMode = Mode.Regex
when { isJSON -> mMode = Mode.Json
isRegex -> Mode.Regex
isJSON -> Mode.Json
else -> Mode.Default
}
} }
//拆分为规则列表 //拆分为规则列表
var start = 0 var start = 0
@ -364,9 +372,10 @@ class AnalyzeRule(private var book: BaseBook? = null) {
val jsMatcher = JS_PATTERN.matcher(vRuleStr) val jsMatcher = JS_PATTERN.matcher(vRuleStr)
while (jsMatcher.find()) { while (jsMatcher.find()) {
if (jsMatcher.start() > start) { 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)) { if (!TextUtils.isEmpty(tmp)) {
ruleList.add(SourceRule(tmp, mode)) ruleList.add(SourceRule(tmp, mMode))
} }
} }
ruleList.add(SourceRule(jsMatcher.group(), Mode.Js)) ruleList.add(SourceRule(jsMatcher.group(), Mode.Js))
@ -375,7 +384,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
if (vRuleStr.length > start) { if (vRuleStr.length > start) {
tmp = vRuleStr.substring(start).replace("\n", "").trim { it <= ' ' } tmp = vRuleStr.substring(start).replace("\n", "").trim { it <= ' ' }
if (!TextUtils.isEmpty(tmp)) { if (!TextUtils.isEmpty(tmp)) {
ruleList.add(SourceRule(tmp, mode)) ruleList.add(SourceRule(tmp, mMode))
} }
} }
return ruleList return ruleList

@ -4,12 +4,10 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook 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.help.BookHelp
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import org.mozilla.javascript.NativeObject
import retrofit2.Response import retrofit2.Response
object BookList { object BookList {
@ -77,31 +75,21 @@ object BookList {
val ruleWordCount = analyzeRule.splitSourceRule(bookListRule.wordCount ?: "") val ruleWordCount = analyzeRule.splitSourceRule(bookListRule.wordCount ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, "列表数为${collections.size}") SourceDebug.printLog(bookSource.bookSourceUrl, "列表数为${collections.size}")
for ((index, item) in collections.withIndex()) { for ((index, item) in collections.withIndex()) {
if (item is NativeObject) { getSearchItem(
getSearchItemAllInOne( item,
item, analyzeRule,
bookSource, bookSource,
bookListRule, baseUrl,
baseUrl, index == 0,
index == 0 ruleName = ruleName,
) ruleBookUrl = ruleBookUrl,
} else { ruleAuthor = ruleAuthor,
getSearchItem( ruleCoverUrl = ruleCoverUrl,
item, ruleIntro = ruleIntro,
analyzeRule, ruleKind = ruleKind,
bookSource, ruleLastChapter = ruleLastChapter,
baseUrl, ruleWordCount = ruleWordCount
index == 0, )?.let { searchBook ->
ruleName = ruleName,
ruleBookUrl = ruleBookUrl,
ruleAuthor = ruleAuthor,
ruleCoverUrl = ruleCoverUrl,
ruleIntro = ruleIntro,
ruleKind = ruleKind,
ruleLastChapter = ruleLastChapter,
ruleWordCount = ruleWordCount
)
}?.let { searchBook ->
if (baseUrl == searchBook.bookUrl) { if (baseUrl == searchBook.bookUrl) {
searchBook.infoHtml = body searchBook.infoHtml = body
} }
@ -215,47 +203,4 @@ object BookList {
return null 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
}
} }
Loading…
Cancel
Save