pull/32/head
kunfei 5 years ago
parent 10364430f7
commit b51594e418
  1. 106
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt

@ -17,20 +17,20 @@ import retrofit2.Response
object BookChapterList { object BookChapterList {
suspend fun analyzeChapterList( suspend fun analyzeChapterList(
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
book: Book, book: Book,
response: Response<String>, response: Response<String>,
bookSource: BookSource, bookSource: BookSource,
analyzeUrl: AnalyzeUrl analyzeUrl: AnalyzeUrl
): List<BookChapter> { ): List<BookChapter> {
var chapterList = arrayListOf<BookChapter>() var 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()
body ?: throw Exception( body ?: throw Exception(
App.INSTANCE.getString( App.INSTANCE.getString(
R.string.error_get_web_content, R.string.error_get_web_content,
baseUrl baseUrl
) )
) )
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取成功:$baseUrl") SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取成功:$baseUrl")
val tocRule = bookSource.getTocRule() val tocRule = bookSource.getTocRule()
@ -45,7 +45,7 @@ object BookChapterList {
listRule = listRule.substring(1) listRule = listRule.substring(1)
} }
var chapterData = var chapterData =
analyzeChapterList(body, baseUrl, tocRule, listRule, book, bookSource, printLog = true) analyzeChapterList(body, baseUrl, tocRule, listRule, book, bookSource, printLog = true)
chapterData.chapterList?.let { chapterData.chapterList?.let {
chapterList.addAll(it) chapterList.addAll(it)
} }
@ -54,25 +54,25 @@ object BookChapterList {
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
AnalyzeUrl( AnalyzeUrl(
ruleUrl = nextUrl, ruleUrl = nextUrl,
book = book, book = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await() ).getResponseAsync().await()
.body()?.let { nextBody -> .body()?.let { nextBody ->
chapterData = analyzeChapterList( chapterData = analyzeChapterList(
nextBody, nextBody,
nextUrl, nextUrl,
tocRule, tocRule,
listRule, listRule,
book, book,
bookSource bookSource
) )
nextUrl = nextUrl =
if (chapterData.nextUrl.isNotEmpty()) chapterData.nextUrl[0] else "" if (chapterData.nextUrl.isNotEmpty()) chapterData.nextUrl[0] else ""
chapterData.chapterList?.let { chapterData.chapterList?.let {
chapterList.addAll(it) chapterList.addAll(it)
}
} }
}
} }
} else if (chapterData.nextUrl.size > 1) { } else if (chapterData.nextUrl.size > 1) {
val chapterDataList = arrayListOf<ChapterData<String>>() val chapterDataList = arrayListOf<ChapterData<String>>()
@ -83,18 +83,18 @@ object BookChapterList {
for (item in chapterDataList) { for (item in chapterDataList) {
withContext(coroutineScope.coroutineContext) { withContext(coroutineScope.coroutineContext) {
val nextResponse = AnalyzeUrl( val nextResponse = AnalyzeUrl(
ruleUrl = item.nextUrl, ruleUrl = item.nextUrl,
book = book, book = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await() ).getResponseAsync().await()
val nextChapterData = analyzeChapterList( val nextChapterData = analyzeChapterList(
nextResponse.body() ?: "", nextResponse.body() ?: "",
item.nextUrl, item.nextUrl,
tocRule, tocRule,
listRule, listRule,
book, book,
bookSource, bookSource,
getNextUrl = false getNextUrl = false
) )
item.chapterList = nextChapterData.chapterList item.chapterList = nextChapterData.chapterList
} }
@ -125,14 +125,14 @@ object BookChapterList {
private fun analyzeChapterList( private fun analyzeChapterList(
body: String, body: String,
baseUrl: String, baseUrl: String,
tocRule: TocRule, tocRule: TocRule,
listRule: String, listRule: String,
book: Book, book: Book,
bookSource: BookSource, bookSource: BookSource,
getNextUrl: Boolean = true, getNextUrl: Boolean = true,
printLog: Boolean = false printLog: Boolean = false
): ChapterData<List<String>> { ): ChapterData<List<String>> {
val chapterList = arrayListOf<BookChapter>() val chapterList = arrayListOf<BookChapter>()
val nextUrlList = arrayListOf<String>() val nextUrlList = arrayListOf<String>()
@ -148,10 +148,10 @@ object BookChapterList {
} }
} }
SourceDebug.printLog( SourceDebug.printLog(
bookSource.bookSourceUrl, bookSource.bookSourceUrl,
1, 1,
TextUtils.join(",", nextUrlList), TextUtils.join(",", nextUrlList),
printLog printLog
) )
} }
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "解析目录列表", printLog) SourceDebug.printLog(bookSource.bookSourceUrl, 1, "解析目录列表", printLog)
@ -173,10 +173,10 @@ object BookChapterList {
} }
} }
SourceDebug.printLog( SourceDebug.printLog(
bookSource.bookSourceUrl, bookSource.bookSourceUrl,
1, 1,
"${chapterList[0].title}${chapterList[0].url}", "${chapterList[0].title}${chapterList[0].url}",
printLog printLog
) )
} }
return ChapterData(chapterList, nextUrlList) return ChapterData(chapterList, nextUrlList)

Loading…
Cancel
Save