From 03e2fce493c9fcc971168230243d9b2eedb6defb Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Mon, 20 Jun 2022 23:13:34 +0800 Subject: [PATCH] MDB: prefer WebP images and change library source (#12254) --- .../overrides/mdb/manhuadb/src/ManhuaDB.kt | 43 ++++++++++++++++--- .../overrides/mdb/maofly/additional.gradle | 2 +- .../tachiyomi/multisrc/mdb/MDBGenerator.kt | 2 +- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/multisrc/overrides/mdb/manhuadb/src/ManhuaDB.kt b/multisrc/overrides/mdb/manhuadb/src/ManhuaDB.kt index b63b55069..387d599c4 100644 --- a/multisrc/overrides/mdb/manhuadb/src/ManhuaDB.kt +++ b/multisrc/overrides/mdb/manhuadb/src/ManhuaDB.kt @@ -1,18 +1,24 @@ package eu.kanade.tachiyomi.extension.zh.manhuadb +import android.app.Application +import android.content.SharedPreferences import android.util.Base64 +import androidx.preference.PreferenceScreen +import androidx.preference.SwitchPreferenceCompat import eu.kanade.tachiyomi.multisrc.mdb.MDB +import eu.kanade.tachiyomi.source.ConfigurableSource +import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.jsonPrimitive import okhttp3.Response import org.jsoup.nodes.Element import org.jsoup.select.Evaluator import org.jsoup.select.QueryParser +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy -class ManhuaDB : MDB("漫画DB", "https://www.manhuadb.com") { +class ManhuaDB : MDB("漫画DB", "https://www.manhuadb.com"), ConfigurableSource { override val supportsLatest = false @@ -36,10 +42,37 @@ class ManhuaDB : MDB("漫画DB", "https://www.manhuadb.com") { // https://www.manhuadb.com/assets/js/vg-read.js override fun parseImages(imgData: String, readerConfig: Element): List { - val list: List = Base64.decode(imgData, Base64.DEFAULT) + val list: List = Base64.decode(imgData, Base64.DEFAULT) .let { json.decodeFromString(String(it)) } val host = readerConfig.attr("data-host") val dir = readerConfig.attr("data-img_pre") - return list.map { host + dir + it["img"]!!.jsonPrimitive.content } + val useWebp = preferences.getBoolean(WEBP_PREF, true) + return list.map { + host + dir + if (useWebp && it.img_webp != null) it.img_webp else it.img + } + } + + @Serializable + data class Image(val img: String, val img_webp: String? = null) + + private val preferences: SharedPreferences by lazy { + Injekt.get().getSharedPreferences("source_$id", 0x0000) + } + + override fun setupPreferenceScreen(screen: PreferenceScreen) { + SwitchPreferenceCompat(screen.context).apply { + key = WEBP_PREF + title = "优先使用 WebP 图片格式" + summary = "默认开启,可以节省网站流量" + setDefaultValue(true) + setOnPreferenceChangeListener { _, newValue -> + preferences.edit().putBoolean(WEBP_PREF, newValue as Boolean).apply() + true + } + }.let { screen.addPreference(it) } + } + + companion object { + private const val WEBP_PREF = "WEBP" } } diff --git a/multisrc/overrides/mdb/maofly/additional.gradle b/multisrc/overrides/mdb/maofly/additional.gradle index cd52f14a8..c129427e8 100644 --- a/multisrc/overrides/mdb/maofly/additional.gradle +++ b/multisrc/overrides/mdb/maofly/additional.gradle @@ -1,3 +1,3 @@ dependencies { - implementation 'com.github.softwarevidal:lz-string4java:lz-string4java-1.0.0' + implementation 'com.github.wajda:lzstring4java:0.1' } diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mdb/MDBGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mdb/MDBGenerator.kt index fbc6b9a54..d088790ce 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mdb/MDBGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mdb/MDBGenerator.kt @@ -8,7 +8,7 @@ class MDBGenerator : ThemeSourceGenerator { override val themePkg = "mdb" override val baseVersionCode = 1 override val sources = listOf( - SingleLang("ManhuaDB", "https://www.manhuadb.com", "zh", sourceName = "漫画DB", overrideVersionCode = 3), + SingleLang("ManhuaDB", "https://www.manhuadb.com", "zh", sourceName = "漫画DB", overrideVersionCode = 4), SingleLang("Maofly", "https://www.maofly.com", "zh", sourceName = "漫画猫", overrideVersionCode = 1), )