From 15b3fc98663d274889f8577d9f67fca5d8187b43 Mon Sep 17 00:00:00 2001 From: bapeey <90949336+bapeey@users.noreply.github.com> Date: Fri, 12 Apr 2024 12:38:27 -0500 Subject: [PATCH] MNS: Fix chapters not found again (#2375) insane --- src/es/mangasnosekai/build.gradle | 2 +- .../es/mangasnosekai/MangasNoSekai.kt | 24 ++++++++++-- .../es/mangasnosekai/MangasNoSekaiDto.kt | 38 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekaiDto.kt diff --git a/src/es/mangasnosekai/build.gradle b/src/es/mangasnosekai/build.gradle index 5e093ed94..6c7b582fb 100644 --- a/src/es/mangasnosekai/build.gradle +++ b/src/es/mangasnosekai/build.gradle @@ -3,7 +3,7 @@ ext { extClass = '.MangasNoSekai' themePkg = 'madara' baseUrl = 'https://mangasnosekai.com' - overrideVersionCode = 11 + overrideVersionCode = 12 } apply from: "$rootDir/common.gradle" diff --git a/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekai.kt b/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekai.kt index f9b669b3b..2aef6b0c7 100644 --- a/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekai.kt +++ b/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekai.kt @@ -8,10 +8,12 @@ import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.util.asJsoup +import kotlinx.serialization.decodeFromString import okhttp3.FormBody import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Request import okhttp3.Response +import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element import java.text.SimpleDateFormat @@ -178,7 +180,7 @@ class MangasNoSekai : Madara( intl["order_by_filter_new"] to "new-manga", ) - private fun altChapterRequest(mangaId: String, page: Int, objects: List>): Request { + private fun altChapterRequest(url: String, mangaId: String, page: Int, objects: List>): Request { val form = FormBody.Builder() .add("mangaid", mangaId) .add("page", page.toString()) @@ -187,7 +189,7 @@ class MangasNoSekai : Madara( form.add(key, value) } - return POST("$baseUrl/wp-admin/admin-ajax.php", xhrHeaders, form.build()) + return POST(baseUrl + url, xhrHeaders, form.build()) } private val altChapterListSelector = "body > div > div" @@ -196,9 +198,13 @@ class MangasNoSekai : Madara( val document = response.asJsoup() launchIO { countViews(document) } + val mangaSlug = response.request.url.toString().substringAfter(baseUrl).removeSuffix("/") val coreScript = document.selectFirst("script#wp-manga-js")!!.attr("abs:src") val coreScriptBody = client.newCall(GET(coreScript, headers)).execute().body.string() + val url = URL_REGEX.find(coreScriptBody)?.groupValues?.get(1) + ?: throw Exception("No se pudo obtener la url del capítulo") + val data = DATA_REGEX.find(coreScriptBody)?.groupValues?.get(1)?.trim() ?: throw Exception("No se pudo obtener la data del capítulo") @@ -218,9 +224,13 @@ class MangasNoSekai : Madara( val chapterElements = mutableListOf() var page = 1 do { - val xhrRequest = altChapterRequest(mangaId, page, objects) + val xhrRequest = altChapterRequest(url, mangaId, page, objects) val xhrResponse = client.newCall(xhrRequest).execute() - val xhrDocument = xhrResponse.asJsoup() + val xhrBody = xhrResponse.body.string() + if (xhrBody.startsWith("{")) { + return chaptersFromJson(xhrBody, mangaSlug) + } + val xhrDocument = Jsoup.parse(xhrBody) chapterElements.addAll(xhrDocument.select(altChapterListSelector)) page++ } while (xhrDocument.select(altChapterListSelector).isNotEmpty()) @@ -236,8 +246,14 @@ class MangasNoSekai : Madara( } ?: 0 } + private fun chaptersFromJson(jsonString: String, mangaSlug: String): List { + val result = json.decodeFromString(jsonString) + return result.manga.first().chapters.map { it.toSChapter(mangaSlug) } + } + companion object { val DATA_REGEX = """function\s+loadMoreChapters[\s\S]*?\$.ajax[\s\S]*?data:\s*\{([\s\S]*?)\},?""".toRegex() + val URL_REGEX = """function\s+loadMoreChapters[\s\S]*?\$.ajax[\s\S]*?url:\s*'(.*?)'""".toRegex() val DATA_OBJECTS_REGEX = """\s*(\w+)\s*:\s*(?:(?:'([^']*)'|([^,\r\n]+))\s*,?\s*)""".toRegex() val MANGA_ID_REGEX = """\"manga_id"\s*:\s*"(.*)\"""".toRegex() val POST_ID_REGEX = """\"postId"\s*:\s*"(.*)\"""".toRegex() diff --git a/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekaiDto.kt b/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekaiDto.kt new file mode 100644 index 000000000..cc8fedb9f --- /dev/null +++ b/src/es/mangasnosekai/src/eu/kanade/tachiyomi/extension/es/mangasnosekai/MangasNoSekaiDto.kt @@ -0,0 +1,38 @@ +package eu.kanade.tachiyomi.extension.es.mangasnosekai + +import eu.kanade.tachiyomi.source.model.SChapter +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.Locale + +@Serializable +data class PayloadDto( + val manga: List, +) + +@Serializable +data class MangaDto( + val chapters: List, +) + +private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale("es")) + +@Serializable +data class ChapterDto( + @SerialName("chapter_name") private val name: String, + @SerialName("chapter_slug") private val slug: String, + @SerialName("date_gmt") private val date: String, +) { + + fun toSChapter(mangaSlug: String) = SChapter.create().apply { + name = this@ChapterDto.name + url = "$mangaSlug/$slug" + date_upload = try { + dateFormat.parse(date)?.time ?: 0 + } catch (e: ParseException) { + 0 + } + } +}