feat: 优化代码

pull/123/head
kunfei 5 years ago
parent e30fdbbe9f
commit fe7c41a569
  1. 23
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  2. 16
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt

@ -31,7 +31,7 @@ object BookChapterList {
App.INSTANCE.getString(R.string.error_get_web_content, baseUrl)
)
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}")
val analyzeRule = AnalyzeRule(book)
val tocRule = bookSource.getTocRule()
val nextUrlList = arrayListOf(baseUrl)
var reverse = false
@ -45,8 +45,7 @@ object BookChapterList {
}
var chapterData =
analyzeChapterList(
analyzeRule.setContent(body, baseUrl),
book.bookUrl, baseUrl, tocRule, listRule, bookSource, log = true
book, baseUrl, body, tocRule, listRule, bookSource, log = true
)
chapterData.chapterList?.let {
chapterList.addAll(it)
@ -67,8 +66,7 @@ object BookChapterList {
).getResponseAwait()
.body?.let { nextBody ->
chapterData = analyzeChapterList(
analyzeRule.setContent(nextBody, nextUrl),
book.bookUrl, nextUrl, tocRule, listRule, bookSource
book, nextUrl, nextBody, tocRule, listRule, bookSource
)
nextUrl = if (chapterData.nextUrl.isNotEmpty()) {
chapterData.nextUrl[0]
@ -100,7 +98,6 @@ object BookChapterList {
item,
book,
bookSource,
analyzeRule,
tocRule,
listRule,
chapterList,
@ -123,7 +120,6 @@ object BookChapterList {
chapterData: ChapterData<String>,
book: Book,
bookSource: BookSource,
analyzeRule: AnalyzeRule,
tocRule: TocRule,
listRule: String,
chapterList: ArrayList<BookChapter>,
@ -136,10 +132,9 @@ object BookChapterList {
ruleUrl = chapterData.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body
).getResponseAwait().body ?: throw Exception("${chapterData.nextUrl}, 下载失败")
val nextChapterData = analyzeChapterList(
analyzeRule.setContent(nextBody, chapterData.nextUrl),
book.bookUrl, chapterData.nextUrl, tocRule, listRule, bookSource,
book, chapterData.nextUrl, nextBody, tocRule, listRule, bookSource,
false
)
synchronized(chapterDataList) {
@ -203,15 +198,17 @@ object BookChapterList {
}
private fun analyzeChapterList(
analyzeRule: AnalyzeRule,
bookUrl: String,
book: Book,
baseUrl: String,
body: String,
tocRule: TocRule,
listRule: String,
bookSource: BookSource,
getNextUrl: Boolean = true,
log: Boolean = false
): ChapterData<List<String>> {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body, baseUrl)
val chapterList = arrayListOf<BookChapter>()
val nextUrlList = arrayListOf<String>()
val nextTocRule = tocRule.nextTocUrl
@ -242,7 +239,7 @@ object BookChapterList {
var isVip: String?
for (item in elements) {
analyzeRule.setContent(item)
val bookChapter = BookChapter(bookUrl = bookUrl)
val bookChapter = BookChapter(bookUrl = book.bookUrl)
analyzeRule.chapter = bookChapter
bookChapter.title = analyzeRule.getString(nameRule)
bookChapter.url = analyzeRule.getString(urlRule, true)

@ -33,13 +33,11 @@ object BookContent {
)
)
Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}")
val analyzeRule = AnalyzeRule(book)
val content = StringBuilder()
val nextUrlList = arrayListOf(baseUrl)
val contentRule = bookSource.getContentRule()
var contentData = analyzeContent(
analyzeRule.setContent(body, baseUrl),
contentRule, bookChapter, bookSource
book, baseUrl, body, contentRule, bookChapter, bookSource
)
content.append(contentData.content.replace(bookChapter.title, ""))
if (contentData.nextUrl.size == 1) {
@ -62,8 +60,7 @@ object BookContent {
.body?.let { nextBody ->
contentData =
analyzeContent(
analyzeRule.setContent(nextBody, nextUrl),
contentRule, bookChapter, bookSource, false
book, nextUrl, nextBody, contentRule, bookChapter, bookSource, false
)
nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
@ -87,8 +84,7 @@ object BookContent {
.body?.let {
contentData =
analyzeContent(
analyzeRule.setContent(it, item.nextUrl),
contentRule, bookChapter, bookSource, false
book, item.nextUrl, it, contentRule, bookChapter, bookSource, false
)
item.content = contentData.content
}
@ -108,12 +104,16 @@ object BookContent {
@Throws(Exception::class)
private fun analyzeContent(
analyzeRule: AnalyzeRule,
book: Book,
baseUrl: String,
body: String,
contentRule: ContentRule,
chapter: BookChapter,
bookSource: BookSource,
printLog: Boolean = true
): ContentData<List<String>> {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body, baseUrl)
val nextUrlList = arrayListOf<String>()
analyzeRule.chapter = chapter
val nextUrlRule = contentRule.nextContentUrl

Loading…
Cancel
Save