From e254932ae737b36dca5f475d4ffecd1e82ab70d8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 23 Feb 2021 17:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=98=E9=87=8FnextChapter?= =?UTF-8?q?Url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/Debug.kt | 2 +- .../app/model/analyzeRule/AnalyzeRule.kt | 2 + .../legado/app/model/webBook/BookContent.kt | 38 ++++++++++++++----- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index c74a36b5e..4697ae48b 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -206,7 +206,7 @@ object Debug { if (it.isNotEmpty()) { log(debugSource, "︽目录页解析完成") log(debugSource, showTime = false) - val nextChapterUrl = if (it.size > 1) it[1].url else null + val nextChapterUrl = it.getOrNull(1)?.url contentDebug(scope, webBook, book, it[0], nextChapterUrl) } else { log(debugSource, "︽目录列表为空", state = -1) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 98e590cf9..d316fcc08 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -27,6 +27,7 @@ import kotlin.collections.HashMap class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { var book: BaseBook? = null var chapter: BookChapter? = null + var nextChapterUrl: String? = null private var content: Any? = null private var baseUrl: String? = null private var baseURL: URL? = null @@ -646,6 +647,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { bindings["chapter"] = chapter bindings["title"] = chapter?.title bindings["src"] = content + bindings["nextChapterUrl"] = nextChapterUrl return runBlocking { SCRIPT_ENGINE.eval(jsStr, bindings) } 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 265035e1c..e0aecc781 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 @@ -26,31 +26,33 @@ object BookContent { bookChapter: BookChapter, bookSource: BookSource, baseUrl: String, - nextChapterUrlF: String? = null + nextChapterUrl: String? = null ): String { body ?: throw Exception( appCtx.getString(R.string.error_get_web_content, baseUrl) ) Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}") + val mNextChapterUrl = if (!nextChapterUrl.isNullOrEmpty()) { + nextChapterUrl + } else { + appDb.bookChapterDao.getChapter(book.bookUrl, bookChapter.index + 1)?.url + } val content = StringBuilder() val nextUrlList = arrayListOf(baseUrl) val contentRule = bookSource.getContentRule() val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl) + analyzeRule.nextChapterUrl = mNextChapterUrl var contentData = analyzeContent( - book, baseUrl, body, contentRule, bookChapter, bookSource + book, baseUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl ) content.append(contentData.content).append("\n") if (contentData.nextUrl.size == 1) { var nextUrl = contentData.nextUrl[0] - val nextChapterUrl = if (!nextChapterUrlF.isNullOrEmpty()) - nextChapterUrlF - else - appDb.bookChapterDao.getChapter(book.bookUrl, bookChapter.index + 1)?.url while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { - if (!nextChapterUrl.isNullOrEmpty() + if (!mNextChapterUrl.isNullOrEmpty() && NetworkUtils.getAbsoluteURL(baseUrl, nextUrl) - == NetworkUtils.getAbsoluteURL(baseUrl, nextChapterUrl) + == NetworkUtils.getAbsoluteURL(baseUrl, mNextChapterUrl) ) break nextUrlList.add(nextUrl) AnalyzeUrl( @@ -59,7 +61,14 @@ object BookContent { headerMapF = bookSource.getHeaderMap() ).getStrResponse(bookSource.bookSourceUrl).body?.let { nextBody -> contentData = analyzeContent( - book, nextUrl, nextBody, contentRule, bookChapter, bookSource, false + book, + nextUrl, + nextBody, + contentRule, + bookChapter, + bookSource, + mNextChapterUrl, + false ) nextUrl = if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" @@ -81,7 +90,14 @@ object BookContent { headerMapF = bookSource.getHeaderMap() ).getStrResponse(bookSource.bookSourceUrl).body?.let { contentData = analyzeContent( - book, item.nextUrl, it, contentRule, bookChapter, bookSource, false + book, + item.nextUrl, + it, + contentRule, + bookChapter, + bookSource, + mNextChapterUrl, + false ) item.content = contentData.content } @@ -115,10 +131,12 @@ object BookContent { contentRule: ContentRule, chapter: BookChapter, bookSource: BookSource, + nextChapterUrl: String?, printLog: Boolean = true ): ContentData> { val analyzeRule = AnalyzeRule(book) analyzeRule.setContent(body).setBaseUrl(baseUrl) + analyzeRule.nextChapterUrl = nextChapterUrl val nextUrlList = arrayListOf() analyzeRule.chapter = chapter //获取正文