pull/783/head
gedoor 4 years ago
parent a9fa608da4
commit f47e49f984
  1. 19
      app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt
  2. 25
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  3. 39
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -1,5 +1,6 @@
package io.legado.app.model.webBook package io.legado.app.model.webBook
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -13,8 +14,22 @@ object PreciseSearch {
bookSources: List<BookSource>, bookSources: List<BookSource>,
name: String, name: String,
author: String author: String
) { ): Book? {
bookSources.forEach { bookSource ->
val webBook = WebBook(bookSource)
kotlin.runCatching {
webBook.searchBookAwait(scope, name).firstOrNull {
it.name == name && it.author == author
}?.let {
return if (it.tocUrl.isBlank()) {
webBook.getBookInfoAwait(scope, it.toBook())
} else {
it.toBook()
}
}
}
}
return null
} }
} }

@ -13,10 +13,12 @@ import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.help.storage.BookWebDav import io.legado.app.help.storage.BookWebDav
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.PreciseSearch
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.utils.msg
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -213,23 +215,18 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun autoChangeSource(name: String, author: String) { private fun autoChangeSource(name: String, author: String) {
if (!AppConfig.autoChangeSource) return if (!AppConfig.autoChangeSource) return
execute { execute {
App.db.bookSourceDao.allTextEnabled.forEach { source -> val sources = App.db.bookSourceDao.allTextEnabled
try { val book = PreciseSearch.searchFirstBook(this, sources, name, author)
WebBook(source).searchBookAwait(this, name) if (book != null) {
.getOrNull(0)?.let { book.upInfoFromOld(ReadBook.book)
if (it.name == name && (it.author == author || author == "")) { changeTo(book)
val book = it.toBook() } else {
book.upInfoFromOld(ReadBook.book) throw Exception("自动换源失败")
changeTo(book)
return@execute
}
}
} catch (e: Exception) {
//nothing
}
} }
}.onStart { }.onStart {
ReadBook.upMsg(context.getString(R.string.source_auto_changing)) ReadBook.upMsg(context.getString(R.string.source_auto_changing))
}.onError {
toast(it.msg)
}.onFinally { }.onFinally {
ReadBook.upMsg(null) ReadBook.upMsg(null)
} }

@ -7,6 +7,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.model.webBook.PreciseSearch
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -89,22 +90,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
text.isJsonArray() -> { text.isJsonArray() -> {
val bookSources = App.db.bookSourceDao.allEnabled importBookshelfByJson(text, groupId)
GSON.fromJsonArray<Map<String, String?>>(text)?.forEach {
val name = it["name"] ?: ""
val author = it["author"] ?: ""
bookSources.forEach { bookSource ->
runCatching {
val webBook = WebBook(bookSource)
val searchBooks = webBook.searchBookAwait(this, name)
val searchBook = searchBooks.firstOrNull()
if (searchBook != null && searchBook.name == name && searchBook.author == author) {
val book = webBook.getBookInfoAwait(this, searchBook.toBook())
App.db.bookDao.insert(book)
}
}
}
}
} }
else -> { else -> {
throw Exception("格式不对") throw Exception("格式不对")
@ -115,6 +101,27 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
private fun importBookshelfByJson(json: String, groupId: Long) {
execute {
val bookSources = App.db.bookSourceDao.allEnabled
GSON.fromJsonArray<Map<String, String?>>(json)?.forEach {
val name = it["name"] ?: ""
val author = it["author"] ?: ""
if (name.isNotEmpty() && App.db.bookDao.getBook(name, author) == null) {
val book = PreciseSearch
.searchFirstBook(this, bookSources, name, author)
book?.let {
if (groupId > 0) {
book.group = groupId
}
}
}
}
}.onFinally {
toast(R.string.success)
}
}
fun checkGroup(groups: List<BookGroup>) { fun checkGroup(groups: List<BookGroup>) {
groups.forEach { group -> groups.forEach { group ->
if (group.groupId >= 0 && group.groupId and (group.groupId - 1) != 0L) { if (group.groupId >= 0 && group.groupId and (group.groupId - 1) != 0L) {

Loading…
Cancel
Save