feat: 优化代码

pull/141/head
kunfei 5 years ago
parent fa6b84589e
commit 0070463d94
  1. 40
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 16
      app/src/main/java/io/legado/app/web/controller/BookshelfController.kt

@ -12,6 +12,7 @@ import io.legado.app.model.webBook.BookInfo
import io.legado.app.model.webBook.BookList import io.legado.app.model.webBook.BookList
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
class WebBook(val bookSource: BookSource) { class WebBook(val bookSource: BookSource) {
@ -171,4 +172,43 @@ class WebBook(val bookSource: BookSource) {
) )
} }
} }
fun getContentBlocking(
book: Book,
bookChapter: BookChapter,
nextChapterUrl: String? = null
): String {
return runBlocking {
if (bookSource.getContentRule().content.isNullOrEmpty()) {
Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}")
return@runBlocking bookChapter.url
}
val body =
if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) {
book.tocHtml
} else {
val analyzeUrl =
AnalyzeUrl(
book = book,
ruleUrl = bookChapter.url,
baseUrl = book.tocUrl,
headerMapF = bookSource.getHeaderMap()
)
analyzeUrl.getResponseAwait(
bookSource.bookSourceUrl,
jsStr = bookSource.getContentRule().webJs,
sourceRegex = bookSource.getContentRule().sourceRegex
).body
}
BookContent.analyzeContent(
this,
body,
book,
bookChapter,
bookSource,
bookChapter.url,
nextChapterUrl
)
}
}
} }

@ -7,7 +7,6 @@ import io.legado.app.model.WebBook
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.web.utils.ReturnData import io.legado.app.web.utils.ReturnData
import kotlinx.coroutines.runBlocking
class BookshelfController { class BookshelfController {
@ -37,27 +36,20 @@ class BookshelfController {
return returnData.setErrorMsg("参数url不能为空,请指定内容地址") return returnData.setErrorMsg("参数url不能为空,请指定内容地址")
} }
val book = App.db.bookDao().getBook(strings[0]) val book = App.db.bookDao().getBook(strings[0])
val chapter = App.db.bookChapterDao().getChapter(strings[0], 1) val chapter = App.db.bookChapterDao().getChapter(strings[0], strings[1].toInt())
if (book == null || chapter == null) { if (book == null || chapter == null) {
returnData.setErrorMsg("未找到") returnData.setErrorMsg("未找到")
} else { } else {
val content = BookHelp.getContent(book, chapter) var content = BookHelp.getContent(book, chapter)
if (content != null) { if (content != null) {
returnData.setData(content) returnData.setData(content)
} else { } else {
runBlocking {
App.db.bookSourceDao().getBookSource(book.origin)?.let { source -> App.db.bookSourceDao().getBookSource(book.origin)?.let { source ->
WebBook(source).getContent(book, chapter) content = WebBook(source).getContentBlocking(book, chapter)
.onSuccess { returnData.setErrorMsg(content)
returnData.setData(it!!)
}
.onError {
returnData.setErrorMsg(it.localizedMessage)
}
} ?: returnData.setErrorMsg("未找到书源") } ?: returnData.setErrorMsg("未找到书源")
} }
} }
}
return returnData return returnData
} }

Loading…
Cancel
Save