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 920be8a00..a8d13edaa 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 @@ -14,7 +14,7 @@ interface BookSourceDao { @Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") fun observeSearch(searchKey: String = ""): DataSource.Factory - @Query("select * from book_sources where enabledExplore = 1 order by customOrder asc") + @Query("select * from book_sources where enabledExplore = 1 and exploreUrl is not null order by customOrder asc") fun liveExplore(): LiveData> @Query("select bookSourceGroup from book_sources") diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 27d16566e..b972cefe8 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -6,14 +6,17 @@ import androidx.room.Ignore import androidx.room.Index import androidx.room.PrimaryKey import io.legado.app.App +import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.userAgent import io.legado.app.data.entities.rule.* +import io.legado.app.help.JsExtensions import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import io.legado.app.utils.getPrefString import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize import java.util.* +import javax.script.SimpleBindings @Parcelize @Entity( @@ -21,24 +24,26 @@ import java.util.* indices = [(Index(value = ["bookSourceUrl"], unique = false))] ) data class BookSource( - var bookSourceName: String = "", // 名称 - var bookSourceGroup: String? = null, // 分组 - @PrimaryKey + var bookSourceName: String = "", // 名称 + var bookSourceGroup: String? = null, // 分组 + @PrimaryKey var bookSourceUrl: String = "", // 地址,包括 http/https - var bookSourceType: Int = 0, // 类型,0 文本,1 音频 - var bookUrlPattern: String? = null, - var customOrder: Int = 0, // 手动排序编号 - var enabled: Boolean = true, // 是否启用 - var enabledExplore: Boolean = true, //启用发现 - var header: String? = null, // header - var loginUrl: String? = null, // 登录地址 - var lastUpdateTime: Long = 0, // 最后更新时间,用于排序 - var weight: Int = 0, // 智能排序的权重 - var ruleExplore: String? = null, // 发现规则 - var ruleSearch: String? = null, // 搜索规则 - var ruleBookInfo: String? = null, // 书籍信息页规则 - var ruleToc: String? = null, // 目录页规则 - var ruleContent: String? = null // 正文页规则 + var bookSourceType: Int = 0, // 类型,0 文本,1 音频 + var bookUrlPattern: String? = null, + var customOrder: Int = 0, // 手动排序编号 + var enabled: Boolean = true, // 是否启用 + var enabledExplore: Boolean = true, //启用发现 + var header: String? = null, // header + var loginUrl: String? = null, // 登录地址 + var lastUpdateTime: Long = 0, // 最后更新时间,用于排序 + var weight: Int = 0, // 智能排序的权重 + var exploreUrl: String? = null, //发现url + var ruleExplore: String? = null, // 发现规则 + var searchUrl: String? = null, //搜索url + var ruleSearch: String? = null, // 搜索规则 + var ruleBookInfo: String? = null, // 书籍信息页规则 + var ruleToc: String? = null, // 目录页规则 + var ruleContent: String? = null // 正文页规则 ) : Parcelable { @Ignore @IgnoredOnParcel @@ -112,4 +117,38 @@ data class BookSource( return contentRuleV!! } + fun getExploreKinds(): ArrayList? { + exploreUrl?.let { + var a = it + if (a.isNotBlank()) { + try { + if (it.startsWith("", false)) { + val bindings = SimpleBindings() + bindings["baseUrl"] = bookSourceUrl + bindings["java"] = JsExtensions() + a = AppConst.SCRIPT_ENGINE.eval( + a.substring(4, a.lastIndexOf("<")), + bindings + ).toString() + } + val exploreKinds = arrayListOf() + val b = a.split("(&&|\n)+".toRegex()) + b.map { c -> + val d = c.split("::") + if (d.size > 1) + exploreKinds.add(ExploreKind(d[0], d[1])) + } + return exploreKinds + } catch (e: Exception) { + e.printStackTrace() + } + } + } + return null + } + + data class ExploreKind( + var title: String, + var url: String + ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt index a20bb60ab..22b267ed3 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt @@ -1,11 +1,6 @@ package io.legado.app.data.entities.rule -import io.legado.app.constant.AppConst -import io.legado.app.help.JsExtensions -import javax.script.SimpleBindings - data class ExploreRule( - var exploreUrl: String? = null, override var bookList: String? = null, override var name: String? = null, override var author: String? = null, @@ -16,40 +11,4 @@ data class ExploreRule( override var bookUrl: String? = null, override var coverUrl: String? = null, override var wordCount: String? = null -) : BookListRule { - - fun getExploreKinds(baseUrl: String): ArrayList? { - exploreUrl?.let { - var a = it - if (a.isNotBlank()) { - try { - if (it.startsWith("", false)) { - val bindings = SimpleBindings() - bindings["baseUrl"] = baseUrl - bindings["java"] = JsExtensions() - a = AppConst.SCRIPT_ENGINE.eval( - a.substring(4, a.lastIndexOf("<")), - bindings - ).toString() - } - val exploreKinds = arrayListOf() - val b = a.split("(&&|\n)+".toRegex()) - b.map { c -> - val d = c.split("::") - if (d.size > 1) - exploreKinds.add(ExploreKind(d[0], d[1])) - } - return exploreKinds - } catch (e: Exception) { - e.printStackTrace() - } - } - } - return null - } - - data class ExploreKind( - var title: String, - var url: String - ) -} \ No newline at end of file +) : BookListRule \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/rule/SearchRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/SearchRule.kt index bef943962..83921e56d 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/SearchRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/SearchRule.kt @@ -1,7 +1,6 @@ package io.legado.app.data.entities.rule data class SearchRule( - var searchUrl: String? = null, override var bookList: String? = null, override var name: String? = null, override var author: String? = null, diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 35f5ef0e1..0ae35a208 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -110,8 +110,12 @@ object Restore { source.bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") source.customOrder = jsonItem.readInt("serialNumber") ?: 0 source.header = OldRule.uaToHeader(jsonItem.readString("httpUserAgent")) + source.searchUrl = OldRule.toNewUrl(jsonItem.readString("ruleSearchUrl")) + source.exploreUrl = OldRule.toNewUrl(jsonItem.readString("ruleFindUrl")) + if (source.exploreUrl.isNullOrBlank()) { + source.enabledExplore = false + } val searchRule = SearchRule( - searchUrl = OldRule.toNewUrl(jsonItem.readString("ruleSearchUrl")), bookList = jsonItem.readString("ruleSearchList"), name = jsonItem.readString("ruleSearchName"), author = jsonItem.readString("ruleSearchAuthor"), @@ -123,7 +127,6 @@ object Restore { ) source.ruleSearch = GSON.toJson(searchRule) val exploreRule = ExploreRule( - exploreUrl = OldRule.toNewUrl(jsonItem.readString("ruleFindUrl")), bookList = jsonItem.readString("ruleFindList"), name = jsonItem.readString("ruleFindName"), author = jsonItem.readString("ruleFindAuthor"), diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 0e460edba..6e00a80bc 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -23,7 +23,7 @@ class WebBook(val bookSource: BookSource) { fun searchBook(key: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) : Coroutine> { return Coroutine.async(scope) { - bookSource.getSearchRule().searchUrl?.let { searchUrl -> + bookSource.searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl( ruleUrl = searchUrl, key = key, diff --git a/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt index 6af95b0f8..2b04d0d5b 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt @@ -43,7 +43,7 @@ class FindBookAdapter(context: Context, private val scope: CoroutineScope, val c rotate_loading.loadingColor = context.accentColor rotate_loading.show() Coroutine.async(scope) { - item.getExploreRule().getExploreKinds(item.bookSourceUrl) + item.getExploreKinds() }.onSuccess { it?.let { gl_child.visible() diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index 6ae96411f..c915b139a 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -140,7 +140,13 @@ class SourceEditActivity : VMBaseActivity(R.layout.activity //搜索 with(bookSource?.getSearchRule()) { searchEditList.clear() - searchEditList.add(EditEntity("searchUrl", this?.searchUrl, R.string.rule_search_url)) + searchEditList.add( + EditEntity( + "searchUrl", + bookSource?.searchUrl, + R.string.rule_search_url + ) + ) searchEditList.add(EditEntity("bookList", this?.bookList, R.string.rule_book_list)) searchEditList.add(EditEntity("name", this?.name, R.string.rule_book_name)) searchEditList.add(EditEntity("author", this?.author, R.string.rule_book_author)) @@ -182,7 +188,13 @@ class SourceEditActivity : VMBaseActivity(R.layout.activity //发现 with(bookSource?.getExploreRule()) { findEditList.clear() - findEditList.add(EditEntity("exploreUrl", this?.exploreUrl, R.string.rule_find_url)) + findEditList.add( + EditEntity( + "exploreUrl", + bookSource?.exploreUrl, + R.string.rule_find_url + ) + ) findEditList.add(EditEntity("bookList", this?.bookList, R.string.rule_book_list)) findEditList.add(EditEntity("name", this?.name, R.string.rule_book_name)) findEditList.add(EditEntity("author", this?.author, R.string.rule_book_author)) @@ -224,7 +236,7 @@ class SourceEditActivity : VMBaseActivity(R.layout.activity for (entity in searchEditList) { with(entity) { when (key) { - "searchUrl" -> searchRule.searchUrl = value + "searchUrl" -> source.searchUrl = value "bookList" -> searchRule.bookList = value "name" -> searchRule.name = value "author" -> searchRule.author = value @@ -241,7 +253,7 @@ class SourceEditActivity : VMBaseActivity(R.layout.activity for (entity in findEditList) { with(entity) { when (key) { - "exploreUrl" -> exploreRule.exploreUrl = value + "exploreUrl" -> source.exploreUrl = value "bookList" -> exploreRule.bookList = value "name" -> exploreRule.name = value "author" -> exploreRule.author = value