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

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

Loading…
Cancel
Save