From 9011b95a32d0f4fbea739ad40cb3d7afd2dd72e3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 1 Feb 2020 21:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 3 ++ .../ui/main/bookshelf/BookshelfViewModel.kt | 44 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index f76ed3d13..159622898 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -53,6 +53,9 @@ interface BookSourceDao { @Query("select * from book_sources where enabled = 1 and bookSourceGroup like '%' || :group || '%'") fun getEnabledByGroup(group: String): List + @get:Query("select * from book_sources where bookUrlPattern is not null || bookUrlPattern <> ''") + val hasBookUrlPattern: List + @get:Query("select * from book_sources where bookSourceGroup is null or bookSourceGroup = ''") val noGroup: List 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 50e77a765..b41031484 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 @@ -2,8 +2,14 @@ package io.legado.app.ui.main.bookshelf import android.app.Application import io.legado.app.App +import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup +import io.legado.app.data.entities.BookSource +import io.legado.app.model.WebBook +import io.legado.app.utils.NetworkUtils +import kotlinx.coroutines.Dispatchers.IO class BookshelfViewModel(application: Application) : BaseViewModel(application) { @@ -31,8 +37,42 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } } - fun addBookByUrl(bookUrl: String) { - + fun addBookByUrl(bookUrls: String) { + execute { + var hasBookUrlPattern: List? = null + val urls = bookUrls.split("\n") + for (url in urls) { + val bookUrl = url.trim() + if (bookUrl.isEmpty()) continue + App.db.bookDao().getBook(bookUrl) ?: continue + val baseUrl = NetworkUtils.getBaseUrl(bookUrl) ?: continue + var source = App.db.bookSourceDao().getBookSource(baseUrl) + if (source == null) { + if (hasBookUrlPattern == null) { + hasBookUrlPattern = App.db.bookSourceDao().hasBookUrlPattern + } + hasBookUrlPattern.forEach { bookSource -> + if (bookUrl.matches(bookSource.bookUrlPattern!!.toRegex())) { + source = bookSource + return@forEach + } + } + } + source?.let { bookSource -> + val book = Book(bookUrl = bookUrl) + WebBook(bookSource).getBookInfo(book, this) + .onSuccess(IO) { + it?.let { book -> + App.db.bookDao().insert(book) + } + } + } + } + }.onSuccess { + toast(R.string.success) + }.onError { + toast(it.localizedMessage ?: "ERROR") + } } }