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.ContentEmptyException
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.book.BookHelp 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.Debug
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.HtmlFormatter import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.mapAsync
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.apache.commons.text.StringEscapeUtils import org.apache.commons.text.StringEscapeUtils
import splitties.init.appCtx import splitties.init.appCtx
@ -105,26 +108,26 @@ object BookContent {
} else if (contentData.second.size > 1) { } else if (contentData.second.size > 1) {
Debug.log(bookSource.bookSourceUrl, "◇并发解析正文,总页数:${contentData.second.size}") Debug.log(bookSource.bookSourceUrl, "◇并发解析正文,总页数:${contentData.second.size}")
withContext(IO) { withContext(IO) {
val asyncArray = Array(contentData.second.size) { flow {
async(IO) { for (urlStr in contentData.second) {
val urlStr = contentData.second[it] emit(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
} }
} }.mapAsync(AppConfig.threadCount) { urlStr ->
asyncArray.forEach { coroutine -> val res = AnalyzeUrl(
coroutineContext.ensureActive() mUrl = urlStr,
contentList.add(coroutine.await()) 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)
} }
} }
} }