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 }