From 358910bafea7160936258f9d81919557cb40579e Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 12 Jul 2022 08:58:55 +0300 Subject: [PATCH] [NineManga] Fix favicons redirect --- .../org/koitharu/kotatsu/parsers/MangaParser.kt | 2 +- .../koitharu/kotatsu/parsers/site/NineMangaParser.kt | 4 ++++ .../koitharu/kotatsu/parsers/util/FaviconParser.kt | 12 ++++++++++-- .../kotatsu/parsers/CloudFlareInterceptor.kt | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt index 32457ddc..0ee27c34 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt @@ -112,7 +112,7 @@ abstract class MangaParser @InternalParsersApi constructor(val source: MangaSour ) open fun getFaviconUrl() = "https://${getDomain()}/favicon.ico" - suspend fun parseFavicons(): Favicons { + open suspend fun parseFavicons(): Favicons { return FaviconParser(context, getDomain()).parseFavicons() } diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NineMangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NineMangaParser.kt index af5e2a55..20c88be1 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NineMangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NineMangaParser.kt @@ -171,6 +171,10 @@ internal abstract class NineMangaParser( } ?: parseFailed("Root not found") } + override suspend fun parseFavicons(): Favicons { + return FaviconParser(context, getDomain()).addHeaders(headers).parseFavicons() + } + private fun parseStatus(status: String) = when { status.contains("Ongoing") -> MangaState.ONGOING status.contains("Completed") -> MangaState.FINISHED diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/FaviconParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/FaviconParser.kt index cd49588a..b7767a33 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/FaviconParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/FaviconParser.kt @@ -1,5 +1,6 @@ package org.koitharu.kotatsu.parsers.util +import okhttp3.Headers import org.jsoup.nodes.Element import org.koitharu.kotatsu.parsers.MangaLoaderContext import org.koitharu.kotatsu.parsers.model.Favicon @@ -8,9 +9,16 @@ import org.koitharu.kotatsu.parsers.util.json.mapJSON class FaviconParser(private val context: MangaLoaderContext, private val domain: String) { + private val headers = Headers.Builder() + + fun addHeaders(headers: Headers): FaviconParser { + this.headers.addAll(headers) + return this + } + suspend fun parseFavicons(): Favicons { val url = "https://$domain" - val doc = context.httpGet(url).parseHtml() + val doc = context.httpGet(url, headers.build()).parseHtml() val result = HashSet() val manifestLink = doc.getElementsByAttributeValue("rel", "manifest").firstOrNull() ?.attrAsAbsoluteUrlOrNull("href") @@ -51,7 +59,7 @@ class FaviconParser(private val context: MangaLoaderContext, private val domain: } private suspend fun parseManifest(url: String): List { - val json = context.httpGet(url).parseJson() + val json = context.httpGet(url, headers.build()).parseJson() val icons = json.getJSONArray("icons") return icons.mapJSON { jo -> Favicon( diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt index 50a3e9d4..5f9a64c3 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt @@ -17,7 +17,7 @@ class CloudFlareInterceptor : Interceptor { if (response.code == HTTP_FORBIDDEN || response.code == HTTP_UNAVAILABLE) { if (response.header(HEADER_SERVER)?.startsWith(SERVER_CLOUDFLARE) == true) { response.closeQuietly() - throw CloudFlareProtectedException(chain.request().url.toString()) + throw CloudFlareProtectedException(response.request.url.toString()) } } return response