From 86792746dc41f479ce83321ff8afd85e43486aff Mon Sep 17 00:00:00 2001 From: happywillow0 <45346080+happywillow0@users.noreply.github.com> Date: Fri, 28 Feb 2020 18:11:11 -0500 Subject: [PATCH] Update MRM Filter Code (#2305) * Update MRM Filter Code * Code Reformat --- src/all/myreadingmanga/build.gradle | 2 +- .../all/myreadingmanga/MyReadingManga.kt | 81 +++++++++++++------ 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/all/myreadingmanga/build.gradle b/src/all/myreadingmanga/build.gradle index 86a75e508..da11854bc 100644 --- a/src/all/myreadingmanga/build.gradle +++ b/src/all/myreadingmanga/build.gradle @@ -5,7 +5,7 @@ ext { appName = 'Tachiyomi: MyReadingManga' pkgNameSuffix = 'all.myreadingmanga' extClass = '.MyReadingMangaFactory' - extVersionCode = 31 + extVersionCode = 32 libVersion = '1.2' } diff --git a/src/all/myreadingmanga/src/eu/kanade/tachiyomi/extension/all/myreadingmanga/MyReadingManga.kt b/src/all/myreadingmanga/src/eu/kanade/tachiyomi/extension/all/myreadingmanga/MyReadingManga.kt index 0b6d92528..d754a3768 100644 --- a/src/all/myreadingmanga/src/eu/kanade/tachiyomi/extension/all/myreadingmanga/MyReadingManga.kt +++ b/src/all/myreadingmanga/src/eu/kanade/tachiyomi/extension/all/myreadingmanga/MyReadingManga.kt @@ -1,17 +1,20 @@ package eu.kanade.tachiyomi.extension.all.myreadingmanga + import android.net.Uri import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.asObservableSuccess import eu.kanade.tachiyomi.source.model.* import eu.kanade.tachiyomi.source.online.ParsedHttpSource import eu.kanade.tachiyomi.util.asJsoup -import okhttp3.* +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response import org.jsoup.nodes.Document import org.jsoup.nodes.Element import rx.Observable import java.io.IOException import java.text.SimpleDateFormat -import java.util.Locale +import java.util.* import java.util.concurrent.TimeUnit open class MyReadingManga(override val lang: String) : ParsedHttpSource() { @@ -49,7 +52,7 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { override fun latestUpdatesParse(response: Response): MangasPage { val document = response.asJsoup() val mangas = mutableListOf() - val list = document.select(latestUpdatesSelector()).filter { element -> + val list = document.select(latestUpdatesSelector()).filter { element -> val select = element.select("a[rel=bookmark]") select.text().contains("[$lang", true) } @@ -107,7 +110,7 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { //Process Filter Results / Same theme as home page else { //return popularMangaParse(response) - val list = document.select(latestUpdatesSelector()).filter { element -> + val list = document.select(latestUpdatesSelector()).filter { element -> val select = element.select("a[rel=bookmark]") select.text().contains("[$lang", true) } @@ -124,13 +127,13 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { } } - override fun searchMangaFromElement(element: Element) = buildManga(element.select("a").first(), element.select("img")?.first() ) + override fun searchMangaFromElement(element: Element) = buildManga(element.select("a").first(), element.select("img")?.first()) private fun buildManga(titleElement: Element, thumbnailElement: Element?): SManga { val manga = SManga.create() manga.setUrlWithoutDomain(titleElement.attr("href")) manga.title = cleanTitle(titleElement.text()) - if (thumbnailElement !=null) manga.thumbnail_url = getThumbnail(getImage(thumbnailElement)) + if (thumbnailElement != null) manga.thumbnail_url = getThumbnail(getImage(thumbnailElement)) return manga } @@ -200,8 +203,9 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { val date = parseDate(document.select(".entry-time").text()) val mangaUrl = document.baseUri() - val chfirstname = document.select(".chapter-class a[href*=$mangaUrl]")?.first()?.text()?.ifEmpty { "Ch. 1" }?.capitalize() ?:"Ch. 1" - val scangroup= document.select(".entry-terms a[href*=group]")?.first()?.text() + val chfirstname = document.select(".chapter-class a[href*=$mangaUrl]")?.first()?.text()?.ifEmpty { "Ch. 1" }?.capitalize() + ?: "Ch. 1" + val scangroup = document.select(".entry-terms a[href*=group]")?.first()?.text() //create first chapter since its on main manga page chapters.add(createChapter("1", document.baseUri(), date, chfirstname, scangroup)) //see if there are multiple chapters or not @@ -209,7 +213,8 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { it.forEach { if (!it.text().contains("Next ยป", true)) { val pageNumber = it.text() - val chname = document.select(".chapter-class a[href$=/$pageNumber/]")?.text()?.ifEmpty { "Ch. $pageNumber" }?.capitalize() ?:"Ch. $pageNumber" + val chname = document.select(".chapter-class a[href$=/$pageNumber/]")?.text()?.ifEmpty { "Ch. $pageNumber" }?.capitalize() + ?: "Ch. $pageNumber" chapters.add(createChapter(it.text(), document.baseUri(), date, chname, scangroup)) } } @@ -220,7 +225,7 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { } private fun parseDate(date: String): Long { - return SimpleDateFormat("MMM dd, yyyy", Locale.US ).parse(date).time + return SimpleDateFormat("MMM dd, yyyy", Locale.US).parse(date).time } private fun createChapter(pageNumber: String, mangaUrl: String, date: Long, chname: String, scangroup: String?): SChapter { @@ -253,11 +258,30 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { //Filter Parsing, grabs home page as document and filters out Genres, Popular Tags, and Catagorys - private val getFilter:Document? = try { network.client.newCall(GET(baseUrl, headers)).execute().asJsoup() } catch (e: IOException) {null} - private fun returnFilter (css: String, attributekey: String): Array> = if (getFilter?.select(".cf-browser-verification").isNullOrEmpty()) { - getFilter?.select(css)?.map { Pair(it.attr(attributekey).substringBeforeLast("/").substringAfterLast("/"), it.text()) }?.toTypedArray() ?: arrayOf(Pair("","Error getting filters")) - } else { - arrayOf(Pair("","Open 'Latest' and force restart app")) + + private val filterDoc = getFilterDoc(baseUrl) + private val categoryDoc = getFilterDoc("$baseUrl/cats/") + private val pairingDoc = getFilterDoc("$baseUrl/pairing/") + private val scangroupDoc = getFilterDoc("$baseUrl/group/") + private fun getFilterDoc(url: String): Document? { + return try { + network.client.newCall(GET(url, headers)).execute().asJsoup() + } catch (e: IOException) { + null + } + } + + private fun returnFilter(document: Document?, css: String, attributekey: String): Array> { + val captchacheck = !document?.select(".cf-captcha-container").isNullOrEmpty() + val cfcheck = !document?.select(".cf-browser-verification").isNullOrEmpty() + return if (captchacheck) { + arrayOf(Pair("", "Solve captcha and force restart app")) + } else if (cfcheck) { + arrayOf(Pair("", "Open 'Latest' and force restart app")) + } else { + document?.select(css)?.map { Pair(it.attr(attributekey).substringBeforeLast("/").substringAfterLast("/"), it.text()) }?.toTypedArray() + ?: arrayOf(Pair("", "Error getting filters")) + } } //Generates the filter lists for app @@ -266,16 +290,20 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { //MRM does not support genre filtering and text search at the same time Filter.Header("NOTE: Filters are ignored if using text search."), Filter.Header("Only one filter can be used at a time."), - GenreFilter(returnFilter(".tagcloud a[href*=/genre/]", "href")), - TagFilter(returnFilter(".tagcloud a[href*=/tag/]","href")), - CatFilter(returnFilter(".level-0", "value")) + GenreFilter(returnFilter(filterDoc, ".tagcloud a[href*=/genre/]", "href")), + TagFilter(returnFilter(filterDoc, ".tagcloud a[href*=/tag/]", "href")), + CatFilter(returnFilter(categoryDoc, ".links a", "abs:href")), + PairingFilter(returnFilter(pairingDoc, ".links a", "abs:href")), + ScanGroupFilter(returnFilter(scangroupDoc, ".links a", "abs:href")) ) return filterList } - private class GenreFilter(GENRES: Array>) : UriSelectFilterPath("Genre", "genre", arrayOf(Pair("","Any"),*GENRES)) - private class TagFilter(POPTAG: Array>) : UriSelectFilterPath("Popular Tags", "tag", arrayOf(Pair("","Any"),*POPTAG)) - private class CatFilter(CATID: Array>) : UriSelectFilterQuery("Categories", "cat", arrayOf(Pair("","Any"), *CATID)) + private class GenreFilter(GENRES: Array>) : UriSelectFilterPath("Genre", "genre", arrayOf(Pair("", "Any"), *GENRES)) + private class TagFilter(POPTAG: Array>) : UriSelectFilterPath("Popular Tags", "tag", arrayOf(Pair("", "Any"), *POPTAG)) + private class CatFilter(CATID: Array>) : UriSelectFilterShortPath("Categories", "cat", arrayOf(Pair("", "Any"), *CATID)) + private class PairingFilter(PAIR: Array>) : UriSelectFilterPath("Pairing", "pairing", arrayOf(Pair("", "Any"), *PAIR)) + private class ScanGroupFilter(GROUP: Array>) : UriSelectFilterPath("Scanlation Group", "group", arrayOf(Pair("", "Any"), *GROUP)) /** * Class that creates a select filter. Each entry in the dropdown has a name and a display name. @@ -293,13 +321,15 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { .appendPath(vals[state].first) } } - private open class UriSelectFilterQuery(displayName: String, val uriParam: String, val vals: Array>, - val firstIsUnspecified: Boolean = true, - defaultValue: Int = 0) : + + private open class UriSelectFilterShortPath(displayName: String, val uriParam: String, val vals: Array>, + val firstIsUnspecified: Boolean = true, + defaultValue: Int = 0) : Filter.Select(displayName, vals.map { it.second }.toTypedArray(), defaultValue), UriFilter { override fun addToUri(uri: Uri.Builder) { if (state != 0 || !firstIsUnspecified) - uri.appendQueryParameter(uriParam, vals[state].first) + uri.appendPath(vals[state].first) + } } @@ -311,3 +341,4 @@ open class MyReadingManga(override val lang: String) : ParsedHttpSource() { } } +