From ea3259be2e08ccc3bd156ad7176f05455f3ffdcf Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 28 Aug 2021 09:45:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/webBook/BookChapterList.kt | 30 ++++++--------- .../legado/app/model/webBook/BookContent.kt | 24 ++++++------ .../legado/app/model/webBook/ChapterData.kt | 8 ---- .../legado/app/model/webBook/ContentData.kt | 6 --- .../legado/app/model/webBook/PreciseSearch.kt | 37 ------------------- .../io/legado/app/model/webBook/WebBook.kt | 27 ++++++++++++++ .../app/ui/book/read/ReadBookViewModel.kt | 3 +- .../ui/main/bookshelf/BookshelfViewModel.kt | 4 +- 8 files changed, 53 insertions(+), 86 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/model/webBook/ChapterData.kt delete mode 100644 app/src/main/java/io/legado/app/model/webBook/ContentData.kt delete mode 100644 app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index ff4cfe15c..5e8022574 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -49,13 +49,11 @@ object BookChapterList { scope, book, baseUrl, redirectUrl, body, tocRule, listRule, bookSource, log = true ) - chapterData.chapterList?.let { - chapterList.addAll(it) - } - when (chapterData.nextUrl.size) { + chapterList.addAll(chapterData.first) + when (chapterData.second.size) { 0 -> Unit 1 -> { - var nextUrl = chapterData.nextUrl[0] + var nextUrl = chapterData.second[0] while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { nextUrlList.add(nextUrl) AnalyzeUrl( @@ -68,20 +66,18 @@ object BookChapterList { scope, book, nextUrl, nextUrl, nextBody, tocRule, listRule, bookSource ) - nextUrl = chapterData.nextUrl.firstOrNull() ?: "" - chapterData.chapterList?.let { - chapterList.addAll(it) - } + nextUrl = chapterData.second.firstOrNull() ?: "" + chapterList.addAll(chapterData.first) } } Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}") } else -> { - Debug.log(bookSource.bookSourceUrl, "◇并发解析目录,总页数:${chapterData.nextUrl.size}") + Debug.log(bookSource.bookSourceUrl, "◇并发解析目录,总页数:${chapterData.second.size}") withContext(IO) { - val asyncArray = Array(chapterData.nextUrl.size) { + val asyncArray = Array(chapterData.second.size) { async(IO) { - val urlStr = chapterData.nextUrl[it] + val urlStr = chapterData.second[it] val analyzeUrl = AnalyzeUrl( ruleUrl = urlStr, book = book, @@ -92,13 +88,11 @@ object BookChapterList { analyzeChapterList( this, book, urlStr, res.url, res.body!!, tocRule, listRule, bookSource, false - ).chapterList + ).first } } asyncArray.forEach { coroutine -> - coroutine.await()?.let { - chapterList.addAll(it) - } + chapterList.addAll(coroutine.await()) } } } @@ -139,7 +133,7 @@ object BookChapterList { bookSource: BookSource, getNextUrl: Boolean = true, log: Boolean = false - ): ChapterData> { + ): Pair, List> { val analyzeRule = AnalyzeRule(book, bookSource) analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.setRedirectUrl(redirectUrl) @@ -201,7 +195,7 @@ object BookChapterList { Debug.log(bookSource.bookSourceUrl, "┌获取首章信息", log) Debug.log(bookSource.bookSourceUrl, "└${chapterList[0].tag}", log) } - return ChapterData(chapterList, nextUrlList) + return Pair(chapterList, nextUrlList) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index c1a2db56c..3051d11be 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -53,9 +53,9 @@ object BookContent { var contentData = analyzeContent( book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl ) - content.append(contentData.content) - if (contentData.nextUrl.size == 1) { - var nextUrl = contentData.nextUrl[0] + content.append(contentData.first) + if (contentData.second.size == 1) { + var nextUrl = contentData.second[0] while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { if (!mNextChapterUrl.isNullOrEmpty() && NetworkUtils.getAbsoluteURL(baseUrl, nextUrl) @@ -75,17 +75,17 @@ object BookContent { bookChapter, bookSource, mNextChapterUrl, false ) nextUrl = - if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" - content.append("\n").append(contentData.content) + if (contentData.second.isNotEmpty()) contentData.second[0] else "" + content.append("\n").append(contentData.first) } } Debug.log(bookSource.bookSourceUrl, "◇本章总页数:${nextUrlList.size}") - } else if (contentData.nextUrl.size > 1) { - Debug.log(bookSource.bookSourceUrl, "◇并发解析目录,总页数:${contentData.nextUrl.size}") + } else if (contentData.second.size > 1) { + Debug.log(bookSource.bookSourceUrl, "◇并发解析目录,总页数:${contentData.second.size}") withContext(IO) { - val asyncArray = Array(contentData.nextUrl.size) { + val asyncArray = Array(contentData.second.size) { async(IO) { - val urlStr = contentData.nextUrl[it] + val urlStr = contentData.second[it] val analyzeUrl = AnalyzeUrl( ruleUrl = urlStr, book = book, @@ -96,7 +96,7 @@ object BookContent { analyzeContent( book, urlStr, res.url, res.body!!, contentRule, bookChapter, bookSource, mNextChapterUrl, false - ).content + ).first } } asyncArray.forEach { coroutine -> @@ -131,7 +131,7 @@ object BookContent { bookSource: BookSource, nextChapterUrl: String?, printLog: Boolean = true - ): ContentData> { + ): Pair> { val analyzeRule = AnalyzeRule(book, bookSource) analyzeRule.setContent(body, baseUrl) val rUrl = analyzeRule.setRedirectUrl(redirectUrl) @@ -150,6 +150,6 @@ object BookContent { } Debug.log(bookSource.bookSourceUrl, "└" + nextUrlList.joinToString(","), printLog) } - return ContentData(content, nextUrlList) + return Pair(content, nextUrlList) } } diff --git a/app/src/main/java/io/legado/app/model/webBook/ChapterData.kt b/app/src/main/java/io/legado/app/model/webBook/ChapterData.kt deleted file mode 100644 index 6b06eb508..000000000 --- a/app/src/main/java/io/legado/app/model/webBook/ChapterData.kt +++ /dev/null @@ -1,8 +0,0 @@ -package io.legado.app.model.webBook - -import io.legado.app.data.entities.BookChapter - -data class ChapterData( - var chapterList: List? = null, - var nextUrl: T -) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webBook/ContentData.kt b/app/src/main/java/io/legado/app/model/webBook/ContentData.kt deleted file mode 100644 index 65185d806..000000000 --- a/app/src/main/java/io/legado/app/model/webBook/ContentData.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.legado.app.model.webBook - -data class ContentData( - var content: String = "", - var nextUrl: T -) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt b/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt deleted file mode 100644 index 2e294e06e..000000000 --- a/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt +++ /dev/null @@ -1,37 +0,0 @@ -package io.legado.app.model.webBook - -import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookSource -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.isActive - -/** - * 精准搜索 - */ -object PreciseSearch { - - suspend fun searchFirstBook( - scope: CoroutineScope, - bookSources: List, - name: String, - author: String - ): Book? { - bookSources.forEach { bookSource -> - kotlin.runCatching { - if (!scope.isActive) return null - WebBook.searchBookAwait(scope, bookSource, name).firstOrNull { - it.name == name && it.author == author - }?.let { - return if (it.tocUrl.isBlank()) { - if (!scope.isActive) return null - WebBook.getBookInfoAwait(scope, bookSource, it.toBook()) - } else { - it.toBook() - } - } - } - } - return null - } - -} \ No newline at end of file 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 4a9d8095e..338ea72d0 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 @@ -10,10 +10,37 @@ import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeUrl import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.isActive import kotlin.coroutines.CoroutineContext @Suppress("MemberVisibilityCanBePrivate") object WebBook { + /** + * 精准搜索 + */ + suspend fun preciseSearch( + scope: CoroutineScope, + bookSources: List, + name: String, + author: String + ): Book? { + bookSources.forEach { bookSource -> + kotlin.runCatching { + if (!scope.isActive) return null + searchBookAwait(scope, bookSource, name).firstOrNull { + it.name == name && it.author == author + }?.let { + return if (it.tocUrl.isBlank()) { + if (!scope.isActive) return null + getBookInfoAwait(scope, bookSource, it.toBook()) + } else { + it.toBook() + } + } + } + } + return 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 c4959f7b4..52df7b306 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 @@ -16,7 +16,6 @@ import io.legado.app.help.ContentProcessor import io.legado.app.help.storage.AppWebDav import io.legado.app.model.ReadBook import io.legado.app.model.localBook.LocalBook -import io.legado.app.model.webBook.PreciseSearch import io.legado.app.model.webBook.WebBook import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud @@ -221,7 +220,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (!AppConfig.autoChangeSource) return execute { val sources = appDb.bookSourceDao.allTextEnabled - val book = PreciseSearch.searchFirstBook(this, sources, name, author) + val book = WebBook.preciseSearch(this, sources, name, author) if (book != null) { book.upInfoFromOld(ReadBook.book) changeTo(book) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 2c7654e18..ae3a39ef7 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -10,7 +10,6 @@ import io.legado.app.data.entities.BookSource import io.legado.app.help.http.newCall import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text -import io.legado.app.model.webBook.PreciseSearch import io.legado.app.model.webBook.WebBook import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO @@ -114,8 +113,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) val name = it["name"] ?: "" val author = it["author"] ?: "" if (name.isNotEmpty() && appDb.bookDao.getBook(name, author) == null) { - val book = PreciseSearch - .searchFirstBook(this, bookSources, name, author) + val book = WebBook.preciseSearch(this, bookSources, name, author) book?.let { if (groupId > 0) { book.group = groupId