pull/1771/head
kunfei 3 years ago
parent 8fc9394b63
commit bbb6548b5d
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 66
      app/src/main/java/io/legado/app/model/webBook/WebBook.kt
  3. 2
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt
  5. 17
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  7. 2
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -124,7 +124,7 @@ object BookController {
if (book.tocUrl.isBlank()) { if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(this, bookSource, book) WebBook.getBookInfoAwait(this, bookSource, book)
} }
WebBook.getChapterListAwait(this, bookSource, book) WebBook.getChapterListAwait(this, bookSource, book).getOrThrow()
} }
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())

@ -188,7 +188,7 @@ object WebBook {
context: CoroutineContext = Dispatchers.IO context: CoroutineContext = Dispatchers.IO
): Coroutine<List<BookChapter>> { ): Coroutine<List<BookChapter>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
getChapterListAwait(scope, bookSource, book) getChapterListAwait(scope, bookSource, book).getOrThrow()
} }
} }
@ -196,40 +196,42 @@ object WebBook {
scope: CoroutineScope, scope: CoroutineScope,
bookSource: BookSource, bookSource: BookSource,
book: Book, book: Book,
): List<BookChapter> { ): Result<List<BookChapter>> {
book.type = bookSource.bookSourceType book.type = bookSource.bookSourceType
return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { return kotlin.runCatching {
BookChapterList.analyzeChapterList( if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
scope = scope, BookChapterList.analyzeChapterList(
bookSource = bookSource, scope = scope,
book = book, bookSource = bookSource,
baseUrl = book.tocUrl, book = book,
redirectUrl = book.tocUrl, baseUrl = book.tocUrl,
body = book.tocHtml redirectUrl = book.tocUrl,
) body = book.tocHtml
} else { )
val analyzeUrl = AnalyzeUrl( } else {
mUrl = book.tocUrl, val analyzeUrl = AnalyzeUrl(
baseUrl = book.bookUrl, mUrl = book.tocUrl,
source = bookSource, baseUrl = book.bookUrl,
ruleData = book, source = bookSource,
headerMapF = bookSource.getHeaderMap(true) ruleData = book,
) headerMapF = bookSource.getHeaderMap(true)
var res = analyzeUrl.getStrResponseAwait() )
//检测书源是否已登录 var res = analyzeUrl.getStrResponseAwait()
bookSource.loginCheckJs?.let { checkJs -> //检测书源是否已登录
if (checkJs.isNotBlank()) { bookSource.loginCheckJs?.let { checkJs ->
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse if (checkJs.isNotBlank()) {
res = analyzeUrl.evalJS(checkJs, result = res) as StrResponse
}
} }
BookChapterList.analyzeChapterList(
scope = scope,
bookSource = bookSource,
book = book,
baseUrl = book.tocUrl,
redirectUrl = res.url,
body = res.body
)
} }
BookChapterList.analyzeChapterList(
scope = scope,
bookSource = bookSource,
book = book,
baseUrl = book.tocUrl,
redirectUrl = res.url,
body = res.body
)
} }
} }

@ -213,7 +213,7 @@ class CheckSourceService : BaseService() {
} }
//校验目录 //校验目录
if (CheckSource.checkCategory) { if (CheckSource.checkCategory) {
val toc = WebBook.getChapterListAwait(this, source, mBook) val toc = WebBook.getChapterListAwait(this, source, mBook).getOrThrow()
val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url
//校验正文 //校验正文
if (CheckSource.checkContent) { if (CheckSource.checkContent) {

@ -195,7 +195,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
} }
private suspend fun loadBookToc(scope: CoroutineScope, source: BookSource, book: Book) { private suspend fun loadBookToc(scope: CoroutineScope, source: BookSource, book: Book) {
val chapters = WebBook.getChapterListAwait(scope, source, book) val chapters = WebBook.getChapterListAwait(scope, source, book).getOrThrow()
tocMap[book.bookUrl] = chapters tocMap[book.bookUrl] = chapters
book.latestChapterTitle = chapters.last().title book.latestChapterTitle = chapters.last().title
val searchBook: SearchBook = book.toSearchBook() val searchBook: SearchBook = book.toSearchBook()
@ -300,7 +300,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
Pair(toc, source) Pair(toc, source)
} }
} }

@ -8,6 +8,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.toastOnUi
class BookshelfManageViewModel(application: Application) : BaseViewModel(application) { class BookshelfManageViewModel(application: Application) : BaseViewModel(application) {
@ -47,11 +48,17 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
if (book.isLocalBook()) return@forEachIndexed if (book.isLocalBook()) return@forEachIndexed
if (book.origin == source.bookSourceUrl) return@forEachIndexed if (book.origin == source.bookSourceUrl) return@forEachIndexed
WebBook.preciseSearchAwait(this, source, book.name, book.author) WebBook.preciseSearchAwait(this, source, book.name, book.author)
.getOrNull()?.let { newBook -> .onFailure {
val toc = WebBook.getChapterListAwait(this, source, newBook) context.toastOnUi("获取书籍出错\n${it.localizedMessage}")
book.changeTo(newBook, toc) }.getOrNull()?.let { newBook ->
appDb.bookDao.insert(newBook) WebBook.getChapterListAwait(this, source, newBook)
appDb.bookChapterDao.insert(*toc.toTypedArray()) .onFailure {
context.toastOnUi("获取目录出错\n${it.localizedMessage}")
}.getOrNull()?.let { toc ->
book.changeTo(newBook, toc)
appDb.bookDao.insert(newBook)
appDb.bookChapterDao.insert(*toc.toTypedArray())
}
} }
} }
}.onFinally { }.onFinally {

@ -231,7 +231,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
changeTo(source, book, toc) changeTo(source, book, toc)
return@execute return@execute
} }

@ -118,7 +118,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
if (book.tocUrl.isBlank()) { if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(this, source, book) WebBook.getBookInfoAwait(this, source, book)
} }
val toc = WebBook.getChapterListAwait(this, source, book) val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()
appDb.bookDao.update(book) appDb.bookDao.update(book)
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())

Loading…
Cancel
Save