diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 725cf82bb..69734fbdd 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -12,7 +12,6 @@ import io.legado.app.model.webBook.BookInfo import io.legado.app.model.webBook.BookList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking import kotlin.coroutines.CoroutineContext class WebBook(val bookSource: BookSource) { @@ -140,75 +139,50 @@ class WebBook(val bookSource: BookSource) { context: CoroutineContext = Dispatchers.IO ): Coroutine { return Coroutine.async(scope, context) { - if (bookSource.getContentRule().content.isNullOrEmpty()) { - Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}") - return@async bookChapter.url - } - val body = - if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) { - book.tocHtml - } else { - val analyzeUrl = - AnalyzeUrl( - book = book, - ruleUrl = bookChapter.url, - baseUrl = book.tocUrl, - headerMapF = bookSource.getHeaderMap() - ) - analyzeUrl.getResponseAwait( - bookSource.bookSourceUrl, - jsStr = bookSource.getContentRule().webJs, - sourceRegex = bookSource.getContentRule().sourceRegex - ).body - } - BookContent.analyzeContent( - this, - body, - book, - bookChapter, - bookSource, - bookChapter.url, - nextChapterUrl + getContentSuspend( + book, bookChapter, nextChapterUrl, scope ) } } - fun getContentBlocking( + /** + * 章节内容 + */ + suspend fun getContentSuspend( book: Book, bookChapter: BookChapter, - nextChapterUrl: String? = null + nextChapterUrl: String? = null, + scope: CoroutineScope = Coroutine.DEFAULT ): String { - return runBlocking { - if (bookSource.getContentRule().content.isNullOrEmpty()) { - Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}") - return@runBlocking bookChapter.url - } - val body = - if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) { - book.tocHtml - } else { - val analyzeUrl = - AnalyzeUrl( - book = book, - ruleUrl = bookChapter.url, - baseUrl = book.tocUrl, - headerMapF = bookSource.getHeaderMap() - ) - analyzeUrl.getResponseAwait( - bookSource.bookSourceUrl, - jsStr = bookSource.getContentRule().webJs, - sourceRegex = bookSource.getContentRule().sourceRegex - ).body - } - BookContent.analyzeContent( - this, - body, - book, - bookChapter, - bookSource, - bookChapter.url, - nextChapterUrl - ) + if (bookSource.getContentRule().content.isNullOrEmpty()) { + Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}") + return bookChapter.url } + val body = + if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) { + book.tocHtml + } else { + val analyzeUrl = + AnalyzeUrl( + book = book, + ruleUrl = bookChapter.url, + baseUrl = book.tocUrl, + headerMapF = bookSource.getHeaderMap() + ) + analyzeUrl.getResponseAwait( + bookSource.bookSourceUrl, + jsStr = bookSource.getContentRule().webJs, + sourceRegex = bookSource.getContentRule().sourceRegex + ).body + } + return BookContent.analyzeContent( + scope, + body, + book, + bookChapter, + bookSource, + bookChapter.url, + nextChapterUrl + ) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/web/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/web/controller/BookshelfController.kt index 53a028c62..5978ab32b 100644 --- a/app/src/main/java/io/legado/app/web/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/web/controller/BookshelfController.kt @@ -7,6 +7,7 @@ import io.legado.app.model.WebBook import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import io.legado.app.web.utils.ReturnData +import kotlinx.coroutines.runBlocking class BookshelfController { @@ -49,7 +50,9 @@ class BookshelfController { returnData.setData(content) } else { App.db.bookSourceDao().getBookSource(book.origin)?.let { source -> - content = WebBook(source).getContentBlocking(book, chapter) + content = runBlocking { + WebBook(source).getContentSuspend(book, chapter) + } returnData.setErrorMsg(content) } ?: returnData.setErrorMsg("未找到书源") }