MangaOwl: update chapter request logic (#11190)

* Update chapter request logic

* refactor
This commit is contained in:
Vetle Ledaal 2022-03-22 11:41:54 +00:00 committed by GitHub
parent 06b8ba1a8b
commit a8050758d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'MangaOwl'
pkgNameSuffix = 'en.mangaowl'
extClass = '.MangaOwl'
extVersionCode = 24
extVersionCode = 25
}
apply from: "$rootDir/common.gradle"

View File

@ -10,12 +10,13 @@ import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.ParsedHttpSource
import eu.kanade.tachiyomi.util.asJsoup
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import java.net.URLDecoder
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Locale
@ -37,6 +38,14 @@ class MangaOwl : ParsedHttpSource() {
.writeTimeout(1, TimeUnit.MINUTES)
.build()
private val trPattern = "window\\['tr'] = '([^']*)';".toRegex(RegexOption.IGNORE_CASE)
private fun getTr(document: Document): String {
val trElement = document.getElementsByTag("script").find { trPattern.find(it.data()) != null } ?: error("tr not found")
val tr = trPattern.find(trElement.data())!!.groups[1]!!.value
return URLDecoder.decode(tr, "utf-8")
}
// Popular
override fun popularMangaRequest(page: Int): Request {
@ -86,7 +95,7 @@ class MangaOwl : ParsedHttpSource() {
}
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val url = "$baseUrl/search/$page".toHttpUrlOrNull()!!.newBuilder()
val url = "$baseUrl/search/$page".toHttpUrl().newBuilder()
url.addQueryParameter("search", query)
filters.forEach { filter ->
@ -150,14 +159,16 @@ class MangaOwl : ParsedHttpSource() {
override fun chapterListParse(response: Response): List<SChapter> {
val document = response.asJsoup()
val trRegex = "window\\['tr'] = '([^']*)';".toRegex(RegexOption.IGNORE_CASE)
val trElement = document.getElementsByTag("script").find { trRegex.find(it.data()) != null } ?: error("tr not found")
val tr = trRegex.find(trElement.data())!!.groups[1]!!.value
TR = getTr(document)
val s = Base64.encodeToString(baseUrl.toByteArray(), Base64.NO_PADDING)
return document.select(chapterListSelector()).map { element ->
SChapter.create().apply {
element.select("a").let {
url = "${it.attr("data-href")}?tr=$tr&s=$s"
url = it.attr("data-href")
.toHttpUrl().newBuilder()
.addQueryParameter("tr", TR)
.addQueryParameter("s", s)
.toString()
name = it.select("label").first().text()
}
date_upload = parseChapterDate(element.select("small:last-of-type").text())
@ -171,6 +182,7 @@ class MangaOwl : ParsedHttpSource() {
val dateFormat by lazy {
SimpleDateFormat("MM/dd/yyyy", Locale.US)
}
var TR: String? = null
}
private fun parseChapterDate(string: String): Long {
@ -182,11 +194,25 @@ class MangaOwl : ParsedHttpSource() {
}
// Pages
override fun pageListRequest(chapter: SChapter) = GET(chapter.url, headers) // url already complete
override fun pageListRequest(chapter: SChapter): Request {
if (TR == null) {
val document = client.newCall(GET(baseUrl)).execute().asJsoup()
TR = getTr(document)
}
val url = chapter.url.toHttpUrl().newBuilder()
.removeAllQueryParameters("tr")
.addQueryParameter("tr", TR)
return GET(url.toString(), headers)
}
override fun pageListParse(document: Document): List<Page> {
return document.select("div.item img.owl-lazy").mapIndexed { i, img ->
Page(i, "", img.attr("abs:data-src"))
}.ifEmpty {
TR = null
listOf()
}
}