From 0817a82f36ef7e4c01ad2582cba2e1ccd7dc611b Mon Sep 17 00:00:00 2001 From: Draken <131387159+dragonx943@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:14:31 +0700 Subject: [PATCH] MadaraDex: Fix access denied errors in getPages Co-authored-by: Koitharu --- .../parsers/site/all/MyReadingManga.kt | 3 + .../parsers/site/madara/en/MadaraDex.kt | 77 +++++++++++++++++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/MyReadingManga.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/MyReadingManga.kt index 49955bbc..e2d582db 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/MyReadingManga.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/all/MyReadingManga.kt @@ -159,6 +159,7 @@ internal class MyReadingManga(context: MangaLoaderContext) : append(page) } + // order append("/?ep_sort=") when (order) { SortOrder.NEWEST -> append("date") @@ -166,6 +167,8 @@ internal class MyReadingManga(context: MangaLoaderContext) : else -> append("") } + + // fix order append("&s=") if (!filter.query.isNullOrEmpty()) { append(filter.query.splitByWhitespace().joinToString(separator = "+")) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/madara/en/MadaraDex.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/madara/en/MadaraDex.kt index 79fd8ce1..059c6333 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/madara/en/MadaraDex.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/madara/en/MadaraDex.kt @@ -1,21 +1,88 @@ package org.koitharu.kotatsu.parsers.site.madara.en +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.Interceptor +import okhttp3.Response import org.koitharu.kotatsu.parsers.MangaLoaderContext import org.koitharu.kotatsu.parsers.MangaSourceParser +import org.koitharu.kotatsu.parsers.config.ConfigKey +import org.koitharu.kotatsu.parsers.exception.ParseException import org.koitharu.kotatsu.parsers.model.ContentType +import org.koitharu.kotatsu.parsers.model.MangaChapter +import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaParserSource +import org.koitharu.kotatsu.parsers.network.UserAgents import org.koitharu.kotatsu.parsers.site.madara.MadaraParser +import org.koitharu.kotatsu.parsers.util.* @MangaSourceParser("MADARADEX", "MadaraDex", "en", ContentType.HENTAI) internal class MadaraDex(context: MangaLoaderContext) : - MadaraParser(context, MangaParserSource.MADARADEX, "madaradex.org") { + MadaraParser(context, MangaParserSource.MADARADEX, "madaradex.org") { + + override fun onCreateConfig(keys: MutableCollection>) { + super.onCreateConfig(keys) + keys.remove(userAgentKey) + } override fun getRequestHeaders() = super.getRequestHeaders().newBuilder() - .add("Referer", "https://$domain/") .add("sec-fetch-site", "same-site") .build() - override val listUrl = "title/" - override val tagPrefix = "genre/" - override val postReq = true + override val authUrl: String + get() = "https://${domain}" + + override suspend fun isAuthorized(): Boolean { + return context.cookieJar.getCookies(domain).any { + it.name.contains("cm_uaid") + } + } + + override val listUrl = "title/" + override val tagPrefix = "genre/" + override val postReq = true + + override suspend fun getPages(chapter: MangaChapter): List { + val fullUrl = chapter.url.toAbsoluteUrl(domain) + val doc = webClient.httpGet(fullUrl).parseHtml() + val root = doc.body().selectFirst(selectBodyPage) + ?: throw ParseException("No image found, try to log in", fullUrl) + + return root.select(selectPage).flatMap { div -> + div.selectOrThrow("img").map { img -> + val fragUrl = img.requireSrc().toRelativeUrl(domain).toHttpUrl().newBuilder() + .fragment(F_URL + fullUrl) + .build() + val cleanUrl = fragUrl.newBuilder().fragment(null).build() + MangaPage( + id = generateUid(cleanUrl.toString()), + url = fragUrl.toString(), + preview = null, + source = source, + ) + } + } + } + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + val url = request.url + val fullUrl = url.fragment?.substringAfter(F_URL) + return if (!fullUrl.isNullOrEmpty()) { + val cleanUrl = url.newBuilder().fragment(null).toString() + val newReq = request.newBuilder() + .header("sec-fetch-site", "same-site") + .header("Referer", fullUrl) + .header("User-Agent", UserAgents.CHROME_DESKTOP) + .header("Cookie", context.cookieJar.getCookies(fullUrl).toString()) + .url(cleanUrl) + .build() + chain.proceed(newReq) + } else { + super.intercept(chain) + } + } + + private companion object { + const val F_URL = "fullUrl=" + } }