diff --git a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt index 9cfe22e8d..a0b2b002f 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt @@ -3,11 +3,15 @@ package io.legado.app.data.dao import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy +import androidx.room.Query import io.legado.app.data.entities.BookChapter @Dao interface BookChapterDao { + @Query("select * from chapters where bookUrl = :bookUrl and `index` = :index") + fun getChapter(bookUrl: String, index: Int): BookChapter? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg bookChapter: BookChapter) 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 0b764b898..f8b7148c9 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -44,11 +44,11 @@ class WebBook(private val bookSource: BookSource) { } } - fun getContent(book: Book, bookChapter: BookChapter): Coroutine { + fun getContent(book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null): Coroutine { return Coroutine.async { val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = bookChapter.url, baseUrl = book.tocUrl) val response = analyzeUrl.getResponseAsync().await() - BookContent.analyzeContent(this, response, book, bookSource) + BookContent.analyzeContent(this, response, book, bookChapter, bookSource, nextChapterUrl) } } } \ 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 a85ccb28c..a8e92444b 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 @@ -3,6 +3,7 @@ package io.legado.app.model.webbook import io.legado.app.App import io.legado.app.R import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.ContentRule import io.legado.app.model.analyzeRule.AnalyzeRule @@ -20,7 +21,9 @@ object BookContent { coroutineScope: CoroutineScope, response: Response, book: Book, - bookSource: BookSource + bookChapter: BookChapter, + bookSource: BookSource, + nextChapterUrlF: String? = null ): String { val baseUrl: String = NetworkUtils.getUrl(response) val body: String? = response.body() @@ -38,7 +41,15 @@ object BookContent { content.append(contentData.content) if (contentData.nextUrl.size == 1) { var nextUrl = contentData.nextUrl[0] + val nextChapterUrl = if (!nextChapterUrlF.isNullOrEmpty()) + nextChapterUrlF + else + App.db.bookChapterDao().getChapter(book.bookUrl, bookChapter.index + 1)?.url while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { + if (!nextChapterUrl.isNullOrEmpty() + && NetworkUtils.getAbsoluteURL(baseUrl, nextUrl) + == NetworkUtils.getAbsoluteURL(baseUrl, nextChapterUrl) + ) break nextUrlList.add(nextUrl) AnalyzeUrl(ruleUrl = nextUrl, book = book).getResponse().execute() .body()?.let { nextBody -> diff --git a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt index a51922f7b..c79b078f9 100644 --- a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt +++ b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt @@ -5,7 +5,6 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.help.coroutine.CompositeCoroutine -import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import io.legado.app.utils.htmlFormat import io.legado.app.utils.isAbsUrl @@ -117,7 +116,8 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { if (it.isNotEmpty()) { printLog(debugSource, 1, "目录完成") printLog(debugSource, 1, "", showTime = false) - contentDebug(book, it[0]) + val nextChapterUrl = if (it.size > 1) it[1].url else null + contentDebug(book, it[0], nextChapterUrl) } else { printLog(debugSource, -1, "目录列表为空") } @@ -129,9 +129,9 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { tasks.add(chapterList) } - private fun contentDebug(book: Book, bookChapter: BookChapter) { + private fun contentDebug(book: Book, bookChapter: BookChapter, nextChapterUrl: String?) { printLog(debugSource, 1, "开始获取内容") - val content = webBook.getContent(book, bookChapter) + val content = webBook.getContent(book, bookChapter, nextChapterUrl) .onSuccess { content -> content?.let { printLog(debugSource, 1000, it)