Fix TruyenQQ popular and search request (#13242)

This commit is contained in:
PainterHalver 2022-08-29 04:01:15 +07:00 committed by GitHub
parent f7065285ee
commit 35b4811c4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 25 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'TruyenQQ' extName = 'TruyenQQ'
pkgNameSuffix = 'vi.truyenqq' pkgNameSuffix = 'vi.truyenqq'
extClass = '.TruyenQQ' extClass = '.TruyenQQ'
extVersionCode = 6 extVersionCode = 7
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"

View File

@ -1,15 +1,18 @@
package eu.kanade.tachiyomi.extension.vi.truyenqq package eu.kanade.tachiyomi.extension.vi.truyenqq
import android.net.Uri
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter 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 okhttp3.Headers import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
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.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -27,51 +30,86 @@ class TruyenQQ : ParsedHttpSource() {
override val supportsLatest: Boolean = true override val supportsLatest: Boolean = true
override val client: OkHttpClient = network.cloudflareClient.newBuilder() override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.connectTimeout(1, TimeUnit.MINUTES) .connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.MINUTES) .readTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true) .retryOnConnectionFailure(true)
.followRedirects(true) .followRedirects(true)
.build() .build()
override fun headersBuilder(): Headers.Builder { override fun headersBuilder(): Headers.Builder = super.headersBuilder().add("Referer", baseUrl)
return super.headersBuilder().add("Referer", baseUrl)
}
private val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.US) private val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.US)
private val floatPattern = Regex("""\d+(?:\.\d+)?""") private val floatPattern = Regex("""\d+(?:\.\d+)?""")
// Popular // Selector trả về array các manga (chọn cả ảnh cx được tí nữa parse)
override fun popularMangaRequest(page: Int): Request {
return GET("$baseUrl/top-thang/trang-$page.html", headers)
}
override fun popularMangaNextPageSelector(): String = ".page_redirect > a:last-child > p:not(.active)"
override fun popularMangaSelector(): String = "ul.grid > li" override fun popularMangaSelector(): String = "ul.grid > li"
override fun popularMangaFromElement(element: Element): SManga = SManga.create().apply { // Selector trả về array các manga update (giống selector ở trên)
setUrlWithoutDomain(element.select("a").first().attr("abs:href")) override fun latestUpdatesSelector(): String = popularMangaSelector()
thumbnail_url = element.select("img.lazy-image").attr("abs:data-src") // Selector của nút trang kế tiếp
title = element.select("h3 a").text() override fun popularMangaNextPageSelector(): String = ".page_redirect > a:nth-last-child(2) > p:not(.active)"
} override fun latestUpdatesNextPageSelector(): String = popularMangaNextPageSelector()
// Latest // Trang html chứa popular
override fun popularMangaRequest(page: Int): Request {
return GET("$baseUrl/truyen-yeu-thich/trang-$page.html", headers)
}
// Trang html chứa Latest (các cập nhật mới nhất)
override fun latestUpdatesRequest(page: Int): Request { override fun latestUpdatesRequest(page: Int): Request {
return GET("$baseUrl/truyen-moi-cap-nhat/trang-$page.html", headers) return GET("$baseUrl/truyen-moi-cap-nhat/trang-$page.html", headers)
} }
override fun latestUpdatesNextPageSelector(): String = popularMangaNextPageSelector()
override fun latestUpdatesSelector(): String = popularMangaSelector()
override fun latestUpdatesFromElement(element: Element): SManga = popularMangaFromElement(element)
// Search // respond là html của trang popular chứ không phải của element đã select
override fun popularMangaParse(response: Response): MangasPage {
val document = response.asJsoup()
val imgURL = document.select(".book_avatar img").map { it.attr("abs:src") }
val mangas = document.select(popularMangaSelector()).mapIndexed { index, element -> popularMangaFromElement(element, imgURL[index]) }
val hasNextPage = popularMangaNextPageSelector().let { selector ->
document.select(selector).first()
} != null
return MangasPage(mangas, hasNextPage)
}
// Từ 1 element trong list popular đã select ở trên parse thông tin 1 Manga
// Trông code bất ổn nhưng t đang cố làm theo blogtruyen vì t không biết gì hết XD
private fun popularMangaFromElement(element: Element, imgURL: String): SManga {
val manga = SManga.create()
element.select(".book_info .book_name h3 a").first().let {
manga.setUrlWithoutDomain((it.attr("href")))
manga.title = it.text().trim()
manga.thumbnail_url = imgURL
}
return manga
}
// Không dùng bản này của fuction nên throw Exception, dùng function ở trên (có 2 params)
override fun popularMangaFromElement(element: Element): SManga = throw Exception("Not Used")
override fun latestUpdatesFromElement(element: Element): SManga {
val manga = SManga.create()
element.select(".book_info .book_name h3 a").first().let {
manga.setUrlWithoutDomain((it.attr("href")))
manga.title = it.text().trim()
}
manga.thumbnail_url = element.select(".book_avatar img").first().attr("abs:src")
return manga
}
// Tìm kiếm
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val uri = Uri.parse("$baseUrl/tim-kiem/trang-$page.html").buildUpon() val url = "$baseUrl/tim-kiem/trang-$page.html"
.appendQueryParameter("q", query) val uri = url.toHttpUrlOrNull()!!.newBuilder()
uri.addQueryParameter("q", query)
return GET(uri.toString(), headers) return GET(uri.toString(), headers)
// Todo Filters // Todo Filters
} }
override fun searchMangaNextPageSelector(): String = popularMangaNextPageSelector() override fun searchMangaNextPageSelector(): String = popularMangaNextPageSelector()
override fun searchMangaSelector(): String = popularMangaSelector() override fun searchMangaSelector(): String = popularMangaSelector()
override fun searchMangaFromElement(element: Element): SManga = popularMangaFromElement(element) override fun searchMangaFromElement(element: Element): SManga = latestUpdatesFromElement(element)
// Details // Details