|
|
@ -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) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|