Horis 4 жил өмнө
parent
commit
d3e756efa7

+ 19 - 0
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@@ -20,6 +20,7 @@ import io.legado.app.help.config.AppConfig
 import io.legado.app.help.glide.GlideHeaders
 import io.legado.app.help.http.*
 import io.legado.app.utils.*
+import kotlinx.coroutines.delay
 import kotlinx.coroutines.runBlocking
 import okhttp3.MediaType.Companion.toMediaType
 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
     @Throws(ConcurrentException::class)
     fun getStrResponse(

+ 7 - 33
app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt

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

+ 7 - 33
app/src/main/java/io/legado/app/model/webBook/BookContent.kt

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