feat: 优化代码

pull/141/head
kunfei 5 years ago
parent c7599bf66c
commit 3a1920c5ef
  1. 48
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 5
      app/src/main/java/io/legado/app/web/controller/BookshelfController.kt

@ -12,7 +12,6 @@ import io.legado.app.model.webBook.BookInfo
import io.legado.app.model.webBook.BookList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.CoroutineContext
class WebBook(val bookSource: BookSource) {
@ -140,48 +139,24 @@ class WebBook(val bookSource: BookSource) {
context: CoroutineContext = Dispatchers.IO
): Coroutine<String> {
return Coroutine.async(scope, context) {
if (bookSource.getContentRule().content.isNullOrEmpty()) {
Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}")
return@async 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
getContentSuspend(
book, bookChapter, nextChapterUrl, scope
)
}
}
fun getContentBlocking(
/**
* 章节内容
*/
suspend fun getContentSuspend(
book: Book,
bookChapter: BookChapter,
nextChapterUrl: String? = null
nextChapterUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT
): String {
return runBlocking {
if (bookSource.getContentRule().content.isNullOrEmpty()) {
Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}")
return@runBlocking bookChapter.url
return bookChapter.url
}
val body =
if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) {
@ -200,8 +175,8 @@ class WebBook(val bookSource: BookSource) {
sourceRegex = bookSource.getContentRule().sourceRegex
).body
}
BookContent.analyzeContent(
this,
return BookContent.analyzeContent(
scope,
body,
book,
bookChapter,
@ -210,5 +185,4 @@ class WebBook(val bookSource: BookSource) {
nextChapterUrl
)
}
}
}

@ -7,6 +7,7 @@ import io.legado.app.model.WebBook
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.web.utils.ReturnData
import kotlinx.coroutines.runBlocking
class BookshelfController {
@ -49,7 +50,9 @@ class BookshelfController {
returnData.setData(content)
} else {
App.db.bookSourceDao().getBookSource(book.origin)?.let { source ->
content = WebBook(source).getContentBlocking(book, chapter)
content = runBlocking {
WebBook(source).getContentSuspend(book, chapter)
}
returnData.setErrorMsg(content)
} ?: returnData.setErrorMsg("未找到书源")
}

Loading…
Cancel
Save