This commit is contained in:
kunfei 2022-09-19 18:29:59 +08:00
parent 26da15e79a
commit b484a1096e
5 changed files with 78 additions and 57 deletions

View File

@ -8,6 +8,10 @@ import io.legado.app.constant.BookType
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.help.SourceAnalyzer import io.legado.app.help.SourceAnalyzer
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import java.io.InputStream import java.io.InputStream
@ -89,46 +93,66 @@ data class BookSource(
return bookSourceUrl return bookSourceUrl
} }
@delegate:Transient @Ignore
@delegate:Ignore
@IgnoredOnParcel @IgnoredOnParcel
val exploreKinds: List<ExploreKind> by lazy { private var exploreKinds: List<ExploreKind>? = null
val exploreUrl = exploreUrl ?: return@lazy emptyList()
val kinds = arrayListOf<ExploreKind>() @Ignore
var ruleStr = exploreUrl @IgnoredOnParcel
if (ruleStr.isNotBlank()) { private val mutex = Mutex()
kotlin.runCatching {
if (exploreUrl.startsWith("<js>", false) suspend fun exploreKinds(): List<ExploreKind> {
|| exploreUrl.startsWith("@js:", false) exploreKinds?.let { return it }
) { val exploreUrl = exploreUrl
val aCache = ACache.get("explore") if (exploreUrl.isNullOrBlank()) {
ruleStr = aCache.getAsString(bookSourceUrl) ?: "" return emptyList()
if (ruleStr.isBlank()) { }
val jsStr = if (exploreUrl.startsWith("@")) { mutex.withLock {
exploreUrl.substring(4) exploreKinds?.let { return it }
} else { val kinds = arrayListOf<ExploreKind>()
exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) var ruleStr: String = exploreUrl
} withContext(IO) {
ruleStr = evalJS(jsStr).toString().trim() kotlin.runCatching {
aCache.put(bookSourceUrl, ruleStr) if (exploreUrl.startsWith("<js>", false)
} || exploreUrl.startsWith("@js:", false)
} ) {
if (ruleStr.isJsonArray()) { val aCache = ACache.get("explore")
GSON.fromJsonArray<ExploreKind>(ruleStr).getOrThrow()?.let { ruleStr = aCache.getAsString(bookSourceUrl) ?: ""
kinds.addAll(it) if (ruleStr.isBlank()) {
} val jsStr = if (exploreUrl.startsWith("@")) {
} else { exploreUrl.substring(4)
ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr -> } else {
val kindCfg = kindStr.split("::") exploreUrl.substring(4, exploreUrl.lastIndexOf("<"))
kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1))) }
} ruleStr = evalJS(jsStr).toString().trim()
} aCache.put(bookSourceUrl, ruleStr)
}.onFailure { }
kinds.add(ExploreKind("ERROR:${it.localizedMessage}", it.stackTraceToString())) }
it.printOnDebug() if (ruleStr.isJsonArray()) {
} GSON.fromJsonArray<ExploreKind>(ruleStr).getOrThrow()?.let {
kinds.addAll(it)
}
} else {
ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr ->
val kindCfg = kindStr.split("::")
kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1)))
}
}
}.onFailure {
kinds.add(ExploreKind("ERROR:${it.localizedMessage}", it.stackTraceToString()))
it.printOnDebug()
}
}
exploreKinds = kinds
return kinds
}
}
suspend fun clearExploreKindsCache() {
withContext(IO) {
ACache.get("explore").remove(bookSourceUrl)
exploreKinds = null
} }
return@lazy kinds
} }
override fun hashCode(): Int { override fun hashCode(): Int {

View File

@ -155,7 +155,7 @@ class CheckSourceService : BaseService() {
} }
//校验发现书籍 //校验发现书籍
if (CheckSource.checkDiscovery) { if (CheckSource.checkDiscovery) {
val exs = source.exploreKinds val exs = source.exploreKinds()
var url: String? = null var url: String? = null
for (ex in exs) { for (ex in exs) {
url = ex.url url = ex.url

View File

@ -88,16 +88,6 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
binding.textMy.text = it binding.textMy.text = it
} }
} }
viewModel.bookSource?.exploreKinds?.firstOrNull {
!it.url.isNullOrBlank()
}?.let {
binding.textFx.text = "${it.title}::${it.url}"
if (it.title.startsWith("ERROR:")) {
adapter.addItem("获取发现出错\n${it.url}")
openOrCloseHelp(false)
searchView.clearFocus()
}
}
binding.textMy.onClick { binding.textMy.onClick {
searchView.setQuery(binding.textMy.text, true) searchView.setQuery(binding.textMy.text, true)
} }
@ -138,6 +128,18 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
} }
} }
} }
launch {
viewModel.bookSource?.exploreKinds()?.firstOrNull {
!it.url.isNullOrBlank()
}?.let {
binding.textFx.text = "${it.title}::${it.url}"
if (it.title.startsWith("ERROR:")) {
adapter.addItem("获取发现出错\n${it.url}")
openOrCloseHelp(false)
searchView.clearFocus()
}
}
}
} }
/** /**

View File

@ -56,7 +56,7 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
callBack.scrollTo(scrollTo) callBack.scrollTo(scrollTo)
} }
Coroutine.async(callBack.scope) { Coroutine.async(callBack.scope) {
item.exploreKinds item.exploreKinds()
}.onSuccess { kindList -> }.onSuccess { kindList ->
upKindList(flexbox, item.bookSourceUrl, kindList) upKindList(flexbox, item.bookSourceUrl, kindList)
}.onFinally { }.onFinally {
@ -167,9 +167,9 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
putExtra("key", source.bookSourceUrl) putExtra("key", source.bookSourceUrl)
} }
R.id.menu_refresh -> Coroutine.async(callBack.scope) { R.id.menu_refresh -> Coroutine.async(callBack.scope) {
ACache.get("explore").remove(source.bookSourceUrl) source.clearExploreKindsCache()
}.onSuccess { }.onSuccess {
callBack.refreshData() notifyItemChanged(position)
} }
R.id.menu_del -> callBack.deleteSource(source) R.id.menu_del -> callBack.deleteSource(source)
} }
@ -181,7 +181,6 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
interface CallBack { interface CallBack {
val scope: CoroutineScope val scope: CoroutineScope
fun refreshData()
fun scrollTo(pos: Int) fun scrollTo(pos: Int)
fun openExplore(sourceUrl: String, title: String, exploreUrl: String?) fun openExplore(sourceUrl: String, title: String, exploreUrl: String?)
fun editSource(sourceUrl: String) fun editSource(sourceUrl: String)

View File

@ -157,10 +157,6 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
} }
} }
override fun refreshData() {
upExploreData(searchView.query?.toString())
}
override fun scrollTo(pos: Int) { override fun scrollTo(pos: Int) {
(binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0) (binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0)
} }