[AsuraScans] Fix pages parsing
This commit is contained in:
@@ -11,6 +11,8 @@ import org.koitharu.kotatsu.parsers.config.ConfigKey
|
|||||||
import org.koitharu.kotatsu.parsers.model.*
|
import org.koitharu.kotatsu.parsers.model.*
|
||||||
import org.koitharu.kotatsu.parsers.util.*
|
import org.koitharu.kotatsu.parsers.util.*
|
||||||
import org.koitharu.kotatsu.parsers.util.json.asTypedList
|
import org.koitharu.kotatsu.parsers.util.json.asTypedList
|
||||||
|
import org.koitharu.kotatsu.parsers.util.json.toJSONArrayOrNull
|
||||||
|
import org.koitharu.kotatsu.parsers.util.json.toJSONObjectOrNull
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -185,14 +187,36 @@ internal class AsuraScansParser(context: MangaLoaderContext) :
|
|||||||
|
|
||||||
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
|
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
|
||||||
val doc = webClient.httpGet(chapter.url.toAbsoluteUrl(domain)).parseHtml()
|
val doc = webClient.httpGet(chapter.url.toAbsoluteUrl(domain)).parseHtml()
|
||||||
return doc.selectOrThrow("div.w-full > img.object-cover").map { img ->
|
val data = doc.selectOrThrow("script").mapNotNull { x ->
|
||||||
val urlPage = img.requireSrc().toRelativeUrl(domain)
|
x.data().substringBetween("self.__next_f.push(", ")", "")
|
||||||
|
.trim()
|
||||||
|
.takeUnless { it.isEmpty() }
|
||||||
|
}.flatMap { it.jsonStrings() }
|
||||||
|
.joinToString("")
|
||||||
|
.split('\n')
|
||||||
|
.mapNotNull { x ->
|
||||||
|
x.substringAfter(':').toJSONObjectOrNull()
|
||||||
|
}
|
||||||
|
val pages = data.filter { it.has("order") && it.has("url") }
|
||||||
|
.associate { it.getInt("order") to it.getString("url") }.values
|
||||||
|
return pages.map { url ->
|
||||||
MangaPage(
|
MangaPage(
|
||||||
id = generateUid(urlPage),
|
id = generateUid(url),
|
||||||
url = urlPage,
|
url = url,
|
||||||
preview = null,
|
preview = null,
|
||||||
source = source,
|
source = source,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun String.jsonStrings(): List<String> {
|
||||||
|
val ja = toJSONArrayOrNull() ?: return emptyList()
|
||||||
|
val result = ArrayList<String>(ja.length())
|
||||||
|
repeat(ja.length()) { i ->
|
||||||
|
(ja.get(i) as? String)?.let { item ->
|
||||||
|
result.add(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,23 @@ package org.koitharu.kotatsu.parsers.util.json
|
|||||||
|
|
||||||
import androidx.collection.ArraySet
|
import androidx.collection.ArraySet
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
public fun String.toJSONObjectOrNull(): JSONObject? = try {
|
||||||
|
JSONObject(this)
|
||||||
|
} catch (_: JSONException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun String.toJSONArrayOrNull(): JSONArray? = try {
|
||||||
|
JSONArray(this)
|
||||||
|
} catch (_: JSONException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
public inline fun <R, C : MutableCollection<in R>> JSONArray.mapJSONTo(
|
public inline fun <R, C : MutableCollection<in R>> JSONArray.mapJSONTo(
|
||||||
destination: C,
|
destination: C,
|
||||||
block: (JSONObject) -> R,
|
block: (JSONObject) -> R,
|
||||||
|
|||||||
Reference in New Issue
Block a user