Fix Third3FindCrawler

master^2
fengyuecanzhu 2 years ago
parent 2b2999eff7
commit 4d40520e60
No known key found for this signature in database
GPG Key ID: 04B78AD06A9D6E6C
  1. 2
      app/src/main/java/xyz/fycz/myreader/util/utils/ACache.kt
  2. 84
      app/src/main/java/xyz/fycz/myreader/webapi/crawler/source/find/Third3FindCrawler.kt

@ -52,7 +52,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
@JvmOverloads @JvmOverloads
fun get( fun get(
ctx: Context, ctx: Context = App.getmContext(),
cacheName: String = "ACache", cacheName: String = "ACache",
maxSize: Long = MAX_SIZE.toLong(), maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT, maxCount: Int = MAX_COUNT,

@ -18,11 +18,95 @@
package xyz.fycz.myreader.webapi.crawler.source.find package xyz.fycz.myreader.webapi.crawler.source.find
import io.reactivex.Observable
import xyz.fycz.myreader.application.App
import xyz.fycz.myreader.entity.FindKind
import xyz.fycz.myreader.entity.thirdsource.source3.ExploreKind3
import xyz.fycz.myreader.greendao.entity.rule.BookSource import xyz.fycz.myreader.greendao.entity.rule.BookSource
import xyz.fycz.myreader.util.utils.ACache
import xyz.fycz.myreader.util.utils.GSON
import xyz.fycz.myreader.util.utils.fromJsonArray
import xyz.fycz.myreader.util.utils.isJsonArray
/** /**
* @author fengyue * @author fengyue
* @date 2022/1/20 15:33 * @date 2022/1/20 15:33
*/ */
class Third3FindCrawler(source: BookSource) : ThirdFindCrawler(source) { class Third3FindCrawler(source: BookSource) : ThirdFindCrawler(source) {
override fun initData(): Observable<Boolean> {
return Observable.create { emitter ->
val exploreUrl = source.findRule.url
if (exploreUrl.isNullOrBlank()) {
emitter.onNext(false)
emitter.onComplete()
return@create
}
val kinds = arrayListOf<ExploreKind3>()
var ruleStr = exploreUrl
kotlin.runCatching {
if (exploreUrl.startsWith("<js>", false)
|| exploreUrl.startsWith("@js:", false)
) {
val aCache = ACache.get(cacheName = "explore")
ruleStr = aCache.getAsString(source.sourceUrl) ?: ""
if (ruleStr.isBlank()) {
val jsStr = if (exploreUrl.startsWith("@")) {
exploreUrl.substring(4)
} else {
exploreUrl.substring(4, exploreUrl.lastIndexOf("<"))
}
ruleStr = source.evalJS(jsStr).toString().trim()
aCache.put(source.sourceUrl, ruleStr)
}
}
if (ruleStr.isJsonArray()) {
GSON.fromJsonArray<ExploreKind3>(ruleStr)?.let {
kinds.addAll(it)
}
} else {
ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr ->
val kindCfg = kindStr.split("::")
kinds.add(ExploreKind3(kindCfg.getOrNull(0) ?: "", kindCfg.getOrNull(1)))
}
}
var children = arrayListOf<FindKind>()
var groupName = name
var nameCount = 0
kinds.forEach {
if (it.title.isBlank()) {
if (children.size > 0) {
nameCount++
kindsMap[groupName] = children
children = arrayListOf()
}
groupName = "$name[$nameCount]"
} else if (it.url.isNullOrBlank()) {
if (children.size > 0) {
kindsMap[groupName] = children
children = arrayListOf()
}
groupName = it.title.replace("\\s".toRegex(), "")
} else {
val findKindBean = FindKind().apply {
tag = source.sourceUrl
name = it.title.replace("\\s".toRegex(), "")
url = it.url
}
children.add(findKindBean)
}
}
if (children.size > 0) {
kindsMap[groupName] = children
}
emitter.onNext(true)
}.onFailure {
emitter.onNext(false)
kinds.add(ExploreKind3("ERROR:${it.localizedMessage}", it.stackTraceToString()))
if (App.isDebug()) {
it.printStackTrace()
}
}
emitter.onComplete()
}
}
} }
Loading…
Cancel
Save