[Remanga] Add Accept header to image requests

pull/168/head
Koitharu 3 years ago
parent a3ffecc00f
commit 35695904d1
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -4,6 +4,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import okhttp3.Headers import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import okhttp3.internal.headersContentLength import okhttp3.internal.headersContentLength
import org.json.JSONArray import org.json.JSONArray
@ -24,12 +25,13 @@ private const val PAGE_SIZE_SEARCH = 50
private const val NSFW_ALERT = "сексуальные сцены" private const val NSFW_ALERT = "сексуальные сцены"
private const val NOTHING_FOUND = "Ничего не найдено" private const val NOTHING_FOUND = "Ничего не найдено"
private const val MIN_IMAGE_SIZE = 1024L private const val MIN_IMAGE_SIZE = 1024L
private const val HEADER_ACCEPT = "Accept"
internal abstract class GroupleParser( internal abstract class GroupleParser(
context: MangaLoaderContext, context: MangaLoaderContext,
source: MangaSource, source: MangaSource,
private val siteId: Int, private val siteId: Int,
) : MangaParser(context, source), MangaParserAuthProvider { ) : MangaParser(context, source), MangaParserAuthProvider, Interceptor {
@Volatile @Volatile
private var cachedPagesServer: String? = null private var cachedPagesServer: String? = null
@ -282,6 +284,23 @@ internal abstract class GroupleParser(
} else res } else res
} }
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
if (!request.header(HEADER_ACCEPT).isNullOrEmpty()) {
return chain.proceed(request)
}
val ext = request.url.pathSegments.lastOrNull()?.substringAfterLast('.', "")?.lowercase(Locale.ROOT)
return if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "webp") {
chain.proceed(
request.newBuilder()
.header(HEADER_ACCEPT, "image/webp,image/png;q=0.9,image/jpeg,*/*;q=0.8")
.build(),
)
} else {
chain.proceed(request)
}
}
override fun onCreateConfig(keys: MutableCollection<ConfigKey<*>>) { override fun onCreateConfig(keys: MutableCollection<ConfigKey<*>>) {
super.onCreateConfig(keys) super.onCreateConfig(keys)
keys.add(userAgentKey) keys.add(userAgentKey)

Loading…
Cancel
Save