diff --git a/app/src/main/java/xyz/fycz/myreader/util/utils/ACache.kt b/app/src/main/java/xyz/fycz/myreader/util/utils/ACache.kt index 58d8835..71ed2ad 100644 --- a/app/src/main/java/xyz/fycz/myreader/util/utils/ACache.kt +++ b/app/src/main/java/xyz/fycz/myreader/util/utils/ACache.kt @@ -52,7 +52,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) @JvmOverloads fun get( - ctx: Context, + ctx: Context = App.getmContext(), cacheName: String = "ACache", maxSize: Long = MAX_SIZE.toLong(), maxCount: Int = MAX_COUNT, diff --git a/app/src/main/java/xyz/fycz/myreader/webapi/crawler/source/find/Third3FindCrawler.kt b/app/src/main/java/xyz/fycz/myreader/webapi/crawler/source/find/Third3FindCrawler.kt index 3875cc8..83d74de 100644 --- a/app/src/main/java/xyz/fycz/myreader/webapi/crawler/source/find/Third3FindCrawler.kt +++ b/app/src/main/java/xyz/fycz/myreader/webapi/crawler/source/find/Third3FindCrawler.kt @@ -18,11 +18,95 @@ 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.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 * @date 2022/1/20 15:33 */ class Third3FindCrawler(source: BookSource) : ThirdFindCrawler(source) { + override fun initData(): Observable { + return Observable.create { emitter -> + val exploreUrl = source.findRule.url + if (exploreUrl.isNullOrBlank()) { + emitter.onNext(false) + emitter.onComplete() + return@create + } + val kinds = arrayListOf() + var ruleStr = exploreUrl + kotlin.runCatching { + if (exploreUrl.startsWith("", 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(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() + 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() + } + } } \ No newline at end of file