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
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<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)
}
@Throws(Exception::class)
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? {
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.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<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl
): List<BookChapter> {
analyzeUrl: AnalyzeUrl,
success: (List<BookChapter>) -> Unit
) {
val chapterList = arrayListOf<BookChapter>()
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<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 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)
}

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