pull/32/head
kunfei 5 years ago
parent a692416bbf
commit 4a34a78816
  1. 2
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 73
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 43
      app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt
  4. 1
      app/src/main/java/io/legado/app/data/entities/rule/SearchRule.kt
  5. 7
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  6. 2
      app/src/main/java/io/legado/app/model/WebBook.kt
  7. 2
      app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt
  8. 20
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.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<Int, BookSource>
@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<List<BookSource>>
@Query("select bookSourceGroup from book_sources")

@ -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<ExploreKind>? {
exploreUrl?.let {
var a = it
if (a.isNotBlank()) {
try {
if (it.startsWith("<js>", 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<ExploreKind>()
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
)
}

@ -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<ExploreKind>? {
exploreUrl?.let {
var a = it
if (a.isNotBlank()) {
try {
if (it.startsWith("<js>", 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<ExploreKind>()
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
)
}
) : BookListRule

@ -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,

@ -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"),

@ -23,7 +23,7 @@ class WebBook(val bookSource: BookSource) {
fun searchBook(key: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT)
: Coroutine<List<SearchBook>> {
return Coroutine.async(scope) {
bookSource.getSearchRule().searchUrl?.let { searchUrl ->
bookSource.searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(
ruleUrl = searchUrl,
key = key,

@ -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()

@ -140,7 +140,13 @@ class SourceEditActivity : VMBaseActivity<SourceEditViewModel>(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<SourceEditViewModel>(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<SourceEditViewModel>(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<SourceEditViewModel>(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

Loading…
Cancel
Save