pull/38/head
kunfei 5 years ago
parent ff5db43db4
commit 213b3e772d
  1. 45
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  2. 57
      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,8 +186,12 @@ 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()) {
result = content
if (content is NativeObject) {
result = content[ruleList[0].rule]?.toString()
} else {
for (sourceRule in ruleList) { for (sourceRule in ruleList) {
putRule(sourceRule.putMap) putRule(sourceRule.putMap)
sourceRule.makeUpRule(result) sourceRule.makeUpRule(result)
@ -210,6 +215,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
} }
} }
}
if (result == null) result = "" if (result == null) result = ""
if (isUrl) { if (isUrl) {
return NetworkUtils.getAbsoluteURL(baseUrl, result.toString()) ?: "" return NetworkUtils.getAbsoluteURL(baseUrl, result.toString()) ?: ""
@ -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,15 +75,6 @@ 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) {
getSearchItemAllInOne(
item,
bookSource,
bookListRule,
baseUrl,
index == 0
)
} else {
getSearchItem( getSearchItem(
item, item,
analyzeRule, analyzeRule,
@ -100,8 +89,7 @@ object BookList {
ruleKind = ruleKind, ruleKind = ruleKind,
ruleLastChapter = ruleLastChapter, ruleLastChapter = ruleLastChapter,
ruleWordCount = ruleWordCount ruleWordCount = ruleWordCount
) )?.let { searchBook ->
}?.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