From 50e58499a94d40751a0f9070cb52bb77175550b9 Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Sat, 9 Jul 2022 09:46:21 +0800 Subject: [PATCH] SinMH: cleanup and extract lib (#12423) * SinMH: cleanup, replace rate limiter and extract lib * revert rate limiter change * remove dependency from theme generator --- .../overrides/sinmh/gufengmh/src/Gufengmh.kt | 1 + .../sinmh/wuqimanga/additional.gradle | 3 ++ .../sinmh/wuqimanga/src/WuqiManga.kt | 31 ++++--------------- .../multisrc/sinmh/ProgressiveParser.kt | 11 +++++++ .../kanade/tachiyomi/multisrc/sinmh/SinMH.kt | 17 ++-------- .../multisrc/sinmh/SinMHGenerator.kt | 2 +- 6 files changed, 25 insertions(+), 40 deletions(-) create mode 100644 multisrc/overrides/sinmh/wuqimanga/additional.gradle create mode 100644 multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/ProgressiveParser.kt diff --git a/multisrc/overrides/sinmh/gufengmh/src/Gufengmh.kt b/multisrc/overrides/sinmh/gufengmh/src/Gufengmh.kt index 1955119cf..711a1b749 100644 --- a/multisrc/overrides/sinmh/gufengmh/src/Gufengmh.kt +++ b/multisrc/overrides/sinmh/gufengmh/src/Gufengmh.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.extension.zh.gufengmh +import eu.kanade.tachiyomi.multisrc.sinmh.ProgressiveParser import eu.kanade.tachiyomi.multisrc.sinmh.SinMH import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.SChapter diff --git a/multisrc/overrides/sinmh/wuqimanga/additional.gradle b/multisrc/overrides/sinmh/wuqimanga/additional.gradle new file mode 100644 index 000000000..7b7c4bce0 --- /dev/null +++ b/multisrc/overrides/sinmh/wuqimanga/additional.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation 'com.github.stevenyomi:unpacker:12a09e3c1a' // 1.1 +} diff --git a/multisrc/overrides/sinmh/wuqimanga/src/WuqiManga.kt b/multisrc/overrides/sinmh/wuqimanga/src/WuqiManga.kt index c1840a582..91f53bbaa 100644 --- a/multisrc/overrides/sinmh/wuqimanga/src/WuqiManga.kt +++ b/multisrc/overrides/sinmh/wuqimanga/src/WuqiManga.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.extension.zh.wuqimanga +import com.github.stevenyomi.unpacker.Unpacker import eu.kanade.tachiyomi.multisrc.sinmh.SinMH import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.Filter @@ -59,20 +60,12 @@ class WuqiManga : SinMH("57漫画", "http://www.wuqimh.net") { override fun pageListRequest(chapter: SChapter) = GET(baseUrl + chapter.url, headers) - // Reference: https://github.com/evanw/packer/blob/master/packer.js override fun pageListParse(document: Document): List { - val script = document.selectFirst("body > script").html().let(::ProgressiveParser) - val imageList = script.substringBetween(":[", "]").replace("\\", "") - if (imageList.isEmpty()) return emptyList() - script.consumeUntil("""};',""") - val dictionary = script.substringBetween("'", "'").split('|') - val size = dictionary.size - val unpacked = Regex("""\b\w+\b""").replace(imageList) { - with(it.value) { - val key = parseRadix62() - return@replace if (key < size) dictionary[key] else this - } - }.removeSurrounding("'").split("','") + val script = document.selectFirst("body > script").html() + val unpacked = Unpacker.unpack(script, ":[", "]") + .ifEmpty { return emptyList() } + .replace("\\", "") + .removeSurrounding("\"").split("\",\"") val list = unpacked.filterNot { it.endsWith("/ManHuaKu/222.jpg") }.map { image -> if (image.startsWith("http")) image else imageHost + image } @@ -131,15 +124,3 @@ class WuqiManga : SinMH("57漫画", "http://www.wuqimh.net") { private val sortNames = arrayOf("最新发布", "最近更新", "人气最旺", "评分最高") private val sortKeys = arrayOf("order-id", "order-addtime", "order-hits", "order-gold") } - -private fun String.parseRadix62(): Int { - var result = 0 - for (char in this) { - result = result * 62 + when { - char <= '9' -> char.code - '0'.code - char >= 'a' -> char.code - 'a'.code + 10 - else -> char.code - 'A'.code + 36 - } - } - return result -} diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/ProgressiveParser.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/ProgressiveParser.kt new file mode 100644 index 000000000..edb0c9234 --- /dev/null +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/ProgressiveParser.kt @@ -0,0 +1,11 @@ +package eu.kanade.tachiyomi.multisrc.sinmh + +internal class ProgressiveParser(private val text: String) { + private var startIndex = 0 + fun substringBetween(left: String, right: String): String = with(text) { + val leftIndex = indexOf(left, startIndex) + left.length + val rightIndex = indexOf(right, leftIndex) + startIndex = rightIndex + right.length + return substring(leftIndex, rightIndex) + } +} diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMH.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMH.kt index 73cb13ee5..d099fda7b 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMH.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMH.kt @@ -32,7 +32,7 @@ abstract class SinMH( ) : ParsedHttpSource() { override val baseUrl = _baseUrl - protected open val mobileUrl = _baseUrl.replace("www", "m") + protected open val mobileUrl = _baseUrl.replaceFirst("www.", "m.") override val supportsLatest = true override val client = network.client.newBuilder().rateLimit(2).build() @@ -206,20 +206,9 @@ abstract class SinMH( } } - protected class ProgressiveParser(private val text: String) { - private var startIndex = 0 - fun consumeUntil(string: String) = with(text) { startIndex = indexOf(string, startIndex) + string.length } - fun substringBetween(left: String, right: String): String = with(text) { - val leftIndex = indexOf(left, startIndex) + left.length - val rightIndex = indexOf(right, leftIndex) - startIndex = rightIndex + right.length - return substring(leftIndex, rightIndex) - } - } - // default parsing of ["...","..."] protected open fun parsePageImages(chapterImages: String): List = - if (chapterImages.length > 2) { + if (chapterImages.length > 4) { chapterImages.run { substring(2, length - 2) }.replace("""\/""", "/").split("\",\"") } else emptyList() // [] @@ -258,7 +247,7 @@ abstract class SinMH( if (categories.isNotEmpty()) { list = ArrayList(categories.size + 2) with(list) { - add(Filter.Header("如果使用文本搜索,将会忽略分类筛选")) + add(Filter.Header("分类筛选(搜索文本时无效)")) categories.forEach { add(it.toUriPartFilter()) } } } else { diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMHGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMHGenerator.kt index 0001b365d..f4d3e2715 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMHGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/sinmh/SinMHGenerator.kt @@ -6,7 +6,7 @@ import generator.ThemeSourceGenerator class SinMHGenerator : ThemeSourceGenerator { override val themeClass = "SinMH" override val themePkg = "sinmh" - override val baseVersionCode = 5 + override val baseVersionCode = 6 override val sources = listOf( SingleLang( name = "Gufeng Manhua", baseUrl = "https://www.gufengmh9.com", lang = "zh",