This commit is contained in:
Horis
2025-05-06 16:06:45 +08:00
parent 262d3d02b4
commit 9e1cedd683
6 changed files with 72 additions and 9 deletions
@@ -148,12 +148,12 @@ data class BookSource(
return rule return rule
} }
fun getReviewRule(): ReviewRule { // fun getReviewRule(): ReviewRule {
ruleReview?.let { return it } // ruleReview?.let { return it }
val rule = ReviewRule() // val rule = ReviewRule()
ruleReview = rule // ruleReview = rule
return rule // return rule
} // }
fun getDisPlayNameGroup(): String { fun getDisPlayNameGroup(): String {
return if (bookSourceGroup.isNullOrBlank()) { return if (bookSourceGroup.isNullOrBlank()) {
@@ -104,6 +104,13 @@ suspend fun BookSource.clearExploreKindsCache() {
} }
} }
fun BookSource.exploreKindsJson(): String {
val exploreKindsKey = getExploreKindsKey()
return aCache.getAsString(exploreKindsKey)?.takeIf { it.isJsonArray() }
?: exploreUrl.takeIf { it.isJsonArray() }
?: ""
}
fun BookSource.getBookType(): Int { fun BookSource.getBookType(): Int {
return when (bookSourceType) { return when (bookSourceType) {
BookSourceType.file -> BookType.text or BookType.webFile BookSourceType.file -> BookType.text or BookType.webFile
@@ -17,8 +17,10 @@ import io.legado.app.help.CacheManager
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.CookieStore
import io.legado.app.help.source.getShareScope import io.legado.app.help.source.getShareScope
import io.legado.app.model.Debug
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.GSONStrict
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getOrPutLimit import io.legado.app.utils.getOrPutLimit
@@ -78,6 +80,8 @@ class AnalyzeRule(
private var coroutineContext: CoroutineContext = EmptyCoroutineContext private var coroutineContext: CoroutineContext = EmptyCoroutineContext
private var loggedNonStandardJSON = false
@JvmOverloads @JvmOverloads
fun setContent(content: Any?, baseUrl: String? = null): AnalyzeRule { fun setContent(content: Any?, baseUrl: String? = null): AnalyzeRule {
if (content == null) throw AssertionError("内容不可空(Content cannot be null") if (content == null) throw AssertionError("内容不可空(Content cannot be null")
@@ -408,9 +412,20 @@ class AnalyzeRule(
val putMatcher = putPattern.matcher(vRuleStr) val putMatcher = putPattern.matcher(vRuleStr)
while (putMatcher.find()) { while (putMatcher.find()) {
vRuleStr = vRuleStr.replace(putMatcher.group(), "") vRuleStr = vRuleStr.replace(putMatcher.group(), "")
GSON.fromJsonObject<Map<String, String>>(putMatcher.group(1)) val putJsonStr = putMatcher.group(1)
val putJson = GSONStrict.fromJsonObject<Map<String, String>>(putJsonStr)
.getOrNull()
if (putJson != null) {
putMap.putAll(putJson)
continue
}
GSON.fromJsonObject<Map<String, String>>(putJsonStr)
.getOrNull() .getOrNull()
?.let { ?.let {
if (!loggedNonStandardJSON) {
Debug.log("≡@put 规则 JSON 格式不规范,请改为规范格式")
loggedNonStandardJSON = true
}
putMap.putAll(it) putMap.putAll(it)
} }
} }
@@ -39,8 +39,10 @@ import io.legado.app.help.http.postForm
import io.legado.app.help.http.postJson import io.legado.app.help.http.postJson
import io.legado.app.help.http.postMultipart import io.legado.app.help.http.postMultipart
import io.legado.app.help.source.getShareScope import io.legado.app.help.source.getShareScope
import io.legado.app.model.Debug
import io.legado.app.utils.EncoderUtils import io.legado.app.utils.EncoderUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.GSONStrict
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
@@ -219,8 +221,15 @@ class AnalyzeUrl(
baseUrl = it baseUrl = it
} }
if (urlNoOption.length != ruleUrl.length) { if (urlNoOption.length != ruleUrl.length) {
GSON.fromJsonObject<UrlOption>(ruleUrl.substring(urlMatcher.end())).getOrNull() val urlOptionStr = ruleUrl.substring(urlMatcher.end())
?.let { option -> var urlOption = GSONStrict.fromJsonObject<UrlOption>(urlOptionStr).getOrNull()
if (urlOption == null) {
urlOption = GSON.fromJsonObject<UrlOption>(urlOptionStr).getOrNull()
if (urlOption != null) {
Debug.log("≡链接参数 JSON 格式不规范,请改为规范格式")
}
}
urlOption?.let { option ->
option.getMethod()?.let { option.getMethod()?.let {
if (it.equals("POST", true)) method = RequestMethod.POST if (it.equals("POST", true)) method = RequestMethod.POST
} }
@@ -5,17 +5,22 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.rule.BookListRule import io.legado.app.data.entities.rule.BookListRule
import io.legado.app.data.entities.rule.ExploreKind
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.book.BookHelp import io.legado.app.help.book.BookHelp
import io.legado.app.help.source.exploreKindsJson
import io.legado.app.help.source.getBookType import io.legado.app.help.source.getBookType
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext import io.legado.app.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleData import io.legado.app.model.analyzeRule.RuleData
import io.legado.app.utils.GSON
import io.legado.app.utils.GSONStrict
import io.legado.app.utils.HtmlFormatter import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat import io.legado.app.utils.StringUtils.wordCountFormat
import io.legado.app.utils.fromJsonArray
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import splitties.init.appCtx import splitties.init.appCtx
import kotlin.coroutines.coroutineContext import kotlin.coroutines.coroutineContext
@@ -50,6 +55,9 @@ object BookList {
analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.setContent(body).setBaseUrl(baseUrl)
analyzeRule.setRedirectUrl(baseUrl) analyzeRule.setRedirectUrl(baseUrl)
analyzeRule.setCoroutineContext(coroutineContext) analyzeRule.setCoroutineContext(coroutineContext)
if (!isSearch) {
checkExploreJson(bookSource)
}
if (isSearch) bookSource.bookUrlPattern?.let { if (isSearch) bookSource.bookUrlPattern?.let {
coroutineContext.ensureActive() coroutineContext.ensureActive()
if (baseUrl.matches(it.toRegex())) { if (baseUrl.matches(it.toRegex())) {
@@ -279,4 +287,21 @@ object BookList {
return null return null
} }
private fun checkExploreJson(bookSource: BookSource) {
if (Debug.callback == null) {
return
}
val json = bookSource.exploreKindsJson()
if (json.isEmpty()) {
return
}
val kinds = GSONStrict.fromJsonArray<ExploreKind>(json).getOrNull()
if (kinds != null) {
return
}
GSON.fromJsonArray<ExploreKind>(json).getOrNull()?.let {
Debug.log("≡发现地址规则 JSON 格式不规范,请改为规范格式")
}
}
} }
@@ -7,6 +7,7 @@ import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement import com.google.gson.JsonElement
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import com.google.gson.JsonSyntaxException import com.google.gson.JsonSyntaxException
import com.google.gson.Strictness
import com.google.gson.ToNumberPolicy import com.google.gson.ToNumberPolicy
import com.google.gson.internal.LinkedTreeMap import com.google.gson.internal.LinkedTreeMap
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
@@ -49,6 +50,12 @@ val GSON: Gson by lazy {
.create() .create()
} }
val GSONStrict: Gson by lazy {
GSON.newBuilder()
.setStrictness(Strictness.STRICT)
.create()
}
inline fun <reified T> genericType(): Type = object : TypeToken<T>() {}.type inline fun <reified T> genericType(): Type = object : TypeToken<T>() {}.type
inline fun <reified T> Gson.fromJsonObject(json: String?): Result<T> { inline fun <reified T> Gson.fromJsonObject(json: String?): Result<T> {