Kumanga: Add extra headers and fix ban when fetching chapters (#1681)

Fix ban
This commit is contained in:
bapeey 2024-03-04 05:35:04 -05:00 committed by Draff
parent 99f355d65b
commit d48b870553
3 changed files with 25 additions and 19 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'Kumanga' extName = 'Kumanga'
extClass = '.Kumanga' extClass = '.Kumanga'
extVersionCode = 9 extVersionCode = 10
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"

View File

@ -25,7 +25,6 @@ import uy.kohesive.injekt.injectLazy
import java.net.URL import java.net.URL
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale import java.util.Locale
import kotlin.math.roundToInt
class Kumanga : HttpSource() { class Kumanga : HttpSource() {
@ -43,12 +42,20 @@ class Kumanga : HttpSource() {
private val json: Json by injectLazy() private val json: Json by injectLazy()
override fun headersBuilder(): Headers.Builder = Headers.Builder() override fun headersBuilder() = super.headersBuilder()
.add("Referer", "$baseUrl/") .add("Referer", "$baseUrl/")
.add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8")
.add("Accept-language", "es-419,es;q=0.6")
.add("Cache-Control", "max-age=0")
.add("Sec-Fetch-Dest", "document")
.add("Sec-Fetch-Mode", "navigate")
.add("Sec-Fetch-Site", "none")
.add("Sec-Fetch-User", "?1")
.add("Sec-GPC", "1")
.add("Upgrade-Insecure-Requests", "1")
override val client: OkHttpClient = network.cloudflareClient override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.newBuilder() .rateLimit(1)
.rateLimit(2)
.addInterceptor { chain -> .addInterceptor { chain ->
val request = chain.request() val request = chain.request()
if (!request.url.toString().startsWith(apiUrl)) return@addInterceptor chain.proceed(request) if (!request.url.toString().startsWith(apiUrl)) return@addInterceptor chain.proceed(request)
@ -130,24 +137,23 @@ class Kumanga : HttpSource() {
override fun chapterListParse(response: Response): List<SChapter> = mutableListOf<SChapter>().apply { override fun chapterListParse(response: Response): List<SChapter> = mutableListOf<SChapter>().apply {
var document = response.asJsoup() var document = response.asJsoup()
var location = document.location()
val params = document.select("script:containsData(totCntnts)").toString() val params = document.select("script:containsData(totCntnts)").toString()
val numberChapters = params.substringAfter("totCntnts=").substringBefore(";").toIntOrNull()
val mangaId = params.substringAfter("mid=").substringBefore(";") val mangaId = params.substringAfter("mid=").substringBefore(";")
val mangaSlug = params.substringAfter("slg='").substringBefore("';") val mangaSlug = params.substringAfter("slg='").substringBefore("';")
if (numberChapters != null) { var hasNextPage = document.select("ul.pagination li.next:not(.disabled)").isNotEmpty()
// Calculating total of pages, Kumanga shows 10 chapters per page, total_pages = #chapters / 10
val numberOfPages = (numberChapters / 10.toDouble() + 0.4).roundToInt()
document.select(chapterSelector()).map { add(chapterFromElement(it)) } document.select(chapterSelector()).map { add(chapterFromElement(it)) }
var page = 2 var page = 2
while (page <= numberOfPages) { while (hasNextPage) {
document = client.newCall(GET(baseUrl + getMangaUrl(mangaId, mangaSlug, page))).execute().asJsoup() val pageHeaders = headersBuilder().set("Referer", location).build()
document = client.newCall(GET(baseUrl + getMangaUrl(mangaId, mangaSlug, page), pageHeaders)).execute().asJsoup()
location = document.location()
document.select(chapterSelector()).map { add(chapterFromElement(it)) } document.select(chapterSelector()).map { add(chapterFromElement(it)) }
page++ page++
} hasNextPage = document.select("ul.pagination li.next:not(.disabled)").isNotEmpty()
} else {
throw Exception("No fue posible obtener los capítulos")
} }
} }

View File

@ -22,7 +22,7 @@ class ComicDto(
} }
private fun guessMangaCover(mangaId: String, baseUrl: String) = "$baseUrl/kumathumb.php?src=$mangaId" private fun guessMangaCover(mangaId: String, baseUrl: String) = "$baseUrl/kumathumb.php?src=$mangaId"
private fun createMangaUrl(mangaId: String, mangaSlug: String) = "/manga/$mangaId/p/1/$mangaSlug" private fun createMangaUrl(mangaId: String, mangaSlug: String) = "/manga/$mangaId/$mangaSlug"
} }
@Serializable @Serializable