添加通用封面规则

This commit is contained in:
kunfei 2022-03-08 13:55:37 +08:00
parent 2fcbe87eab
commit 10bdb9962c
6 changed files with 57 additions and 18 deletions

View File

@ -26,6 +26,10 @@ interface BaseSource : JsExtensions {
fun getKey(): String
override fun getSource(): BaseSource? {
return this
}
fun loginUi(): List<RowUi>? {
return GSON.fromJsonArray<RowUi>(loginUi)
.onFailure {

View File

@ -79,10 +79,6 @@ data class BookSource(
return bookSourceUrl
}
override fun getSource(): BaseSource {
return this
}
@delegate:Transient
@delegate:Ignore
@IgnoredOnParcel

View File

@ -33,10 +33,6 @@ data class HttpTTS(
return "httpTts:$id"
}
override fun getSource(): BaseSource {
return this
}
@Suppress("MemberVisibilityCanBePrivate")
companion object {

View File

@ -52,10 +52,6 @@ data class RssSource(
return sourceUrl
}
override fun getSource(): BaseSource {
return this
}
override fun equals(other: Any?): Boolean {
if (other is RssSource) {
return other.sourceUrl == sourceUrl

View File

@ -8,22 +8,31 @@ import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.RequestOptions
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.BaseSource
import io.legado.app.help.BlurTransformation
import io.legado.app.help.CacheManager
import io.legado.app.help.config.AppConfig
import io.legado.app.help.glide.ImageLoader
import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.*
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
object BookCover {
private const val coverRuleConfigKey = "legadoCoverRuleConfig"
var drawBookName = true
private set
var drawBookAuthor = true
private set
lateinit var defaultDrawable: Drawable
private set
var coverRuleConfig: CoverRuleConfig? =
GSON.fromJsonObject<CoverRuleConfig>(CacheManager.get(coverRuleConfigKey)).getOrNull()
private val analyzeRule by lazy {
AnalyzeRule()
}
init {
upDefaultCover()
@ -58,4 +67,42 @@ object BookCover {
.apply(RequestOptions.bitmapTransform(BlurTransformation(context, 25)))
}
fun searchCover(name: String, author: String): String? {
val config = coverRuleConfig ?: return null
if (config.searchUrl.isBlank() || config.coverRule.isBlank()) {
return null
}
val analyzeUrl =
AnalyzeUrl(config.searchUrl, name, source = config, headerMapF = config.getHeaderMap())
return runBlocking {
analyzeUrl.getStrResponseAwait().body?.let { body ->
return@let analyzeRule.getString(config.coverRule, body, true)
}
}
}
fun saveCoverRuleConfig(config: CoverRuleConfig) {
coverRuleConfig = config
val json = GSON.toJson(config)
CacheManager.put(coverRuleConfigKey, json)
}
data class CoverRuleConfig(
var searchUrl: String,
var coverRule: String,
override var concurrentRate: String? = null,
override var loginUrl: String? = null,
override var loginUi: String? = null,
override var header: String? = null,
) : BaseSource {
override fun getTag(): String {
return searchUrl
}
override fun getKey(): String {
return searchUrl
}
}
}

View File

@ -24,7 +24,7 @@ import javax.script.SimpleBindings
@Keep
@Suppress("unused", "RegExpRedundantEscape", "MemberVisibilityCanBePrivate")
class AnalyzeRule(
val ruleData: RuleDataInterface,
val ruleData: RuleDataInterface? = null,
private val source: BaseSource? = null
) : JsExtensions {
@ -613,7 +613,7 @@ class AnalyzeRule(
fun put(key: String, value: String): String {
chapter?.putVariable(key, value)
?: book?.putVariable(key, value)
?: ruleData.putVariable(key, value)
?: ruleData?.putVariable(key, value)
return value
}
@ -628,7 +628,7 @@ class AnalyzeRule(
}
return chapter?.variableMap?.get(key)
?: book?.variableMap?.get(key)
?: ruleData.variableMap[key]
?: ruleData?.variableMap?.get(key)
?: ""
}