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