From 3873976886c41f44a1012551dee5af9cc1caf093 Mon Sep 17 00:00:00 2001
From: Vetle Ledaal <vetle.ledaal@gmail.com>
Date: Sun, 12 May 2024 04:25:54 +0000
Subject: [PATCH] PTNF: fix mangaId, fix pagination, script order (#2964)

* PTNF: fix mangaId, fix pagination, script order

* actually use the most common mangaId
---
 src/es/plottwistnofansub/build.gradle         |  2 +-
 .../es/plottwistnofansub/PlotTwistNoFansub.kt | 26 +++++++++++--------
 2 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/es/plottwistnofansub/build.gradle b/src/es/plottwistnofansub/build.gradle
index a3eb29699..acb517505 100644
--- a/src/es/plottwistnofansub/build.gradle
+++ b/src/es/plottwistnofansub/build.gradle
@@ -1,7 +1,7 @@
 ext {
     extName = 'Plot Twist No Fansub'
     extClass = '.PlotTwistNoFansub'
-    extVersionCode = 4
+    extVersionCode = 5
     isNsfw = true
 }
 
diff --git a/src/es/plottwistnofansub/src/eu/kanade/tachiyomi/extension/es/plottwistnofansub/PlotTwistNoFansub.kt b/src/es/plottwistnofansub/src/eu/kanade/tachiyomi/extension/es/plottwistnofansub/PlotTwistNoFansub.kt
index 5ce0db8c6..052ea7535 100644
--- a/src/es/plottwistnofansub/src/eu/kanade/tachiyomi/extension/es/plottwistnofansub/PlotTwistNoFansub.kt
+++ b/src/es/plottwistnofansub/src/eu/kanade/tachiyomi/extension/es/plottwistnofansub/PlotTwistNoFansub.kt
@@ -138,19 +138,26 @@ class PlotTwistNoFansub : ParsedHttpSource(), ConfigurableSource {
 
     override fun chapterListParse(response: Response): List<SChapter> {
         val document = response.asJsoup()
-        val mangaId = document.selectFirst(".chapters-container .row.itemlist p[data-mangaid]")!!.attr("data-mangaid")
+
+        val mangaIds = listOfNotNull(
+            MANGAID1_REGEX.find(document.html())?.groupValues?.get(1),
+            document.selectFirst("link[rel=shortlink]")?.attr("href")?.substringAfterLast("="),
+            document.selectFirst("body")?.classNames()?.filter { it.startsWith("postid-") }?.getOrNull(0)?.substringAfterLast("-"),
+            document.selectFirst(".td-post-views span")?.classNames()?.filter { it.startsWith("td-nr-views-") }?.getOrNull(0)?.substringAfterLast("-"),
+        ) + document.select("*[data-mangaid]").map { it.attr("data-mangaid") }
+
+        val mangaId = mangaIds.groupingBy { it }.eachCount().maxBy { it.value }.key
 
         val key = getKey(document)
         val url = "$baseUrl/wp-admin/admin-ajax.php"
-        val formBody = FormBody.Builder()
-            .add("action", key)
-            .add("manga_id", mangaId)
 
         var page = 1
         val chapterList = mutableListOf<SChapter>()
 
         do {
-            val body = formBody
+            val body = FormBody.Builder()
+                .add("action", key)
+                .add("manga_id", mangaId)
                 .add("pageNumber", page.toString())
                 .build()
 
@@ -217,21 +224,17 @@ class PlotTwistNoFansub : ParsedHttpSource(), ConfigurableSource {
     private fun getKey(document: Document): String {
         val customPriorityWant = listOf("custom")
         val customPriorityJunk = listOf("bootstrap", "pagi", "reader", "jquery")
+        val customPriorityJunk2 = listOf("multilanguage-", "ad-", "td-", "bj-", "html-", "gd-")
 
         document.select("script[src*=\"wp-content/plugins/\"]")
             .asSequence()
             .map { it.attr("src") }
-            .filterNot { it.contains("wp-content/plugins/multilanguage-") }
-            .filterNot { it.contains("wp-content/plugins/ad-") }
-            .filterNot { it.contains("wp-content/plugins/td-") }
-            .filterNot { it.contains("wp-content/plugins/bj-") }
-            .filterNot { it.contains("wp-content/plugins/html-") }
-            .filterNot { it.contains("wp-content/plugins/gd-") }
             .sortedWith(
                 compareBy<String> { url ->
                     when {
                         customPriorityWant.any { url.contains(it) } -> 0
                         customPriorityJunk.any { url.contains(it) } -> 2
+                        customPriorityJunk2.any { url.contains(it) } -> 3
                         else -> 1
                     }
                 },
@@ -254,6 +257,7 @@ class PlotTwistNoFansub : ParsedHttpSource(), ConfigurableSource {
     }
 
     companion object {
+        private val MANGAID1_REGEX = ""","manid":"(\d+)",""".toRegex()
         private val UNESCAPE_REGEX = """\\(.)""".toRegex()
         private val CHAPTER_PAGES_REGEX = """obj\s*=\s*(.*)\s*;""".toRegex()
         private val ACTION_REGEX = """action:\s*?(['"])([^\r\n]+?)\1""".toRegex()