优化
This commit is contained in:
@@ -21,6 +21,7 @@ import io.legado.app.utils.isJson
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import io.legado.app.utils.splitNotBlank
|
||||
import io.legado.app.utils.stackTraceStr
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.apache.commons.text.StringEscapeUtils
|
||||
@@ -31,6 +32,7 @@ import java.util.regex.Pattern
|
||||
import kotlin.collections.component1
|
||||
import kotlin.collections.component2
|
||||
import kotlin.collections.set
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* 解析规则获取结果
|
||||
@@ -65,6 +67,8 @@ class AnalyzeRule(
|
||||
|
||||
private val stringRuleCache = hashMapOf<String, List<SourceRule>>()
|
||||
|
||||
private var coroutineContext: CoroutineContext? = null
|
||||
|
||||
@JvmOverloads
|
||||
fun setContent(content: Any?, baseUrl: String? = null): AnalyzeRule {
|
||||
if (content == null) throw AssertionError("内容不可空(Content cannot be null)")
|
||||
@@ -80,6 +84,11 @@ class AnalyzeRule(
|
||||
return this
|
||||
}
|
||||
|
||||
fun setCoroutineContext(context: CoroutineContext?): AnalyzeRule {
|
||||
coroutineContext = context
|
||||
return this
|
||||
}
|
||||
|
||||
fun setBaseUrl(baseUrl: String?): AnalyzeRule {
|
||||
baseUrl?.let {
|
||||
this.baseUrl = baseUrl
|
||||
@@ -752,7 +761,7 @@ class AnalyzeRule(
|
||||
source?.getShareScope()?.let {
|
||||
scope.prototype = it
|
||||
}
|
||||
return RhinoScriptEngine.eval(jsStr, scope)
|
||||
return RhinoScriptEngine.eval(jsStr, scope, coroutineContext)
|
||||
}
|
||||
|
||||
override fun getSource(): BaseSource? {
|
||||
|
||||
@@ -168,6 +168,7 @@ object BookChapterList {
|
||||
val analyzeRule = AnalyzeRule(book, bookSource)
|
||||
analyzeRule.setContent(body).setBaseUrl(baseUrl)
|
||||
analyzeRule.setRedirectUrl(redirectUrl)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
//获取目录列表
|
||||
val chapterList = arrayListOf<BookChapter>()
|
||||
Debug.log(bookSource.bookSourceUrl, "┌获取目录列表", log)
|
||||
|
||||
@@ -57,6 +57,7 @@ object BookContent {
|
||||
val analyzeRule = AnalyzeRule(book, bookSource)
|
||||
analyzeRule.setContent(body, baseUrl)
|
||||
analyzeRule.setRedirectUrl(redirectUrl)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
analyzeRule.chapter = bookChapter
|
||||
analyzeRule.nextChapterUrl = mNextChapterUrl
|
||||
coroutineContext.ensureActive()
|
||||
@@ -150,7 +151,7 @@ object BookContent {
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun analyzeContent(
|
||||
private suspend fun analyzeContent(
|
||||
book: Book,
|
||||
baseUrl: String,
|
||||
redirectUrl: String,
|
||||
@@ -164,6 +165,7 @@ object BookContent {
|
||||
): Pair<String, List<String>> {
|
||||
val analyzeRule = AnalyzeRule(book, bookSource)
|
||||
analyzeRule.setContent(body, baseUrl)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
val rUrl = analyzeRule.setRedirectUrl(redirectUrl)
|
||||
analyzeRule.nextChapterUrl = nextChapterUrl
|
||||
val nextUrlList = arrayListOf<String>()
|
||||
|
||||
@@ -40,6 +40,7 @@ object BookInfo {
|
||||
val analyzeRule = AnalyzeRule(book, bookSource)
|
||||
analyzeRule.setContent(body).setBaseUrl(baseUrl)
|
||||
analyzeRule.setRedirectUrl(redirectUrl)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
analyzeBookInfo(book, body, analyzeRule, bookSource, baseUrl, redirectUrl, canReName)
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ object BookList {
|
||||
val analyzeRule = AnalyzeRule(ruleData, bookSource)
|
||||
analyzeRule.setContent(body).setBaseUrl(baseUrl)
|
||||
analyzeRule.setRedirectUrl(baseUrl)
|
||||
analyzeRule.setCoroutineContext(coroutineContext)
|
||||
if (isSearch) bookSource.bookUrlPattern?.let {
|
||||
coroutineContext.ensureActive()
|
||||
if (baseUrl.matches(it.toRegex())) {
|
||||
|
||||
@@ -18,6 +18,7 @@ import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
object WebBook {
|
||||
@@ -196,12 +197,14 @@ object WebBook {
|
||||
}
|
||||
}
|
||||
|
||||
fun runPreUpdateJs(bookSource: BookSource, book: Book): Result<Boolean> {
|
||||
suspend fun runPreUpdateJs(bookSource: BookSource, book: Book): Result<Boolean> {
|
||||
return kotlin.runCatching {
|
||||
val preUpdateJs = bookSource.ruleToc?.preUpdateJs
|
||||
if (!preUpdateJs.isNullOrBlank()) {
|
||||
kotlin.runCatching {
|
||||
AnalyzeRule(book, bookSource).evalJS(preUpdateJs)
|
||||
AnalyzeRule(book, bookSource)
|
||||
.setCoroutineContext(coroutineContext)
|
||||
.evalJS(preUpdateJs)
|
||||
}.onFailure {
|
||||
AppLog.put("执行preUpdateJs规则失败 书源:${bookSource.bookSourceName}", it)
|
||||
throw it
|
||||
|
||||
Reference in New Issue
Block a user