From 52da22ba09da5faf475df21764755c64ab31bee6 Mon Sep 17 00:00:00 2001 From: Mike <51273546+SnakeDoc83@users.noreply.github.com> Date: Fri, 17 Apr 2020 16:57:52 -0400 Subject: [PATCH] Mangahere - fix pages (#2696) Mangahere - fix pages --- src/en/mangahere/build.gradle | 2 +- .../extension/en/mangahere/Mangahere.kt | 42 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/en/mangahere/build.gradle b/src/en/mangahere/build.gradle index b5be67039..955a4e6ee 100644 --- a/src/en/mangahere/build.gradle +++ b/src/en/mangahere/build.gradle @@ -5,7 +5,7 @@ ext { appName = 'Tachiyomi: Mangahere' pkgNameSuffix = 'en.mangahere' extClass = '.Mangahere' - extVersionCode = 13 + extVersionCode = 14 libVersion = '1.2' } diff --git a/src/en/mangahere/src/eu/kanade/tachiyomi/extension/en/mangahere/Mangahere.kt b/src/en/mangahere/src/eu/kanade/tachiyomi/extension/en/mangahere/Mangahere.kt index 6e9844976..509e80f78 100644 --- a/src/en/mangahere/src/eu/kanade/tachiyomi/extension/en/mangahere/Mangahere.kt +++ b/src/en/mangahere/src/eu/kanade/tachiyomi/extension/en/mangahere/Mangahere.kt @@ -197,6 +197,25 @@ class Mangahere : ParsedHttpSource() { val bar = document.select("script[src*=chapter_bar]") val duktape = Duktape.create() + /* + function to drop last imageUrl if it's broken/unneccesary, working imageUrls are incremental (e.g. t001, t002, etc); if the difference between + the last two isn't 1 or doesn't have an Int at the end of the last imageUrl's filename, drop last Page + */ + fun List.dropLastIfBroken(): List { + val list = this.takeLast(2).map { page -> + try { + page.imageUrl!!.substringBeforeLast(".").substringAfterLast("/").takeLast(2).toInt() + } catch (_: NumberFormatException) { + return this.dropLast(1) + } + } + return when { + list[0] == 0 && 100 - list[1] == 1 -> this + list[1] - list[0] == 1 -> this + else -> this.dropLast(1) + } + } + // if-branch is for webtoon reader, else is for page-by-page return if (bar.isNotEmpty()) { val script = document.select("script:containsData(function(p,a,c,k,e,d))").html().removePrefix("eval") @@ -204,24 +223,7 @@ class Mangahere : ParsedHttpSource() { val urls = deobfuscatedScript.substringAfter("newImgs=['").substringBefore("'];").split("','") duktape.close() - /* - last webtoon imageUrl is usually broken, working imageUrls are incremental (e.g. t001, t002, etc); if the difference between - the last two isn't 1 or doesn't have an Int at the end of the last imageUrl's filename, drop last Page - */ - urls.mapIndexed { index, s -> Page(index, "", "https:$s") }.let { pages -> - val list = pages.takeLast(2).map { page -> - try { - page.imageUrl!!.substringBeforeLast(".").substringAfterLast("/").takeLast(2).toInt() - } catch (_: NumberFormatException) { - return pages.dropLast(1) - } - } - when { - list[0] == 0 && 100 - list[1] == 1 -> pages - list[1] - list[0] == 1 -> pages - else -> pages.dropLast(1) - } - } + urls.mapIndexed { index, s -> Page(index, "", "https:$s") } } else { val html = document.html() val link = document.location() @@ -281,7 +283,9 @@ class Mangahere : ParsedHttpSource() { Page(i - 1, "", "https:$baseLink$imageLink") } - }.also { duktape.close() } + } + .dropLastIfBroken() + .also { duktape.close() } } private fun extractSecretKey(html: String, duktape: Duktape): String {