From 80f0d20818061631e97601da2ba911e7f839e54c Mon Sep 17 00:00:00 2001 From: snakedoc83 Date: Tue, 7 Jul 2020 04:12:50 -0400 Subject: [PATCH] refactor search --- src/all/wpcomics/build.gradle | 2 +- .../extension/all/wpcomics/WPComics.kt | 107 +++++++++- .../extension/all/wpcomics/WPComicsFactory.kt | 188 ++---------------- 3 files changed, 115 insertions(+), 182 deletions(-) diff --git a/src/all/wpcomics/build.gradle b/src/all/wpcomics/build.gradle index b551a535d..377bdf39e 100644 --- a/src/all/wpcomics/build.gradle +++ b/src/all/wpcomics/build.gradle @@ -5,7 +5,7 @@ ext { extName = 'WP-Comics' pkgNameSuffix = 'all.wpcomics' extClass = '.WPComicsFactory' - extVersionCode = 8 + extVersionCode = 9 libVersion = '1.2' } diff --git a/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComics.kt b/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComics.kt index 73a460c44..1975c1526 100644 --- a/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComics.kt +++ b/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComics.kt @@ -1,18 +1,20 @@ package eu.kanade.tachiyomi.extension.all.wpcomics import eu.kanade.tachiyomi.network.GET +import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.ParsedHttpSource -import java.text.SimpleDateFormat -import java.util.Calendar -import java.util.Locale +import okhttp3.HttpUrl import okhttp3.OkHttpClient import okhttp3.Request import org.jsoup.nodes.Document import org.jsoup.nodes.Element +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale abstract class WPComics( override val name: String, @@ -64,8 +66,30 @@ abstract class WPComics( // Search + protected open val searchPath = "tim-truyen" + override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { - return GET("$baseUrl/?s=$query&post_type=comics&page=$page") + val filterList = filters.let { if (it.isEmpty()) getFilterList() else it } + return if (filterList.isEmpty()) { + GET("$baseUrl/?s=$query&post_type=comics&page=$page") + } else { + val url = HttpUrl.parse("$baseUrl/$searchPath")!!.newBuilder() + + filterList.forEach { filter -> + when (filter) { + is GenreFilter -> filter.toUriPart()?.let { url.addPathSegment(it) } + is StatusFilter -> filter.toUriPart()?.let { url.addQueryParameter("status", it) } + } + } + + url.apply { + addQueryParameter("keyword", query) + addQueryParameter("page", page.toString()) + addQueryParameter("sort", "0") + } + + GET(url.toString().replace("www.nettruyen.com/tim-truyen?status=2&", "www.nettruyen.com/truyen-full?"), headers) + } } override fun searchMangaSelector() = "div.items div.item div.image a" @@ -147,10 +171,10 @@ abstract class WPComics( (if (gmtOffset == null) this.substringAfterLast(" ") else "$this $gmtOffset").let { // timestamp has year if (Regex("""\d+/\d+/\d\d""").find(it)?.value != null) { - dateFormat.parse(it).time + dateFormat.parse(it)?.time ?: 0 } else { // MangaSum - timestamp sometimes doesn't have year (current year implied) - dateFormat.parse("$it/$currentYear").time + dateFormat.parse("$it/$currentYear")?.time ?: 0 } } } @@ -189,4 +213,75 @@ abstract class WPComics( } override fun imageUrlParse(document: Document): String = throw UnsupportedOperationException("Not used") + + // Filters + + protected class StatusFilter(vals: Array>) : UriPartFilter("Status", vals) + protected class GenreFilter(vals: Array>) : UriPartFilter("Genre", vals) + + protected open fun getStatusList(): Array> = arrayOf( + Pair(null, "Tất cả"), + Pair("1", "Đang tiến hành"), + Pair("2", "Đã hoàn thành"), + Pair("3", "Tạm ngừng") + ) + protected open fun getGenreList(): Array> = arrayOf( + null to "Tất cả", + "action" to "Action", + "adult" to "Adult", + "adventure" to "Adventure", + "anime" to "Anime", + "chuyen-sinh" to "Chuyển Sinh", + "comedy" to "Comedy", + "comic" to "Comic", + "cooking" to "Cooking", + "co-dai" to "Cổ Đại", + "doujinshi" to "Doujinshi", + "drama" to "Drama", + "dam-my" to "Đam Mỹ", + "ecchi" to "Ecchi", + "fantasy" to "Fantasy", + "gender-bender" to "Gender Bender", + "harem" to "Harem", + "historical" to "Historical", + "horror" to "Horror", + "josei" to "Josei", + "live-action" to "Live action", + "manga" to "Manga", + "manhua" to "Manhua", + "manhwa" to "Manhwa", + "martial-arts" to "Martial Arts", + "mature" to "Mature", + "mecha" to "Mecha", + "mystery" to "Mystery", + "ngon-tinh" to "Ngôn Tình", + "one-shot" to "One shot", + "psychological" to "Psychological", + "romance" to "Romance", + "school-life" to "School Life", + "sci-fi" to "Sci-fi", + "seinen" to "Seinen", + "shoujo" to "Shoujo", + "shoujo-ai" to "Shoujo Ai", + "shounen" to "Shounen", + "shounen-ai" to "Shounen Ai", + "slice-of-life" to "Slice of Life", + "smut" to "Smut", + "soft-yaoi" to "Soft Yaoi", + "soft-yuri" to "Soft Yuri", + "sports" to "Sports", + "supernatural" to "Supernatural", + "thieu-nhi" to "Thiếu Nhi", + "tragedy" to "Tragedy", + "trinh-tham" to "Trinh Thám", + "truyen-scan" to "Truyện scan", + "truyen-mau" to "Truyện Màu", + "webtoon" to "Webtoon", + "xuyen-khong" to "Xuyên Không" + ) + + protected open class UriPartFilter(displayName: String, val vals: Array>) : + Filter.Select(displayName, vals.map { it.second }.toTypedArray()) { + fun toUriPart() = vals[state].first + } } diff --git a/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComicsFactory.kt b/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComicsFactory.kt index 8c8d5d2d8..1199d806f 100644 --- a/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComicsFactory.kt +++ b/src/all/wpcomics/src/eu/kanade/tachiyomi/extension/all/wpcomics/WPComicsFactory.kt @@ -4,7 +4,6 @@ import eu.kanade.tachiyomi.annotations.MultiSource import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceFactory -import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter @@ -12,7 +11,6 @@ import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.util.asJsoup import java.text.SimpleDateFormat import java.util.Locale -import okhttp3.HttpUrl import okhttp3.Request import okhttp3.Response import org.jsoup.nodes.Document @@ -26,7 +24,8 @@ class WPComicsFactory : SourceFactory { MangaSum(), XoxoComics(), NhatTruyen(), - NetTruyen() + NetTruyen(), + TruyenChon() ) } @@ -94,182 +93,21 @@ private class NhatTruyen : WPComics("NhatTruyen", "http://nhattruyen.com", "vi", private class NetTruyen : WPComics("NetTruyen", "http://www.nettruyen.com", "vi", SimpleDateFormat("dd/MM/yy", Locale.getDefault()), null) { override fun imageRequest(page: Page): Request = GET(page.imageUrl!!, headersBuilder().add("Referer", baseUrl).build()) - - // search and filters taken from old extension (1.2.5) - override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { - var url = HttpUrl.parse("$baseUrl/tim-truyen?")!!.newBuilder() - (if (filters.isEmpty()) getFilterList() else filters).forEach { filter -> - when (filter) { - is Genre -> { - url = if (filter.state == 0) url else - HttpUrl.parse(url.toString() - .replace("tim-truyen?", - "tim-truyen/${getGenreList().map { it.first }[filter.state]}?"))!! - .newBuilder() - } - is Status -> { - url.addQueryParameter("status", if (filter.state == 0) "hajau" else filter.state.toString()) - url.addQueryParameter("sort", "0") - } - } - } - url.addQueryParameter("keyword", query) - return GET(url.toString(), headers) + override fun getFilterList(): FilterList { + return FilterList( + StatusFilter(getStatusList()), + GenreFilter(getGenreList()) + ) } - - private fun getStatusList() = arrayOf("Tất cả", "Đang tiến hành", "Đã hoàn thành", "Tạm ngừng") - - private class Status(status: Array) : Filter.Select("Status", status) - private class Genre(genreList: Array) : Filter.Select("Thể loại", genreList) - - override fun getFilterList() = FilterList( - Status(getStatusList()), - Genre(getGenreList().map { it.second }.toTypedArray()) - ) - - private fun getGenreList() = arrayOf( - "tim-truyen" to "Tất cả", - "action" to "Action", - "adult" to "Adult", - "adventure" to "Adventure", - "anime" to "Anime", - "chuyen-sinh" to "Chuyển Sinh", - "comedy" to "Comedy", - "comic" to "Comic", - "cooking" to "Cooking", - "co-dai" to "Cổ Đại", - "doujinshi" to "Doujinshi", - "drama" to "Drama", - "dam-my" to "Đam Mỹ", - "ecchi" to "Ecchi", - "fantasy" to "Fantasy", - "gender-bender" to "Gender Bender", - "harem" to "Harem", - "historical" to "Historical", - "horror" to "Horror", - "josei" to "Josei", - "live-action" to "Live action", - "manga" to "Manga", - "manhua" to "Manhua", - "manhwa" to "Manhwa", - "martial-arts" to "Martial Arts", - "mature" to "Mature", - "mecha" to "Mecha", - "mystery" to "Mystery", - "ngon-tinh" to "Ngôn Tình", - "one-shot" to "One shot", - "psychological" to "Psychological", - "romance" to "Romance", - "school-life" to "School Life", - "sci-fi" to "Sci-fi", - "seinen" to "Seinen", - "shoujo" to "Shoujo", - "shoujo-ai" to "Shoujo Ai", - "shounen" to "Shounen", - "shounen-ai" to "Shounen Ai", - "slice-of-life" to "Slice of Life", - "smut" to "Smut", - "soft-yaoi" to "Soft Yaoi", - "soft-yuri" to "Soft Yuri", - "sports" to "Sports", - "supernatural" to "Supernatural", - "thieu-nhi" to "Thiếu Nhi", - "tragedy" to "Tragedy", - "trinh-tham" to "Trinh Thám", - "truyen-scan" to "Truyện scan", - "truyen-mau" to "Truyện Màu", - "webtoon" to "Webtoon", - "xuyen-khong" to "Xuyên Không" - ) } private class TruyenChon : WPComics("TruyenChon", "http://truyenchon.com", "vi", SimpleDateFormat("dd/MM/yy", Locale.getDefault()), null) { + override val searchPath = "the-loai" override fun imageRequest(page: Page): Request = GET(page.imageUrl!!, headersBuilder().add("Referer", baseUrl).build()) - - // search and filters taken from old extension (1.2.5) - override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { - var url = HttpUrl.parse("$baseUrl/tim-truyen?")!!.newBuilder() - (if (filters.isEmpty()) getFilterList() else filters).forEach { filter -> - when (filter) { - is Genre -> { - url = if (filter.state == 0) url else - HttpUrl.parse(url.toString() - .replace("tim-truyen?", - "tim-truyen/${getGenreList().map { it.first }[filter.state]}?"))!! - .newBuilder() - } - is Status -> { - url.addQueryParameter("status", if (filter.state == 0) "hajau" else filter.state.toString()) - url.addQueryParameter("sort", "0") - } - } - } - url.addQueryParameter("keyword", query) - return GET(url.toString(), headers) + override fun getFilterList(): FilterList { + return FilterList( + StatusFilter(getStatusList()), + GenreFilter(getGenreList()) + ) } - - private fun getStatusList() = arrayOf("Tất cả", "Đang tiến hành", "Đã hoàn thành", "Tạm ngừng") - - private class Status(status: Array) : Filter.Select("Status", status) - private class Genre(genreList: Array) : Filter.Select("Thể loại", genreList) - - override fun getFilterList() = FilterList( - Status(getStatusList()), - Genre(getGenreList().map { it.second }.toTypedArray()) - ) - - private fun getGenreList() = arrayOf( - "tim-truyen" to "Tất cả", - "action" to "Action", - "adult" to "Adult", - "adventure" to "Adventure", - "anime" to "Anime", - "chuyen-sinh" to "Chuyển Sinh", - "comedy" to "Comedy", - "comic" to "Comic", - "cooking" to "Cooking", - "co-dai" to "Cổ Đại", - "doujinshi" to "Doujinshi", - "drama" to "Drama", - "dam-my" to "Đam Mỹ", - "ecchi" to "Ecchi", - "fantasy" to "Fantasy", - "gender-bender" to "Gender Bender", - "harem" to "Harem", - "historical" to "Historical", - "horror" to "Horror", - "josei" to "Josei", - "live-action" to "Live action", - "manga" to "Manga", - "manhua" to "Manhua", - "manhwa" to "Manhwa", - "martial-arts" to "Martial Arts", - "mature" to "Mature", - "mecha" to "Mecha", - "mystery" to "Mystery", - "ngon-tinh" to "Ngôn Tình", - "one-shot" to "One shot", - "psychological" to "Psychological", - "romance" to "Romance", - "school-life" to "School Life", - "sci-fi" to "Sci-fi", - "seinen" to "Seinen", - "shoujo" to "Shoujo", - "shoujo-ai" to "Shoujo Ai", - "shounen" to "Shounen", - "shounen-ai" to "Shounen Ai", - "slice-of-life" to "Slice of Life", - "smut" to "Smut", - "soft-yaoi" to "Soft Yaoi", - "soft-yuri" to "Soft Yuri", - "sports" to "Sports", - "supernatural" to "Supernatural", - "thieu-nhi" to "Thiếu Nhi", - "tragedy" to "Tragedy", - "trinh-tham" to "Trinh Thám", - "truyen-scan" to "Truyện scan", - "truyen-mau" to "Truyện Màu", - "webtoon" to "Webtoon", - "xuyen-khong" to "Xuyên Không" - ) }