From d876fd414e5de55c9b184d667cbca4492be9a266 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 25 Oct 2022 10:34:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E9=A1=B5=E6=AD=A3?= =?UTF-8?q?=E6=96=87=E5=B9=B6=E5=8F=91=E8=AE=BF=E9=97=AE=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/webBook/BookContent.kt | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index fc1e85806..f9dd4f50c 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -6,9 +6,11 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.ContentRule +import io.legado.app.exception.ConcurrentException import io.legado.app.exception.ContentEmptyException import io.legado.app.exception.NoStackTraceException import io.legado.app.help.book.BookHelp +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -16,6 +18,7 @@ import io.legado.app.utils.HtmlFormatter import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.async +import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive import kotlinx.coroutines.withContext import splitties.init.appCtx @@ -68,15 +71,28 @@ object BookContent { ) break nextUrlList.add(nextUrl) coroutineContext.ensureActive() - val res = AnalyzeUrl( + val analyzeUrl = AnalyzeUrl( mUrl = nextUrl, source = bookSource, ruleData = book, headerMapF = bookSource.getHeaderMap() - ).getStrResponseAwait() - res.body?.let { nextBody -> + ) + var res: StrResponse? = null + var isConcurrent: Boolean + do { + //控制并发访问 + isConcurrent = false + try { + res = analyzeUrl.getStrResponseAwait() + } catch (e: ConcurrentException) { + isConcurrent = true + //如果是并发限制等待再次访问 + delay(e.waitTime.toLong()) + } + } while (!isConcurrent) + res!!.body?.let { nextBody -> contentData = analyzeContent( - book, nextUrl, res.url, nextBody, contentRule, + book, nextUrl, res!!.url, nextBody, contentRule, bookChapter, bookSource, mNextChapterUrl, false ) nextUrl = @@ -97,9 +113,21 @@ object BookContent { ruleData = book, headerMapF = bookSource.getHeaderMap() ) - val res = analyzeUrl.getStrResponseAwait() + var res: StrResponse? = null + var isConcurrent: Boolean + do { + //控制并发访问 + isConcurrent = false + try { + res = analyzeUrl.getStrResponseAwait() + } catch (e: ConcurrentException) { + isConcurrent = true + //如果是并发限制等待再次访问 + delay(e.waitTime.toLong()) + } + } while (!isConcurrent) analyzeContent( - book, urlStr, res.url, res.body!!, contentRule, + book, urlStr, res!!.url, res!!.body!!, contentRule, bookChapter, bookSource, mNextChapterUrl, false ).first }