This commit is contained in:
Horis 2024-01-31 18:31:17 +08:00
parent 8e7e82b213
commit ae1558c029

View File

@ -9,14 +9,17 @@ import io.legado.app.data.entities.rule.ContentRule
import io.legado.app.exception.ContentEmptyException
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.book.BookHelp
import io.legado.app.help.config.AppConfig
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.mapAsync
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import org.apache.commons.text.StringEscapeUtils
import splitties.init.appCtx
@ -105,26 +108,26 @@ object BookContent {
} else if (contentData.second.size > 1) {
Debug.log(bookSource.bookSourceUrl, "◇并发解析正文,总页数:${contentData.second.size}")
withContext(IO) {
val asyncArray = Array(contentData.second.size) {
async(IO) {
val urlStr = contentData.second[it]
val res = AnalyzeUrl(
mUrl = urlStr,
source = bookSource,
ruleData = book,
headerMapF = bookSource.getHeaderMap()
).getStrResponseAwait() //控制并发访问
analyzeContent(
book, urlStr, res.url, res.body!!, contentRule,
bookChapter, bookSource, mNextChapterUrl,
getNextPageUrl = false,
printLog = false
).first
flow {
for (urlStr in contentData.second) {
emit(urlStr)
}
}
asyncArray.forEach { coroutine ->
coroutineContext.ensureActive()
contentList.add(coroutine.await())
}.mapAsync(AppConfig.threadCount) { urlStr ->
val res = AnalyzeUrl(
mUrl = urlStr,
source = bookSource,
ruleData = book,
headerMapF = bookSource.getHeaderMap()
).getStrResponseAwait() //控制并发访问
analyzeContent(
book, urlStr, res.url, res.body!!, contentRule,
bookChapter, bookSource, mNextChapterUrl,
getNextPageUrl = false,
printLog = false
).first
}.collect {
currentCoroutineContext().ensureActive()
contentList.add(it)
}
}
}