pull/32/head
kunfei 5 years ago
parent b081d83f67
commit fecd36ac65
  1. 10
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 1
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  3. 62
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  4. 6
      app/src/main/java/io/legado/app/model/webbook/ChapterData.kt

@ -1,10 +1,12 @@
package io.legado.app.model package io.legado.app.model
import io.legado.app.data.entities.Book 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.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeUrl 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.BookInfo
import io.legado.app.model.webbook.BookList import io.legado.app.model.webbook.BookList
@ -40,4 +42,12 @@ class WebBook(private val bookSource: BookSource) {
return@async book return@async book
} }
} }
fun getChapterList(book: Book, success: (List<BookChapter>) -> Unit) {
Coroutine.async {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl)
val response = analyzeUrl.getResponseAsync().await()
BookChapterList.analyzeChapterList(book, response, bookSource, analyzeUrl, success)
}
}
} }

@ -112,7 +112,6 @@ class AnalyzeRule(private var book: BaseBook? = null) {
return getStringList(ruleList, isUrl) return getStringList(ruleList, isUrl)
} }
@Throws(Exception::class) @Throws(Exception::class)
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? { fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? {
var result: Any? = null var result: Any? = null

@ -6,18 +6,20 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.TocRule 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.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import retrofit2.Response import retrofit2.Response
class BookChapterList { object BookChapterList {
fun analyzeChapterList( fun analyzeChapterList(
book: Book, book: Book,
response: Response<String>, response: Response<String>,
bookSource: BookSource, bookSource: BookSource,
analyzeUrl: AnalyzeUrl analyzeUrl: AnalyzeUrl,
): List<BookChapter> { success: (List<BookChapter>) -> Unit
) {
val chapterList = arrayListOf<BookChapter>() val chapterList = arrayListOf<BookChapter>()
val baseUrl: String = NetworkUtils.getUrl(response) val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body() val body: String? = response.body()
@ -29,34 +31,68 @@ class BookChapterList {
) )
val tocRule = bookSource.getTocRule() val tocRule = bookSource.getTocRule()
val nextUrlList = arrayListOf(baseUrl) val nextUrlList = arrayListOf(baseUrl)
var chapterData = analyzeChapterList(body, tocRule) var chapterData = analyzeChapterList(body, baseUrl, tocRule, book)
chapterList.addAll(chapterData.chapterList) chapterData.chapterList?.let {
chapterList.addAll(it)
}
if (chapterData.nextUrlList.size == 1) { if (chapterData.nextUrlList.size == 1) {
var nextUrl = chapterData.nextUrlList[0] var nextUrl = chapterData.nextUrlList[0]
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
AnalyzeUrl(ruleUrl = nextUrl, book = book).getResponse().execute() AnalyzeUrl(ruleUrl = nextUrl, book = book).getResponse().execute()
.body()?.let { .body()?.let { nextBody ->
chapterData = analyzeChapterList(it, tocRule) chapterData = analyzeChapterList(nextBody, nextUrl, tocRule, book)
nextUrl = if (chapterData.nextUrlList.isEmpty()) { nextUrl = if (chapterData.nextUrlList.isEmpty()) {
"" ""
} else { } else {
chapterData.nextUrlList[0] chapterData.nextUrlList[0]
} }
chapterList.addAll(chapterData.chapterList) chapterData.chapterList?.let {
chapterList.addAll(it)
}
} }
} }
success(chapterList)
} else if (chapterData.nextUrlList.size > 1) { } else if (chapterData.nextUrlList.size > 1) {
val chapterDataList = arrayListOf<ChapterData<String>>()
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<List<String>> {
val chapterList = arrayListOf<BookChapter>() val chapterList = arrayListOf<BookChapter>()
val nextUrlList = arrayListOf<String>() val nextUrlList = arrayListOf<String>()
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) return ChapterData(chapterList, nextUrlList)
} }

@ -2,7 +2,7 @@ package io.legado.app.model.webbook
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
data class ChapterData( data class ChapterData<T>(
var chapterList: List<BookChapter>, var chapterList: List<BookChapter>? = null,
var nextUrlList: List<String> var nextUrlList: T
) )
Loading…
Cancel
Save