SinMH: cleanup and extract lib (#12423)

* SinMH: cleanup, replace rate limiter and extract lib

* revert rate limiter change

* remove dependency from theme generator
This commit is contained in:
stevenyomi 2022-07-09 09:46:21 +08:00 committed by GitHub
parent 56bb0a4ffe
commit 50e58499a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 40 deletions

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.zh.gufengmh 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.multisrc.sinmh.SinMH
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter

View File

@ -0,0 +1,3 @@
dependencies {
implementation 'com.github.stevenyomi:unpacker:12a09e3c1a' // 1.1
}

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.zh.wuqimanga package eu.kanade.tachiyomi.extension.zh.wuqimanga
import com.github.stevenyomi.unpacker.Unpacker
import eu.kanade.tachiyomi.multisrc.sinmh.SinMH import eu.kanade.tachiyomi.multisrc.sinmh.SinMH
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.source.model.Filter 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) 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<Page> { override fun pageListParse(document: Document): List<Page> {
val script = document.selectFirst("body > script").html().let(::ProgressiveParser) val script = document.selectFirst("body > script").html()
val imageList = script.substringBetween(":[", "]").replace("\\", "") val unpacked = Unpacker.unpack(script, ":[", "]")
if (imageList.isEmpty()) return emptyList() .ifEmpty { return emptyList() }
script.consumeUntil("""};',""") .replace("\\", "")
val dictionary = script.substringBetween("'", "'").split('|') .removeSurrounding("\"").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 list = unpacked.filterNot { it.endsWith("/ManHuaKu/222.jpg") }.map { image -> val list = unpacked.filterNot { it.endsWith("/ManHuaKu/222.jpg") }.map { image ->
if (image.startsWith("http")) image else imageHost + 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 sortNames = arrayOf("最新发布", "最近更新", "人气最旺", "评分最高")
private val sortKeys = arrayOf("order-id", "order-addtime", "order-hits", "order-gold") 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
}

View File

@ -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)
}
}

View File

@ -32,7 +32,7 @@ abstract class SinMH(
) : ParsedHttpSource() { ) : ParsedHttpSource() {
override val baseUrl = _baseUrl 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 supportsLatest = true
override val client = network.client.newBuilder().rateLimit(2).build() 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 ["...","..."] // default parsing of ["...","..."]
protected open fun parsePageImages(chapterImages: String): List<String> = protected open fun parsePageImages(chapterImages: String): List<String> =
if (chapterImages.length > 2) { if (chapterImages.length > 4) {
chapterImages.run { substring(2, length - 2) }.replace("""\/""", "/").split("\",\"") chapterImages.run { substring(2, length - 2) }.replace("""\/""", "/").split("\",\"")
} else emptyList() // [] } else emptyList() // []
@ -258,7 +247,7 @@ abstract class SinMH(
if (categories.isNotEmpty()) { if (categories.isNotEmpty()) {
list = ArrayList(categories.size + 2) list = ArrayList(categories.size + 2)
with(list) { with(list) {
add(Filter.Header("如果使用文本搜索,将会忽略分类筛选")) add(Filter.Header("分类筛选(搜索文本时无效)"))
categories.forEach { add(it.toUriPartFilter()) } categories.forEach { add(it.toUriPartFilter()) }
} }
} else { } else {

View File

@ -6,7 +6,7 @@ import generator.ThemeSourceGenerator
class SinMHGenerator : ThemeSourceGenerator { class SinMHGenerator : ThemeSourceGenerator {
override val themeClass = "SinMH" override val themeClass = "SinMH"
override val themePkg = "sinmh" override val themePkg = "sinmh"
override val baseVersionCode = 5 override val baseVersionCode = 6
override val sources = listOf( override val sources = listOf(
SingleLang( SingleLang(
name = "Gufeng Manhua", baseUrl = "https://www.gufengmh9.com", lang = "zh", name = "Gufeng Manhua", baseUrl = "https://www.gufengmh9.com", lang = "zh",