From fdec2f1009004484f9a8f05a7e467fa7df0730d4 Mon Sep 17 00:00:00 2001 From: Mike <51273546+SnakeDoc83@users.noreply.github.com> Date: Fri, 10 Jul 2020 01:08:48 -0400 Subject: [PATCH] MyReadingManga - some refactoring, added language filter (#3741) --- src/all/myreadingmanga/build.gradle | 2 +- .../all/myreadingmanga/MyReadingManga.kt | 91 ++++++++++--------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/all/myreadingmanga/build.gradle b/src/all/myreadingmanga/build.gradle index 2f9678e45..b00c67cee 100644 --- a/src/all/myreadingmanga/build.gradle +++ b/src/all/myreadingmanga/build.gradle @@ -5,7 +5,7 @@ ext { extName = 'MyReadingManga' pkgNameSuffix = 'all.myreadingmanga' extClass = '.MyReadingMangaFactory' - extVersionCode = 35 + extVersionCode = 36 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 068d6c84f..a0a491f67 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 @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.all.myreadingmanga import android.annotation.SuppressLint import android.net.Uri +import android.webkit.URLUtil import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.asObservableSuccess import eu.kanade.tachiyomi.source.model.Filter @@ -67,7 +68,9 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin var fqIndex = 0 val uri = Uri.parse("$baseUrl/search/").buildUpon() .appendQueryParameter("wpsolr_q", query) - .appendQueryParameter("wpsolr_fq[$fqIndex]", "lang_str:$siteLang") + @Suppress("NullableBooleanElvis") + if ((filters.find { it is EnforceLanguageFilter } as? Filter.CheckBox)?.state ?: true) + uri.appendQueryParameter("wpsolr_fq[$fqIndex]", "lang_str:$siteLang") .appendQueryParameter("wpsolr_page", page.toString()) filters.forEach { if (it is UriFilter) { @@ -100,21 +103,22 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin return manga } + private val extensionRegex = Regex("""\.(jpg|png|jpeg)""") + private fun getImage(element: Element): String { - var url = - when { - element.attr("data-src").endsWith(".jpg") || element.attr("data-src").endsWith(".png") || element.attr("data-src").endsWith(".jpeg") -> element.attr("data-src") - element.attr("src").endsWith(".jpg") || element.attr("src").endsWith(".png") || element.attr("src").endsWith(".jpeg") -> element.attr("src") - else -> element.attr("data-lazy-src") - } - if (url.startsWith("//")) { - url = "https:$url" + return when { + element.attr("data-src").contains(extensionRegex) -> element.attr("abs:data-src") + element.attr("src").contains(extensionRegex) -> element.attr("abs:src") + else -> element.attr("abs:data-lazy-src") } - return url } // removes resizing - private fun getThumbnail(thumbnailUrl: String) = thumbnailUrl.substringBeforeLast("-") + "." + thumbnailUrl.substringAfterLast(".") + private fun getThumbnail(thumbnailUrl: String?): String? { + thumbnailUrl ?: return null + val url = thumbnailUrl.substringBeforeLast("-") + "." + thumbnailUrl.substringAfterLast(".") + return if (URLUtil.isValidUrl(url)) url else null + } // cleans up the name removing author and language from the title private fun cleanTitle(title: String) = title.substringBeforeLast("[").substringAfterLast("]").substringBeforeLast("(").trim() @@ -123,7 +127,7 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin // Manga Details override fun fetchMangaDetails(manga: SManga): Observable { - val needCover = manga.thumbnail_url.isNullOrEmpty() + val needCover = manga.thumbnail_url?.let { !client.newCall(GET(it, headers)).execute().isSuccessful } ?: true return client.newCall(mangaDetailsRequest(manga)) .asObservableSuccess() @@ -132,25 +136,28 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin } } - private fun mangaDetailsParse(document: Document, needCover: Boolean): SManga { - val manga = SManga.create() - manga.author = cleanAuthor(document.select("h1").text()) - manga.artist = cleanAuthor(document.select("h1").text()) - manga.genre = document.select(".entry-header p a[href*=genre]").joinToString(", ") { it.text() } - val extendedDescription = document.select(".entry-content p:not(p:containsOwn(|)):not(.chapter-class + p)")?.joinToString("\n") { it.text() } - manga.description = document.select("h1").text() + if (extendedDescription.isNullOrEmpty()) "" else "\n\n$extendedDescription" - manga.status = when (document.select("a[href*=status]")?.first()?.text()) { - "Ongoing" -> SManga.ONGOING - "Completed" -> SManga.COMPLETED - else -> SManga.UNKNOWN - } + private fun mangaDetailsParse(document: Document, needCover: Boolean = true): SManga { + return SManga.create().apply { + author = cleanAuthor(document.select("h1").text()) + artist = author + genre = document.select(".entry-header p a[href*=genre]").joinToString { it.text() } + val basicDescription = document.select("h1").text() + // too troublesome to achieve 100% accuracy assigning scanlator group during chapterListParse + val scanlatedBy = document.select(".entry-terms:has(a[href*=group])").firstOrNull() + ?.select("a[href*=group]")?.joinToString(prefix = "Scanlated by: ") { it.text() } + val extendedDescription = document.select(".entry-content p:not(p:containsOwn(|)):not(.chapter-class + p)")?.joinToString("\n") { it.text() } + description = listOfNotNull(basicDescription, scanlatedBy, extendedDescription).joinToString("\n") + status = when (document.select("a[href*=status]")?.first()?.text()) { + "Ongoing" -> SManga.ONGOING + "Completed" -> SManga.COMPLETED + else -> SManga.UNKNOWN + } - if (needCover) { - manga.thumbnail_url = getThumbnail(client.newCall(GET("$baseUrl/search/?search=${document.location()}", headers)) - .execute().asJsoup().select("div.wdm_results div.p_content img").first().attr("abs:src")) + if (needCover) { + thumbnail_url = getThumbnail(getImage(client.newCall(GET("$baseUrl/search/?search=${document.location()}", headers)) + .execute().asJsoup().select("div.wdm_results div.p_content img").first())) + } } - - return manga } override fun mangaDetailsParse(document: Document) = throw Exception("Not used") @@ -167,9 +174,8 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin 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() // create first chapter since its on main manga page - chapters.add(createChapter("1", document.baseUri(), date, chfirstname, scangroup)) + chapters.add(createChapter("1", document.baseUri(), date, chfirstname)) // see if there are multiple chapters or not document.select(chapterListSelector())?.let { it -> it.forEach { @@ -177,7 +183,7 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin val pageNumber = it.text() 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)) + chapters.add(createChapter(it.text(), document.baseUri(), date, chname)) } } } @@ -186,33 +192,27 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin } 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 ?: 0 } - private fun createChapter(pageNumber: String, mangaUrl: String, date: Long, chname: String, scangroup: String?): SChapter { + private fun createChapter(pageNumber: String, mangaUrl: String, date: Long, chname: String): SChapter { val chapter = SChapter.create() chapter.setUrlWithoutDomain("$mangaUrl/$pageNumber") chapter.name = chname chapter.date_upload = date - chapter.scanlator = scangroup return chapter } override fun chapterFromElement(element: Element) = throw Exception("Not used") // Pages - override fun pageListParse(response: Response): List { - val body = response.asJsoup() - val pages = mutableListOf() - val elements = body.select("div>img") - for (i in 0 until elements.size) { - pages.add(Page(i, "", getImage(elements[i]))) + + override fun pageListParse(document: Document): List { + return document.select("div > img").mapIndexed { i, img -> + Page(i, "", getImage(img)) } - return pages } - override fun pageListParse(document: Document) = throw Exception("Not used") - override fun imageUrlRequest(page: Page) = throw Exception("Not used") override fun imageUrlParse(document: Document) = throw Exception("Not used") // Filter Parsing, grabs pages as document and filters out Genres, Popular Tags, and Categories, Parings, and Scan Groups @@ -254,6 +254,7 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin // Generates the filter lists for app override fun getFilterList(): FilterList { return FilterList( + EnforceLanguageFilter(), GenreFilter(returnFilter(getCache(cachedPagesUrls["genres"]!!), ".tagcloud a[href*=/genre/]")), TagFilter(returnFilter(getCache(cachedPagesUrls["tags"]!!), ".tagcloud a[href*=/tag/]")), CatFilter(returnFilter(getCache(cachedPagesUrls["categories"]!!), ".links a")), @@ -262,6 +263,8 @@ open class MyReadingManga(override val lang: String, private val siteLang: Strin ) } + private class EnforceLanguageFilter : Filter.CheckBox("Enforce language", true) + private class GenreFilter(GENRES: Array) : UriSelectFilter("Genre", "genre_str", arrayOf("Any", *GENRES)) private class TagFilter(POPTAG: Array) : UriSelectFilter("Popular Tags", "tags", arrayOf("Any", *POPTAG)) private class CatFilter(CATID: Array) : UriSelectFilter("Categories", "categories", arrayOf("Any", *CATID))