From 89ec0bd57197e25505629e30a9843f9c83de0b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9D=BF=E5=8D=8E?= Date: Sun, 17 Sep 2023 06:54:34 +0800 Subject: [PATCH] Fix: Manwa.me download and read only showing 3 pages (#17970) --- src/zh/manwa/build.gradle | 2 +- .../tachiyomi/extension/zh/manwa/Manwa.kt | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/zh/manwa/build.gradle b/src/zh/manwa/build.gradle index 7545cb352..f83184cba 100644 --- a/src/zh/manwa/build.gradle +++ b/src/zh/manwa/build.gradle @@ -5,7 +5,7 @@ ext { extName = 'Manwa' pkgNameSuffix = 'zh.manwa' extClass = '.Manwa' - extVersionCode = 3 + extVersionCode = 4 isNsfw = true } diff --git a/src/zh/manwa/src/eu/kanade/tachiyomi/extension/zh/manwa/Manwa.kt b/src/zh/manwa/src/eu/kanade/tachiyomi/extension/zh/manwa/Manwa.kt index 90b6e7b47..ad2d11d8e 100644 --- a/src/zh/manwa/src/eu/kanade/tachiyomi/extension/zh/manwa/Manwa.kt +++ b/src/zh/manwa/src/eu/kanade/tachiyomi/extension/zh/manwa/Manwa.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.extension.zh.manwa import android.app.Application import android.content.SharedPreferences import android.net.Uri +import androidx.preference.CheckBoxPreference import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.network.GET @@ -19,6 +20,8 @@ import kotlinx.serialization.json.int import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive +import okhttp3.Cookie +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.Interceptor import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.OkHttpClient @@ -67,6 +70,8 @@ class Manwa : ParsedHttpSource(), ConfigurableSource { .addNetworkInterceptor(rewriteOctetStream) .build() + private val baseHttpUrl = baseUrl.toHttpUrlOrNull() + // Popular override fun popularMangaRequest(page: Int) = GET("$baseUrl/rank", headers) override fun popularMangaNextPageSelector(): String? = null @@ -99,6 +104,7 @@ class Manwa : ParsedHttpSource(), ConfigurableSource { val totalPage = jsonObject["total"]!!.jsonPrimitive.int return MangasPage(mangas, totalPage > currentPage + 15) } + override fun latestUpdatesNextPageSelector() = throw Exception("Not used") override fun latestUpdatesSelector() = throw Exception("Not used") override fun latestUpdatesFromElement(element: Element) = throw Exception("Not used") @@ -138,6 +144,7 @@ class Manwa : ParsedHttpSource(), ConfigurableSource { url = element.attr("href") name = element.text() } + override fun chapterListParse(response: Response): List { return super.chapterListParse(response).reversed() } @@ -153,7 +160,20 @@ class Manwa : ParsedHttpSource(), ConfigurableSource { } override fun pageListParse(document: Document): List = mutableListOf().apply { - document.select("#cp_img > .img-content > img[data-r-src]").forEachIndexed { index, it -> + val cssQuery = "#cp_img > div.img-content > img[data-r-src]" + val elements = document.select(cssQuery) + if (elements.size == 3) { + val darkReader = document.selectFirst("#cp_img p") + if (darkReader != null) { + if (preferences.getBoolean(AUTO_CLEAR_COOKIE_KEY, false)) { + clearCookies() + } + + throw Exception(darkReader.text()) + } + } + + elements.forEachIndexed { index, it -> add(Page(index, "", it.attr("data-r-src"))) } } @@ -168,11 +188,32 @@ class Manwa : ParsedHttpSource(), ConfigurableSource { entryValues = IMAGE_HOST_ENTRY_VALUES setDefaultValue(IMAGE_HOST_ENTRY_VALUES[0]) }.let { screen.addPreference(it) } + + CheckBoxPreference(screen.context).apply { + key = AUTO_CLEAR_COOKIE_KEY + title = "自动删除 Cookie" + + setDefaultValue(false) + }.let { screen.addPreference(it) } + } + + private fun clearCookies() { + if (baseHttpUrl == null) { + return + } + val cookies = client.cookieJar.loadForRequest(baseHttpUrl) + val obsoletedCookies = cookies.map { + val cookie = Cookie.parse(baseHttpUrl, "${it.name}=; Max-Age=-1")!! + cookie + } + client.cookieJar.saveFromResponse(baseHttpUrl, obsoletedCookies) } companion object { private const val IMAGE_HOST_KEY = "IMG_HOST" private val IMAGE_HOST_ENTRIES = arrayOf("图源1", "图源2", "图源3") private val IMAGE_HOST_ENTRY_VALUES = arrayOf("1", "2", "3") + + private const val AUTO_CLEAR_COOKIE_KEY = "CLEAR_COOKIE" } }