From 9ebe1c003edd860f98a2b1f0d9e7b634a236f38a Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 31 Dec 2020 11:10:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookshelfController.kt | 2 +- .../io/legado/app/model/webBook/BookInfo.kt | 2 + .../io/legado/app/model/webBook/WebBook.kt | 143 ++++++++++-------- .../app/ui/book/read/ReadBookViewModel.kt | 2 +- 4 files changed, 82 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt index 55410156e..cb3e4a9f2 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt @@ -66,7 +66,7 @@ object BookshelfController { } else { App.db.bookSourceDao.getBookSource(book.origin)?.let { source -> runBlocking { - WebBook(source).getContentSuspend(book, chapter) + WebBook(source).getContentAwait(book, chapter) }.let { saveBookReadIndex(book, index) returnData.setData(it) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt index 877081cbc..851995f37 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt @@ -10,11 +10,13 @@ import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.utils.NetworkUtils import io.legado.app.utils.StringUtils.wordCountFormat import io.legado.app.utils.htmlFormat +import kotlinx.coroutines.CoroutineScope object BookInfo { @Throws(Exception::class) fun analyzeBookInfo( + scope: CoroutineScope, book: Book, body: String?, bookSource: BookSource, diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 952ec1993..d864616e9 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlin.coroutines.CoroutineContext +@Suppress("MemberVisibilityCanBePrivate") class WebBook(val bookSource: BookSource) { val sourceUrl: String @@ -26,11 +27,11 @@ class WebBook(val bookSource: BookSource) { context: CoroutineContext = Dispatchers.IO, ): Coroutine> { return Coroutine.async(scope, context) { - searchBookSuspend(scope, key, page) + searchBookAwait(scope, key, page) } } - suspend fun searchBookSuspend( + suspend fun searchBookAwait( scope: CoroutineScope, key: String, page: Int? = 1, @@ -68,28 +69,36 @@ class WebBook(val bookSource: BookSource) { scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO, ): Coroutine> { - val variableBook = SearchBook() return Coroutine.async(scope, context) { - val analyzeUrl = AnalyzeUrl( - ruleUrl = url, - page = page, - baseUrl = sourceUrl, - book = variableBook, - headerMapF = bookSource.getHeaderMap() - ) - val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) - BookList.analyzeBookList( - scope, - res.body, - bookSource, - analyzeUrl, - res.url, - variableBook, - false - ) + exploreBookAwait(url, page, scope) } } + suspend fun exploreBookAwait( + url: String, + page: Int? = 1, + scope: CoroutineScope = Coroutine.DEFAULT + ): ArrayList { + val variableBook = SearchBook() + val analyzeUrl = AnalyzeUrl( + ruleUrl = url, + page = page, + baseUrl = sourceUrl, + book = variableBook, + headerMapF = bookSource.getHeaderMap() + ) + val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + return BookList.analyzeBookList( + scope, + res.body, + bookSource, + analyzeUrl, + res.url, + variableBook, + false + ) + } + /** * 书籍信息 */ @@ -100,23 +109,38 @@ class WebBook(val bookSource: BookSource) { canReName: Boolean = true, ): Coroutine { return Coroutine.async(scope, context) { - book.type = bookSource.bookSourceType - if (!book.infoHtml.isNullOrEmpty()) { - book.infoHtml - BookInfo.analyzeBookInfo(book, book.infoHtml, bookSource, book.bookUrl, canReName) - } else { - val res = AnalyzeUrl( - ruleUrl = book.bookUrl, - baseUrl = sourceUrl, - headerMapF = bookSource.getHeaderMap(), - book = book - ).getStrResponse(bookSource.bookSourceUrl) - BookInfo.analyzeBookInfo(book, res.body, bookSource, book.bookUrl, canReName) - } - book + getBookInfoAwait(book, scope, canReName) } } + suspend fun getBookInfoAwait( + book: Book, + scope: CoroutineScope = Coroutine.DEFAULT, + canReName: Boolean = true, + ): Book { + book.type = bookSource.bookSourceType + if (!book.infoHtml.isNullOrEmpty()) { + book.infoHtml + BookInfo.analyzeBookInfo( + scope, + book, + book.infoHtml, + bookSource, + book.bookUrl, + canReName + ) + } else { + val res = AnalyzeUrl( + ruleUrl = book.bookUrl, + baseUrl = sourceUrl, + headerMapF = bookSource.getHeaderMap(), + book = book + ).getStrResponse(bookSource.bookSourceUrl) + BookInfo.analyzeBookInfo(scope, book, res.body, bookSource, book.bookUrl, canReName) + } + return book + } + /** * 目录 */ @@ -126,31 +150,25 @@ class WebBook(val bookSource: BookSource) { context: CoroutineContext = Dispatchers.IO ): Coroutine> { return Coroutine.async(scope, context) { - book.type = bookSource.bookSourceType - if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { - BookChapterList.analyzeChapterList( - this, - book, - book.tocHtml, - bookSource, - book.tocUrl - ) - } else { - val res = AnalyzeUrl( - book = book, - ruleUrl = book.tocUrl, - baseUrl = book.bookUrl, - headerMapF = bookSource.getHeaderMap() - ).getStrResponse(bookSource.bookSourceUrl) - BookChapterList.analyzeChapterList( - this, - book, - res.body, - bookSource, - book.tocUrl - ) - } + getChapterListAwait(book, scope) + } + } + suspend fun getChapterListAwait( + book: Book, + scope: CoroutineScope = Coroutine.DEFAULT + ): List { + book.type = bookSource.bookSourceType + return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { + BookChapterList.analyzeChapterList(scope, book, book.tocHtml, bookSource, book.tocUrl) + } else { + val res = AnalyzeUrl( + book = book, + ruleUrl = book.tocUrl, + baseUrl = book.bookUrl, + headerMapF = bookSource.getHeaderMap() + ).getStrResponse(bookSource.bookSourceUrl) + BookChapterList.analyzeChapterList(scope, book, res.body, bookSource, book.tocUrl) } } @@ -165,16 +183,11 @@ class WebBook(val bookSource: BookSource) { context: CoroutineContext = Dispatchers.IO ): Coroutine { return Coroutine.async(scope, context) { - getContentSuspend( - book, bookChapter, nextChapterUrl, scope - ) + getContentAwait(book, bookChapter, nextChapterUrl, scope) } } - /** - * 章节内容 - */ - suspend fun getContentSuspend( + suspend fun getContentAwait( book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null, diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 7f0962663..1cdb92986 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -216,7 +216,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { App.db.bookSourceDao.allTextEnabled.forEach { source -> try { WebBook(source) - .searchBookSuspend(this, name) + .searchBookAwait(this, name) .getOrNull(0)?.let { if (it.name == name && (it.author == author || author == "")) { val book = it.toBook()