pull/2446/head
kunfei 2 years ago
parent ca6fc902a6
commit 52d0ba8b60
  1. 15
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  2. 7
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  3. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 7
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -1,5 +1,6 @@
package io.legado.app.model.webBook
import io.legado.app.constant.AppLog
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
@ -9,6 +10,7 @@ import io.legado.app.help.book.getBookType
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.http.StrResponse
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleData
import kotlinx.coroutines.CoroutineScope
@ -179,19 +181,30 @@ object WebBook {
scope: CoroutineScope,
bookSource: BookSource,
book: Book,
runPerJs: Boolean = false,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<BookChapter>> {
return Coroutine.async(scope, context) {
getChapterListAwait(bookSource, book).getOrThrow()
getChapterListAwait(bookSource, book, runPerJs).getOrThrow()
}
}
suspend fun getChapterListAwait(
bookSource: BookSource,
book: Book,
runPerJs: Boolean = false
): Result<List<BookChapter>> {
book.type = bookSource.getBookType()
return kotlin.runCatching {
val preUpdateJs = bookSource.ruleToc?.preUpdateJs
if (runPerJs && !preUpdateJs.isNullOrBlank()) {
kotlin.runCatching {
AnalyzeRule(book, bookSource).evalJS(preUpdateJs)
}.onFailure {
AppLog.put("执行preUpdateJs规则失败 书源:${bookSource.bookSourceName}", it)
throw it
}
}
if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
BookChapterList.analyzeChapterList(
bookSource = bookSource,

@ -23,7 +23,6 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.webdav.ObjectNotFoundException
import io.legado.app.model.BookCover
import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.remote.RemoteBookWebDav
import io.legado.app.model.webBook.WebBook
@ -191,11 +190,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} else {
bookSource?.let { bookSource ->
val oldBook = book.copy()
val preUpdateJs = bookSource.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, bookSource).evalJS(preUpdateJs)
}
WebBook.getChapterList(this, bookSource, book)
WebBook.getChapterList(this, bookSource, book, true)
.onSuccess(IO) {
if (inBookshelf) {
if (oldBook.bookUrl == book.bookUrl) {

@ -25,7 +25,6 @@ import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.BaseReadAloudService
@ -154,17 +153,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.bookSource?.let {
viewModelScope.launch(IO) {
val oldBook = book.copy()
val preUpdateJs = it.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) {
val source = it
kotlin.runCatching {
AnalyzeRule(book, it).evalJS(preUpdateJs)
}.onFailure {
ReadBook.upMsg("执行preUpdateJs规则失败")
AppLog.put("执行preUpdateJs规则失败 书源:${source.bookSourceName}", it)
}
}
WebBook.getChapterList(viewModelScope, it, book)
WebBook.getChapterList(viewModelScope, it, book, true)
.onSuccess(IO) { cList ->
if (oldBook.bookUrl == book.bookUrl) {
appDb.bookDao.update(book)

@ -19,7 +19,6 @@ import io.legado.app.help.book.removeType
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.LocalConfig
import io.legado.app.model.CacheBook
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService
import io.legado.app.utils.postEvent
@ -123,14 +122,10 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
upTocAdd(bookUrl)
execute(context = upTocPool) {
val oldBook = book.copy()
val preUpdateJs = source.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, source).evalJS(preUpdateJs)
}
if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(source, book)
}
val toc = WebBook.getChapterListAwait(source, book).getOrThrow()
val toc = WebBook.getChapterListAwait(source, book, true).getOrThrow()
book.removeType(BookType.updateError)
if (book.bookUrl == bookUrl) {
appDb.bookDao.update(book)

Loading…
Cancel
Save