diff --git a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt index cb3e4a9f2..df3c96d1c 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt @@ -66,7 +66,7 @@ object BookshelfController { } else { App.db.bookSourceDao.getBookSource(book.origin)?.let { source -> runBlocking { - WebBook(source).getContentAwait(book, chapter) + WebBook(source).getContentAwait(this, book, chapter) }.let { saveBookReadIndex(book, index) returnData.setData(it) diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index d9a9fa49c..ec3d31097 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -11,6 +11,7 @@ import io.legado.app.model.webBook.WebBook import io.legado.app.utils.htmlFormat import io.legado.app.utils.isAbsUrl import io.legado.app.utils.msg +import kotlinx.coroutines.CoroutineScope import java.text.SimpleDateFormat import java.util.* @@ -101,7 +102,7 @@ object Debug { } } - fun startDebug(webBook: WebBook, key: String) { + fun startDebug(scope: CoroutineScope, webBook: WebBook, key: String) { cancelDebug() debugSource = webBook.sourceUrl startTime = System.currentTimeMillis() @@ -111,12 +112,12 @@ object Debug { book.origin = webBook.sourceUrl book.bookUrl = key log(webBook.sourceUrl, "⇒开始访问详情页:$key") - infoDebug(webBook, book) + infoDebug(scope, webBook, book) } key.contains("::") -> { val url = key.substring(key.indexOf("::") + 2) log(webBook.sourceUrl, "⇒开始访问发现页:$url") - exploreDebug(webBook, url) + exploreDebug(scope, webBook, url) } key.startsWith("++")-> { val url = key.substring(2) @@ -124,7 +125,7 @@ object Debug { book.origin = webBook.sourceUrl book.tocUrl = url log(webBook.sourceUrl, "⇒开始访目录页:$url") - tocDebug(webBook, book) + tocDebug(scope, webBook, book) } key.startsWith("--")-> { val url = key.substring(2) @@ -134,23 +135,23 @@ object Debug { val chapter = BookChapter() chapter.title = "调试" chapter.url = url - contentDebug(webBook, book, chapter, null) + contentDebug(scope, webBook, book, chapter, null) } else -> { log(webBook.sourceUrl, "⇒开始搜索关键字:$key") - searchDebug(webBook, key) + searchDebug(scope, webBook, key) } } } - private fun exploreDebug(webBook: WebBook, url: String) { + private fun exploreDebug(scope: CoroutineScope, webBook: WebBook, url: String) { log(debugSource, "︾开始解析发现页") - val explore = webBook.exploreBook(url, 1) + val explore = webBook.exploreBook(scope, url, 1) .onSuccess { exploreBooks -> if (exploreBooks.isNotEmpty()) { log(debugSource, "︽发现页解析完成") log(debugSource, showTime = false) - infoDebug(webBook, exploreBooks[0].toBook()) + infoDebug(scope, webBook, exploreBooks[0].toBook()) } else { log(debugSource, "︽未获取到书籍", state = -1) } @@ -161,14 +162,14 @@ object Debug { tasks.add(explore) } - private fun searchDebug(webBook: WebBook, key: String) { + private fun searchDebug(scope: CoroutineScope, webBook: WebBook, key: String) { log(debugSource, "︾开始解析搜索页") - val search = webBook.searchBook(key, 1) + val search = webBook.searchBook(scope, key, 1) .onSuccess { searchBooks -> if (searchBooks.isNotEmpty()) { log(debugSource, "︽搜索页解析完成") log(debugSource, showTime = false) - infoDebug(webBook, searchBooks[0].toBook()) + infoDebug(scope, webBook, searchBooks[0].toBook()) } else { log(debugSource, "︽未获取到书籍", state = -1) } @@ -179,13 +180,13 @@ object Debug { tasks.add(search) } - private fun infoDebug(webBook: WebBook, book: Book) { + private fun infoDebug(scope: CoroutineScope, webBook: WebBook, book: Book) { log(debugSource, "︾开始解析详情页") - val info = webBook.getBookInfo(book) + val info = webBook.getBookInfo(scope, book) .onSuccess { log(debugSource, "︽详情页解析完成") log(debugSource, showTime = false) - tocDebug(webBook, book) + tocDebug(scope, webBook, book) } .onError { log(debugSource, it.msg, state = -1) @@ -193,15 +194,15 @@ object Debug { tasks.add(info) } - private fun tocDebug(webBook: WebBook, book: Book) { + private fun tocDebug(scope: CoroutineScope, webBook: WebBook, book: Book) { log(debugSource, "︾开始解析目录页") - val chapterList = webBook.getChapterList(book) + val chapterList = webBook.getChapterList(scope, book) .onSuccess { if (it.isNotEmpty()) { log(debugSource, "︽目录页解析完成") log(debugSource, showTime = false) val nextChapterUrl = if (it.size > 1) it[1].url else null - contentDebug(webBook, book, it[0], nextChapterUrl) + contentDebug(scope, webBook, book, it[0], nextChapterUrl) } else { log(debugSource, "︽目录列表为空", state = -1) } @@ -213,13 +214,14 @@ object Debug { } private fun contentDebug( + scope: CoroutineScope, webBook: WebBook, book: Book, bookChapter: BookChapter, nextChapterUrl: String? ) { log(debugSource, "︾开始解析正文页") - val content = webBook.getContent(book, bookChapter, nextChapterUrl) + val content = webBook.getContent(scope, book, bookChapter, nextChapterUrl) .onSuccess { log(debugSource, "︽正文页解析完成", state = 1000) } diff --git a/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt b/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt new file mode 100644 index 000000000..fe1cd36ce --- /dev/null +++ b/app/src/main/java/io/legado/app/model/webBook/PreciseSearch.kt @@ -0,0 +1,20 @@ +package io.legado.app.model.webBook + +import io.legado.app.data.entities.BookSource +import kotlinx.coroutines.CoroutineScope + +/** + * 精准搜索 + */ +object PreciseSearch { + + suspend fun searchFirstBook( + scope: CoroutineScope, + bookSources: List, + name: String, + author: String + ) { + + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt index d94c4df10..95aa3a95e 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt +++ b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt @@ -68,9 +68,9 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C searchIndex++ val source = bookSourceList[searchIndex] val task = WebBook(source).searchBook( + scope, searchKey, searchPage, - scope = scope, context = searchPool!! ).timeout(30000L) .onSuccess(IO) { diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index d864616e9..bafc0ca0f 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -21,9 +21,9 @@ class WebBook(val bookSource: BookSource) { * 搜索 */ fun searchBook( + scope: CoroutineScope, key: String, page: Int? = 1, - scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO, ): Coroutine> { return Coroutine.async(scope, context) { @@ -64,20 +64,20 @@ class WebBook(val bookSource: BookSource) { * 发现 */ fun exploreBook( + scope: CoroutineScope, url: String, page: Int? = 1, - scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO, ): Coroutine> { return Coroutine.async(scope, context) { - exploreBookAwait(url, page, scope) + exploreBookAwait(scope, url, page) } } suspend fun exploreBookAwait( + scope: CoroutineScope = Coroutine.DEFAULT, url: String, page: Int? = 1, - scope: CoroutineScope = Coroutine.DEFAULT ): ArrayList { val variableBook = SearchBook() val analyzeUrl = AnalyzeUrl( @@ -103,19 +103,19 @@ class WebBook(val bookSource: BookSource) { * 书籍信息 */ fun getBookInfo( + scope: CoroutineScope, book: Book, - scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO, canReName: Boolean = true, ): Coroutine { return Coroutine.async(scope, context) { - getBookInfoAwait(book, scope, canReName) + getBookInfoAwait(scope, book, canReName) } } suspend fun getBookInfoAwait( - book: Book, scope: CoroutineScope = Coroutine.DEFAULT, + book: Book, canReName: Boolean = true, ): Book { book.type = bookSource.bookSourceType @@ -145,18 +145,18 @@ class WebBook(val bookSource: BookSource) { * 目录 */ fun getChapterList( + scope: CoroutineScope, book: Book, - scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO ): Coroutine> { return Coroutine.async(scope, context) { - getChapterListAwait(book, scope) + getChapterListAwait(scope, book) } } suspend fun getChapterListAwait( + scope: CoroutineScope = Coroutine.DEFAULT, book: Book, - scope: CoroutineScope = Coroutine.DEFAULT ): List { book.type = bookSource.bookSourceType return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { @@ -176,22 +176,22 @@ class WebBook(val bookSource: BookSource) { * 章节内容 */ fun getContent( + scope: CoroutineScope, book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null, - scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO ): Coroutine { return Coroutine.async(scope, context) { - getContentAwait(book, bookChapter, nextChapterUrl, scope) + getContentAwait(scope, book, bookChapter, nextChapterUrl) } } suspend fun getContentAwait( + scope: CoroutineScope, book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null, - scope: CoroutineScope = Coroutine.DEFAULT ): String { if (bookSource.getContentRule().content.isNullOrEmpty()) { Debug.log(sourceUrl, "⇒正文规则为空,使用章节链接:${bookChapter.url}") diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index b268adea1..d895def23 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -270,7 +270,7 @@ class AudioPlayService : BaseService(), val book = AudioPlay.book val webBook = AudioPlay.webBook if (book != null && webBook != null) { - webBook.getContent(book, chapter, scope = this@AudioPlayService) + webBook.getContent(this@AudioPlayService, book, chapter) .onSuccess { content -> if (content.isEmpty()) { withContext(Main) { diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index 181985df3..06a6dea35 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -184,12 +184,8 @@ class CacheBookService : BaseService() { return@async } if (!BookHelp.hasContent(book, bookChapter)) { - webBook.getContent( - book, - bookChapter, - scope = this, - context = searchPool - ).timeout(60000L) + webBook.getContent(this, book, bookChapter, context = searchPool) + .timeout(60000L) .onError { synchronized(this) { downloadingList.remove(bookChapter.url) diff --git a/app/src/main/java/io/legado/app/service/help/CacheBook.kt b/app/src/main/java/io/legado/app/service/help/CacheBook.kt index 1c69ac09c..782ab3bf8 100644 --- a/app/src/main/java/io/legado/app/service/help/CacheBook.kt +++ b/app/src/main/java/io/legado/app/service/help/CacheBook.kt @@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService import io.legado.app.utils.msg +import kotlinx.coroutines.CoroutineScope import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArraySet @@ -61,6 +62,7 @@ object CacheBook { } fun download( + scope: CoroutineScope, webBook: WebBook, book: Book, chapter: BookChapter, @@ -73,7 +75,7 @@ object CacheBook { downloadMap[book.bookUrl] = CopyOnWriteArraySet() } downloadMap[book.bookUrl]?.add(chapter.index) - webBook.getContent(book, chapter) + webBook.getContent(scope, book, chapter) .onSuccess { content -> if (ReadBook.book?.bookUrl == book.bookUrl) { ReadBook.contentLoadFinish( diff --git a/app/src/main/java/io/legado/app/service/help/CheckSource.kt b/app/src/main/java/io/legado/app/service/help/CheckSource.kt index 88d1dbcca..596662429 100644 --- a/app/src/main/java/io/legado/app/service/help/CheckSource.kt +++ b/app/src/main/java/io/legado/app/service/help/CheckSource.kt @@ -50,7 +50,7 @@ class CheckSource(val source: BookSource) { ): Coroutine<*> { val webBook = WebBook(source) return webBook - .searchBook(keyword, scope = scope, context = context) + .searchBook(scope, keyword, context = context) .timeout(60000L) .onError(Dispatchers.IO) { source.addGroup("失效") diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index c4fb40204..3a331ab33 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -282,7 +282,7 @@ object ReadBook { val book = book val webBook = webBook if (book != null && webBook != null) { - CacheBook.download(webBook, book, chapter) + CacheBook.download(Coroutine.DEFAULT, webBook, book, chapter) } else if (book != null) { contentLoadFinish( book, chapter, "没有书源", resetPageOffset = resetPageOffset diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 793c8b2d6..184663f3d 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -53,7 +53,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) changeDruChapterIndex: ((chapters: List) -> Unit)? = null ) { execute { - AudioPlay.webBook?.getBookInfo(book, this) + AudioPlay.webBook?.getBookInfo(this, book) ?.onSuccess { loadChapterList(book, changeDruChapterIndex) } @@ -65,7 +65,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) changeDruChapterIndex: ((chapters: List) -> Unit)? = null ) { execute { - AudioPlay.webBook?.getChapterList(book, this) + AudioPlay.webBook?.getChapterList(this, book) ?.onSuccess(Dispatchers.IO) { cList -> if (cList.isNotEmpty()) { if (changeDruChapterIndex == null) { diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 305126b36..2bfcffdaa 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -98,7 +98,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application searchIndex++ val source = bookSourceList[searchIndex] val task = WebBook(source) - .searchBook(name, scope = this, context = searchPool!!) + .searchBook(this, name, context = searchPool!!) .timeout(60000L) .onSuccess(Dispatchers.IO) { if (it.isNotEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 7c61b8d62..279cf7d79 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -123,7 +123,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio val source = bookSourceList[searchIndex] val webBook = WebBook(source) val task = webBook - .searchBook(name, scope = this, context = searchPool!!) + .searchBook(this, name, context = searchPool!!) .timeout(60000L) .onSuccess(IO) { it.forEach { searchBook -> @@ -159,7 +159,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } private fun loadBookInfo(webBook: WebBook, book: Book) { - webBook.getBookInfo(book, this) + webBook.getBookInfo(this, book) .onSuccess { if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) { loadBookToc(webBook, book) @@ -175,7 +175,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } private fun loadBookToc(webBook: WebBook, book: Book) { - webBook.getChapterList(book, this) + webBook.getChapterList(this, book) .onSuccess(IO) { chapters -> if (chapters.isNotEmpty()) { book.latestChapterTitle = chapters.last().title diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt index 5781eb555..502fdefa5 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt @@ -34,7 +34,7 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application val source = bookSource val url = exploreUrl if (source != null && url != null) { - WebBook(source).exploreBook(url, page, this) + WebBook(source).exploreBook(this, url, page) .timeout(30000L) .onSuccess(IO) { searchBooks -> booksData.postValue(searchBooks) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index ae0be9479..21f2dd157 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -57,7 +57,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { loadChapter(book, changeDruChapterIndex) } else { App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> - WebBook(bookSource).getBookInfo(book, this, canReName = canReName) + WebBook(bookSource).getBookInfo(this, book, canReName = canReName) .onSuccess(IO) { bookData.postValue(book) if (inBookshelf) { @@ -88,7 +88,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } else { App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> - WebBook(bookSource).getChapterList(book, this) + WebBook(bookSource).getChapterList(this, book) .onSuccess(IO) { if (it.isNotEmpty()) { if (inBookshelf) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 1cdb92986..a6d39b804 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -109,7 +109,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (book.isLocalBook()) { loadChapterList(book, changeDruChapterIndex) } else { - ReadBook.webBook?.getBookInfo(book, this, canReName = false) + ReadBook.webBook?.getBookInfo(this, book, canReName = false) ?.onSuccess { loadChapterList(book, changeDruChapterIndex) } @@ -138,7 +138,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.upMsg("LoadTocError:${it.localizedMessage}") } } else { - ReadBook.webBook?.getChapterList(book, this) + ReadBook.webBook?.getChapterList(this, book) ?.onSuccess(IO) { cList -> if (cList.isNotEmpty()) { if (changeDruChapterIndex == null) { @@ -215,8 +215,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { App.db.bookSourceDao.allTextEnabled.forEach { source -> try { - WebBook(source) - .searchBookAwait(this, name) + WebBook(source).searchBookAwait(this, name) .getOrNull(0)?.let { if (it.name == name && (it.author == author || author == "")) { val book = it.toBook() diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt index 82ce0736c..58c8a5307 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt @@ -28,11 +28,14 @@ class BookSourceDebugModel(application: Application) : BaseViewModel(application } fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) { - webBook?.let { + execute { + Debug.callback = this@BookSourceDebugModel + Debug.startDebug(this, webBook!!, key) + }.onStart { start?.invoke() - Debug.callback = this - Debug.startDebug(it, key) - } ?: error?.invoke() + }.onError { + error?.invoke() + } } override fun printLog(state: Int, msg: String) { diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 242e94baa..eea5da5e1 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -75,7 +75,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } App.db.bookSourceDao.getBookSource(book.origin)?.let { bookSource -> val webBook = WebBook(bookSource) - webBook.getChapterList(book, context = upTocPool) + webBook.getChapterList(this, book, context = upTocPool) .timeout(300000) .onSuccess(IO) { App.db.bookDao.update(book) @@ -115,7 +115,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { var addToCache = false while (!addToCache) { if (CacheBook.downloadCount() < 5) { - CacheBook.download(webBook, book, chapter) + CacheBook.download(this, webBook, book, chapter) addToCache = true } else { delay(1000) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index d8ec442ad..5d3fafb4b 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -43,7 +43,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) origin = bookSource.bookSourceUrl, originName = bookSource.bookSourceName ) - WebBook(bookSource).getBookInfo(book, this) + WebBook(bookSource).getBookInfo(this, book) .onSuccess(IO) { it.order = App.db.bookDao.maxOrder + 1 App.db.bookDao.insert(it) @@ -71,13 +71,6 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) val bookMap = hashMapOf() bookMap["name"] = it.name bookMap["author"] = it.author - bookMap["bookUrl"] = it.bookUrl - bookMap["coverUrl"] = it.coverUrl - bookMap["tocUrl"] = it.tocUrl - bookMap["kind"] = it.kind - bookMap["intro"] = it.getDisplayIntro() - bookMap["origin"] = it.origin - bookMap["originName"] = it.originName exportList.add(bookMap) } GSON.toJson(exportList) @@ -96,23 +89,20 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } } text.isJsonArray() -> { + val bookSources = App.db.bookSourceDao.allEnabled GSON.fromJsonArray>(text)?.forEach { - val book = Book( - bookUrl = it["bookUrl"] ?: "", - name = it["name"] ?: "", - author = it["author"] ?: "", - coverUrl = it["coverUrl"], - tocUrl = it["tocUrl"] ?: "", - kind = it["kind"], - intro = it["intro"] ?: "", - origin = it["origin"] ?: "", - originName = it["originName"] ?: "" - ) - if (groupId > 0) { - book.group = groupId - } - if (App.db.bookDao.getBook(book.name, book.author) == null) { - App.db.bookDao.insert(book) + 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) + } + } } } } diff --git a/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt index 3e68f1fd1..136035a80 100644 --- a/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt @@ -57,7 +57,7 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : } App.db.bookSourceDao.getBookSource(tag)?.let { Debug.callback = this - Debug.startDebug(WebBook(it), key) + Debug.startDebug(this, WebBook(it), key) } } }