From fecd36ac6565b0a02c119022b82d60c43b9bb1c4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 21 Jul 2019 17:37:16 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/model/WebBook.kt | 10 +++ .../app/model/analyzeRule/AnalyzeRule.kt | 1 - .../app/model/webbook/BookChapterList.kt | 62 +++++++++++++++---- .../legado/app/model/webbook/ChapterData.kt | 6 +- 4 files changed, 62 insertions(+), 17 deletions(-) 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 6c61a8875..52ee84b0c 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -1,10 +1,12 @@ package io.legado.app.model 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.SearchBook import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.webbook.BookChapterList import io.legado.app.model.webbook.BookInfo import io.legado.app.model.webbook.BookList @@ -40,4 +42,12 @@ class WebBook(private val bookSource: BookSource) { return@async book } } + + fun getChapterList(book: Book, success: (List) -> Unit) { + Coroutine.async { + val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl) + val response = analyzeUrl.getResponseAsync().await() + BookChapterList.analyzeChapterList(book, response, bookSource, analyzeUrl, success) + } + } } \ No newline at end of file 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 4b30e0451..644c11c3f 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 @@ -112,7 +112,6 @@ class AnalyzeRule(private var book: BaseBook? = null) { return getStringList(ruleList, isUrl) } - @Throws(Exception::class) fun getStringList(ruleList: List, isUrl: Boolean): List? { var result: Any? = null 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 110cd2260..99d014f79 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 @@ -6,18 +6,20 @@ 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.TocRule +import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils import retrofit2.Response -class BookChapterList { +object BookChapterList { fun analyzeChapterList( book: Book, response: Response, bookSource: BookSource, - analyzeUrl: AnalyzeUrl - ): List { + analyzeUrl: AnalyzeUrl, + success: (List) -> Unit + ) { val chapterList = arrayListOf() val baseUrl: String = NetworkUtils.getUrl(response) val body: String? = response.body() @@ -29,34 +31,68 @@ class BookChapterList { ) val tocRule = bookSource.getTocRule() val nextUrlList = arrayListOf(baseUrl) - var chapterData = analyzeChapterList(body, tocRule) - chapterList.addAll(chapterData.chapterList) + var chapterData = analyzeChapterList(body, baseUrl, tocRule, book) + chapterData.chapterList?.let { + chapterList.addAll(it) + } if (chapterData.nextUrlList.size == 1) { var nextUrl = chapterData.nextUrlList[0] while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { nextUrlList.add(nextUrl) AnalyzeUrl(ruleUrl = nextUrl, book = book).getResponse().execute() - .body()?.let { - chapterData = analyzeChapterList(it, tocRule) + .body()?.let { nextBody -> + chapterData = analyzeChapterList(nextBody, nextUrl, tocRule, book) nextUrl = if (chapterData.nextUrlList.isEmpty()) { "" } else { chapterData.nextUrlList[0] } - chapterList.addAll(chapterData.chapterList) + chapterData.chapterList?.let { + chapterList.addAll(it) + } } } + success(chapterList) } else if (chapterData.nextUrlList.size > 1) { - + val chapterDataList = arrayListOf>() + for (item in chapterData.nextUrlList) { + if (!nextUrlList.contains(item)) { + val data = ChapterData(nextUrlList = item) + chapterDataList.add(data) + } + } } - return chapterList } - private fun analyzeChapterList(body: String, tocRule: TocRule): ChapterData { + private fun analyzeChapterList( + body: String, + baseUrl: String, + tocRule: TocRule, + book: Book + ): ChapterData> { val chapterList = arrayListOf() val nextUrlList = arrayListOf() - - + val analyzeRule = AnalyzeRule(book) + analyzeRule.setContent(body, baseUrl) + analyzeRule.getStringList(tocRule.nextTocUrl ?: "", true)?.let { + nextUrlList.addAll(it) + } + val elements = analyzeRule.getElements(tocRule.chapterList ?: "") + if (elements.isNotEmpty()) { + val nameRule = analyzeRule.splitSourceRule(tocRule.chapterName ?: "") + val urlRule = analyzeRule.splitSourceRule(tocRule.chapterUrl ?: "") + for (item in elements) { + analyzeRule.setContent(item) + val title = analyzeRule.getString(nameRule) + if (title.isNotEmpty()) { + val bookChapter = BookChapter(bookUrl = book.bookUrl) + bookChapter.title = title + bookChapter.url = analyzeRule.getString(urlRule, true) + if (bookChapter.url.isEmpty()) bookChapter.url = baseUrl + chapterList.add(bookChapter) + } + } + } return ChapterData(chapterList, 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 index f6ddf6b66..fa43c1600 100644 --- a/app/src/main/java/io/legado/app/model/webbook/ChapterData.kt +++ b/app/src/main/java/io/legado/app/model/webbook/ChapterData.kt @@ -2,7 +2,7 @@ package io.legado.app.model.webbook import io.legado.app.data.entities.BookChapter -data class ChapterData( - var chapterList: List, - var nextUrlList: List +data class ChapterData( + var chapterList: List? = null, + var nextUrlList: T ) \ No newline at end of file