pull/32/head
kunfei 5 years ago
parent e7a3016439
commit bedc0e7812
  1. 6
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  2. 34
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  3. 10
      app/src/main/java/io/legado/app/model/webbook/BookList.kt

@ -88,7 +88,7 @@ data class BookSource(
return exploreRuleV!! return exploreRuleV!!
} }
fun getBookInfoRule(): BookInfoRule? { fun getBookInfoRule(): BookInfoRule {
bookInfoRuleV ?: let { bookInfoRuleV ?: let {
bookInfoRuleV = GSON.fromJsonObject<BookInfoRule>(ruleBookInfo) bookInfoRuleV = GSON.fromJsonObject<BookInfoRule>(ruleBookInfo)
bookInfoRuleV ?: let { bookInfoRuleV = BookInfoRule() } bookInfoRuleV ?: let { bookInfoRuleV = BookInfoRule() }
@ -96,7 +96,7 @@ data class BookSource(
return bookInfoRuleV!! return bookInfoRuleV!!
} }
fun getTocRule(): TocRule? { fun getTocRule(): TocRule {
tocRuleV ?: let { tocRuleV ?: let {
tocRuleV = GSON.fromJsonObject<TocRule>(ruleToc) tocRuleV = GSON.fromJsonObject<TocRule>(ruleToc)
tocRuleV ?: let { tocRuleV = TocRule() } tocRuleV ?: let { tocRuleV = TocRule() }
@ -104,7 +104,7 @@ data class BookSource(
return tocRuleV!! return tocRuleV!!
} }
fun getContentRule(): ContentRule? { fun getContentRule(): ContentRule {
contentRuleV ?: let { contentRuleV ?: let {
contentRuleV = GSON.fromJsonObject<ContentRule>(ruleContent) contentRuleV = GSON.fromJsonObject<ContentRule>(ruleContent)
contentRuleV ?: let { contentRuleV = ContentRule() } contentRuleV ?: let { contentRuleV = ContentRule() }

@ -19,7 +19,7 @@ import javax.script.SimpleBindings
*/ */
@Keep @Keep
class AnalyzeRule(private var book: BaseBook? = null) { class AnalyzeRule(private var book: BaseBook? = null) {
private var `object`: Any? = null private var content: Any? = null
private var isJSON: Boolean = false private var isJSON: Boolean = false
private var baseUrl: String? = null private var baseUrl: String? = null
@ -36,10 +36,10 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
@JvmOverloads @JvmOverloads
fun setContent(body: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { fun setContent(content: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule {
if (body == null) throw AssertionError("Content cannot be null") if (content == null) throw AssertionError("Content cannot be null")
isJSON = body.toString().isJson() isJSON = content.toString().isJson()
`object` = body this.content = content
this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0)
objectChangedXP = true objectChangedXP = true
objectChangedJS = true objectChangedJS = true
@ -51,7 +51,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
* 获取XPath解析类 * 获取XPath解析类
*/ */
private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath {
return if (o != `object`) { return if (o != content) {
AnalyzeByXPath().parse(o) AnalyzeByXPath().parse(o)
} else getAnalyzeByXPath() } else getAnalyzeByXPath()
} }
@ -59,7 +59,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
private fun getAnalyzeByXPath(): AnalyzeByXPath { private fun getAnalyzeByXPath(): AnalyzeByXPath {
if (analyzeByXPath == null || objectChangedXP) { if (analyzeByXPath == null || objectChangedXP) {
analyzeByXPath = AnalyzeByXPath() analyzeByXPath = AnalyzeByXPath()
analyzeByXPath!!.parse(`object`!!) analyzeByXPath!!.parse(content!!)
objectChangedXP = false objectChangedXP = false
} }
return analyzeByXPath as AnalyzeByXPath return analyzeByXPath as AnalyzeByXPath
@ -69,7 +69,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
* 获取JSOUP解析类 * 获取JSOUP解析类
*/ */
private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup {
return if (o != `object`) { return if (o != content) {
AnalyzeByJSoup().parse(o) AnalyzeByJSoup().parse(o)
} else getAnalyzeByJSoup() } else getAnalyzeByJSoup()
} }
@ -77,7 +77,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
private fun getAnalyzeByJSoup(): AnalyzeByJSoup { private fun getAnalyzeByJSoup(): AnalyzeByJSoup {
if (analyzeByJSoup == null || objectChangedJS) { if (analyzeByJSoup == null || objectChangedJS) {
analyzeByJSoup = AnalyzeByJSoup() analyzeByJSoup = AnalyzeByJSoup()
analyzeByJSoup!!.parse(`object`!!) analyzeByJSoup!!.parse(content!!)
objectChangedJS = false objectChangedJS = false
} }
return analyzeByJSoup as AnalyzeByJSoup return analyzeByJSoup as AnalyzeByJSoup
@ -87,7 +87,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
* 获取JSON解析类 * 获取JSON解析类
*/ */
private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath {
return if (o != `object`) { return if (o != content) {
AnalyzeByJSonPath().parse(o) AnalyzeByJSonPath().parse(o)
} else getAnalyzeByJSonPath() } else getAnalyzeByJSonPath()
} }
@ -95,7 +95,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
private fun getAnalyzeByJSonPath(): AnalyzeByJSonPath { private fun getAnalyzeByJSonPath(): AnalyzeByJSonPath {
if (analyzeByJSonPath == null || objectChangedJP) { if (analyzeByJSonPath == null || objectChangedJP) {
analyzeByJSonPath = AnalyzeByJSonPath() analyzeByJSonPath = AnalyzeByJSonPath()
analyzeByJSonPath!!.parse(`object`!!) analyzeByJSonPath!!.parse(content!!)
objectChangedJP = false objectChangedJP = false
} }
return analyzeByJSonPath as AnalyzeByJSonPath return analyzeByJSonPath as AnalyzeByJSonPath
@ -116,7 +116,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
@Throws(Exception::class) @Throws(Exception::class)
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? { fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? {
var result: Any? = null var result: Any? = null
`object`?.let { o -> content?.let { o ->
if (ruleList.isNotEmpty()) { if (ruleList.isNotEmpty()) {
if (ruleList.isNotEmpty()) result = o if (ruleList.isNotEmpty()) result = o
for (rule in ruleList) { for (rule in ruleList) {
@ -184,7 +184,7 @@ 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
`object`?.let { o -> content?.let { o ->
if (ruleList.isNotEmpty()) result = o if (ruleList.isNotEmpty()) result = o
for (rule in ruleList) { for (rule in ruleList) {
result?.let { result?.let {
@ -223,7 +223,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
if (TextUtils.isEmpty(ruleStr)) return null if (TextUtils.isEmpty(ruleStr)) return null
var result: Any? = null var result: Any? = null
val ruleList = splitSourceRule(ruleStr) val ruleList = splitSourceRule(ruleStr)
`object`?.let { o -> content?.let { o ->
if (ruleList.isNotEmpty()) result = o if (ruleList.isNotEmpty()) result = o
for (rule in ruleList) { for (rule in ruleList) {
result?.let { result?.let {
@ -250,13 +250,13 @@ class AnalyzeRule(private var book: BaseBook? = null) {
fun getElements(ruleStr: String): List<Any> { fun getElements(ruleStr: String): List<Any> {
var result: Any? = null var result: Any? = null
val ruleList = splitSourceRule(ruleStr) val ruleList = splitSourceRule(ruleStr)
`object`?.let { o -> content?.let { o ->
if (ruleList.isNotEmpty()) result = o if (ruleList.isNotEmpty()) result = o
for (rule in ruleList) { for (rule in ruleList) {
result?.let { result?.let {
when (rule.mode) { when (rule.mode) {
Mode.Js -> { Mode.Js -> {
if (result == null) result = `object` if (result == null) result = content
result = evalJS(rule.rule, result) result = evalJS(rule.rule, result)
} }
Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule)
@ -352,7 +352,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
val sb = StringBuffer(ruleStr.length) val sb = StringBuffer(ruleStr.length)
val expMatcher = EXP_PATTERN.matcher(ruleStr) val expMatcher = EXP_PATTERN.matcher(ruleStr)
while (expMatcher.find()) { while (expMatcher.find()) {
jsEval = evalJS(expMatcher.group(1), `object`) jsEval = evalJS(expMatcher.group(1), content)
if (jsEval is String) { if (jsEval is String) {
expMatcher.appendReplacement(sb, jsEval) expMatcher.appendReplacement(sb, jsEval)
} else if (jsEval is Double && jsEval % 1.0 == 0.0) { } else if (jsEval is Double && jsEval % 1.0 == 0.0) {

@ -18,7 +18,7 @@ class BookList {
analyzeUrl: AnalyzeUrl, analyzeUrl: AnalyzeUrl,
isSearch: Boolean = true isSearch: Boolean = true
): ArrayList<SearchBook> { ): ArrayList<SearchBook> {
var bookList = ArrayList<SearchBook>() val bookList = ArrayList<SearchBook>()
val baseUrl: String = NetworkUtils.getUrl(response) val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body() val body: String? = response.body()
body ?: throw Exception( body ?: throw Exception(
@ -31,7 +31,7 @@ class BookList {
analyzer.setContent(body, baseUrl) analyzer.setContent(body, baseUrl)
bookSource.bookUrlPattern?.let { bookSource.bookUrlPattern?.let {
if (baseUrl.matches(it.toRegex())) { if (baseUrl.matches(it.toRegex())) {
getItem()?.let { searchBook -> getItem(analyzer, bookSource)?.let { searchBook ->
searchBook.bookInfoHtml = body searchBook.bookInfoHtml = body
bookList.add(searchBook) bookList.add(searchBook)
} }
@ -41,8 +41,12 @@ class BookList {
return bookList return bookList
} }
fun getItem(): SearchBook? { private fun getItem(analyzeRule: AnalyzeRule, bookSource: BookSource): SearchBook? {
val searchBook = SearchBook() val searchBook = SearchBook()
analyzeRule.setBook(searchBook)
bookSource.getBookInfoRule().init?.let {
analyzeRule.setContent(analyzeRule.getElement(it))
}
return null return null
} }

Loading…
Cancel
Save