Merge remote-tracking branch 'origin/master'

pull/2492/head
kunfei 2 years ago
commit dad7dd6bd5
  1. 19
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  2. 40
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  3. 40
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt

@ -20,6 +20,7 @@ import io.legado.app.help.config.AppConfig
import io.legado.app.help.glide.GlideHeaders import io.legado.app.help.glide.GlideHeaders
import io.legado.app.help.http.* import io.legado.app.help.http.*
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
@ -420,6 +421,24 @@ class AnalyzeUrl(
} }
} }
/**
* 访问网站,返回StrResponse
* 并发异常自动重试
*/
suspend fun getStrResponseConcurrentAwait(
jsStr: String? = null,
sourceRegex: String? = null,
useWebView: Boolean = true,
): StrResponse {
while (true) {
try {
return getStrResponseAwait(jsStr, sourceRegex, useWebView)
} catch (e: ConcurrentException) {
delay(e.waitTime.toLong())
}
}
}
@JvmOverloads @JvmOverloads
@Throws(ConcurrentException::class) @Throws(ConcurrentException::class)
fun getStrResponse( fun getStrResponse(

@ -64,26 +64,13 @@ object BookChapterList {
var nextUrl = chapterData.second[0] var nextUrl = chapterData.second[0]
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
val analyzeUrl = AnalyzeUrl( val res = AnalyzeUrl(
mUrl = nextUrl, mUrl = nextUrl,
source = bookSource, source = bookSource,
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) ).getStrResponseConcurrentAwait() //控制并发访问
var res: StrResponse? = null res.body?.let { nextBody ->
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 ->
chapterData = analyzeChapterList( chapterData = analyzeChapterList(
book, nextUrl, nextUrl, book, nextUrl, nextUrl,
nextBody, tocRule, listRule, bookSource nextBody, tocRule, listRule, bookSource
@ -100,28 +87,15 @@ object BookChapterList {
val asyncArray = Array(chapterData.second.size) { val asyncArray = Array(chapterData.second.size) {
async(IO) { async(IO) {
val urlStr = chapterData.second[it] val urlStr = chapterData.second[it]
val analyzeUrl = AnalyzeUrl( val res = AnalyzeUrl(
mUrl = urlStr, mUrl = urlStr,
source = bookSource, source = bookSource,
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) ).getStrResponseConcurrentAwait() //控制并发访问
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)
analyzeChapterList( analyzeChapterList(
book, urlStr, res!!.url, book, urlStr, res.url,
res!!.body!!, tocRule, listRule, bookSource, false res.body!!, tocRule, listRule, bookSource, false
).first ).first
} }
} }

@ -71,28 +71,15 @@ object BookContent {
) break ) break
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
coroutineContext.ensureActive() coroutineContext.ensureActive()
val analyzeUrl = AnalyzeUrl( val res = AnalyzeUrl(
mUrl = nextUrl, mUrl = nextUrl,
source = bookSource, source = bookSource,
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) ).getStrResponseConcurrentAwait() //控制并发访问
var res: StrResponse? = null res.body?.let { nextBody ->
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 =
@ -107,27 +94,14 @@ object BookContent {
val asyncArray = Array(contentData.second.size) { val asyncArray = Array(contentData.second.size) {
async(IO) { async(IO) {
val urlStr = contentData.second[it] val urlStr = contentData.second[it]
val analyzeUrl = AnalyzeUrl( val res = AnalyzeUrl(
mUrl = urlStr, mUrl = urlStr,
source = bookSource, source = bookSource,
ruleData = book, ruleData = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) ).getStrResponseConcurrentAwait() //控制并发访问
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