添加多页正文并发访问限制

pull/2481/head
kunfei 2 years ago
parent 118be1d2e4
commit d876fd414e
  1. 40
      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.BookChapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.ContentRule 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.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.http.StrResponse
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
@ -16,6 +18,7 @@ import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
@ -68,15 +71,28 @@ object BookContent {
) break ) break
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
coroutineContext.ensureActive() coroutineContext.ensureActive()
val res = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
mUrl = nextUrl, mUrl = nextUrl,
source = bookSource, source = bookSource,
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() 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( contentData = analyzeContent(
book, nextUrl, res.url, nextBody, contentRule, book, nextUrl, res!!.url, nextBody, contentRule,
bookChapter, bookSource, mNextChapterUrl, false bookChapter, bookSource, mNextChapterUrl, false
) )
nextUrl = nextUrl =
@ -97,9 +113,21 @@ object BookContent {
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() 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( analyzeContent(
book, urlStr, res.url, res.body!!, contentRule, book, urlStr, res!!.url, res!!.body!!, contentRule,
bookChapter, bookSource, mNextChapterUrl, false bookChapter, bookSource, mNextChapterUrl, false
).first ).first
} }

Loading…
Cancel
Save