From 030d2318b3a3b3f9ff3d4e1640bbd28fce2f3a9c Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 17 Feb 2020 13:39:10 -0500 Subject: [PATCH] MangaDex: restore R18 option, but remove unused language cookie header logic MangaDex no longer uses that cookie, so it doesn't do anything. --- src/all/mangadex/build.gradle | 2 +- .../extension/all/mangadex/Mangadex.kt | 114 ++++++++++++++++-- .../extension/all/mangadex/MangadexFactory.kt | 74 ++++++------ .../extension/en/mangadex/MangaDexEnglish.kt | 2 +- 4 files changed, 146 insertions(+), 46 deletions(-) diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index 09e21c07d..b20de0b49 100644 --- a/src/all/mangadex/build.gradle +++ b/src/all/mangadex/build.gradle @@ -5,7 +5,7 @@ ext { appName = 'Tachiyomi: MangaDex' pkgNameSuffix = 'all.mangadex' extClass = '.MangadexFactory' - extVersionCode = 81 + extVersionCode = 82 libVersion = '1.2' } diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/Mangadex.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/Mangadex.kt index 707d5b3e7..ac2137212 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/Mangadex.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/Mangadex.kt @@ -23,13 +23,14 @@ import org.jsoup.parser.Parser import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.net.URLEncoder import java.util.Date import java.util.concurrent.TimeUnit +import kotlin.collections.set abstract class Mangadex( override val lang: String, - private val internalLang: String, - private val langCode: Int + private val internalLang: String ) : ConfigurableSource, ParsedHttpSource() { override val name = "MangaDex" @@ -47,15 +48,39 @@ abstract class Mangadex( private val rateLimitInterceptor = RateLimitInterceptor(4) override val client: OkHttpClient = network.client.newBuilder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) .addNetworkInterceptor(rateLimitInterceptor) .build() + private fun clientBuilder(): OkHttpClient = clientBuilder(getShowR18()) + + private fun clientBuilder(r18Toggle: Int): OkHttpClient = network.client.newBuilder() + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .addNetworkInterceptor(rateLimitInterceptor) + .addNetworkInterceptor { chain -> + val originalCookies = chain.request().header("Cookie") ?: "" + val newReq = chain + .request() + .newBuilder() + .header("Cookie", "$originalCookies; ${cookiesHeader(r18Toggle)}") + .build() + chain.proceed(newReq) + }.build()!! + override fun headersBuilder() = Headers.Builder().apply { add("User-Agent", "Tachiyomi " + System.getProperty("http.agent")) } + private fun cookiesHeader(r18Toggle: Int): String { + val cookies = mutableMapOf() + cookies["mangadex_h_toggle"] = r18Toggle.toString() + return buildCookies(cookies) + } + + private fun buildCookies(cookies: Map) = cookies.entries.joinToString(separator = "; ", postfix = ";") { + "${URLEncoder.encode(it.key, "UTF-8")}=${URLEncoder.encode(it.value, "UTF-8")}" + } + override fun popularMangaSelector() = "div.manga-entry" override fun latestUpdatesSelector() = "tr a.manga_title" @@ -110,6 +135,22 @@ abstract class Mangadex( override fun searchMangaNextPageSelector() = ".pagination li:not(.disabled) span[title*=last page]:not(disabled)" + override fun fetchPopularManga(page: Int): Observable { + return clientBuilder().newCall(popularMangaRequest(page)) + .asObservableSuccess() + .map { response -> + popularMangaParse(response) + } + } + + override fun fetchLatestUpdates(page: Int): Observable { + return clientBuilder().newCall(latestUpdatesRequest(page)) + .asObservableSuccess() + .map { response -> + latestUpdatesParse(response) + } + } + override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable { return if (query.startsWith(PREFIX_ID_SEARCH)) { val realQuery = query.removePrefix(PREFIX_ID_SEARCH) @@ -121,7 +162,7 @@ abstract class Mangadex( MangasPage(listOf(details), false) } } else { - client.newCall(searchMangaRequest(page, query, filters)) + getSearchClient(filters).newCall(searchMangaRequest(page, query, filters)) .asObservableSuccess() .map { response -> searchMangaParse(response) @@ -129,6 +170,22 @@ abstract class Mangadex( } } + private fun getSearchClient(filters: FilterList): OkHttpClient { + filters.forEach { filter -> + when (filter) { + is R18 -> { + return when (filter.state) { + 1 -> clientBuilder(ALL) + 2 -> clientBuilder(ONLY_R18) + 3 -> clientBuilder(NO_R18) + else -> clientBuilder() + } + } + } + } + return clientBuilder() + } + private var groupSearch = "" override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { @@ -280,7 +337,7 @@ abstract class Mangadex( } override fun fetchMangaDetails(manga: SManga): Observable { - return client.newCall(apiRequest(manga)) + return clientBuilder().newCall(apiRequest(manga)) .asObservableSuccess() .map { response -> mangaDetailsParse(response).apply { initialized = true } @@ -352,7 +409,7 @@ abstract class Mangadex( override fun chapterListSelector() = "" override fun fetchChapterList(manga: SManga): Observable> { - return client.newCall(apiRequest(manga)) + return clientBuilder().newCall(apiRequest(manga)) .asObservableSuccess() .map { response -> chapterListParse(response) @@ -522,6 +579,21 @@ abstract class Mangadex( } override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { + val myPref = androidx.preference.ListPreference(screen.context).apply { + key = SHOW_R18_PREF_Title + title = SHOW_R18_PREF_Title + + title = SHOW_R18_PREF_Title + entries = arrayOf("Show No R18+", "Show All", "Show Only R18+") + entryValues = arrayOf("0", "1", "2") + summary = "%s" + + setOnPreferenceChangeListener { _, newValue -> + val selected = newValue as String + val index = this.findIndexOfValue(selected) + preferences.edit().putInt(SHOW_R18_PREF, index).commit() + } + } val thumbsPref = androidx.preference.ListPreference(screen.context).apply { key = SHOW_THUMBNAIL_PREF_Title title = SHOW_THUMBNAIL_PREF_Title @@ -550,11 +622,27 @@ abstract class Mangadex( } } + screen.addPreference(myPref) screen.addPreference(thumbsPref) screen.addPreference(serverPref) } override fun setupPreferenceScreen(screen: PreferenceScreen) { + val myPref = ListPreference(screen.context).apply { + key = SHOW_R18_PREF_Title + title = SHOW_R18_PREF_Title + + title = SHOW_R18_PREF_Title + entries = arrayOf("Show No R18+", "Show All", "Show Only R18+") + entryValues = arrayOf("0", "1", "2") + summary = "%s" + + setOnPreferenceChangeListener { _, newValue -> + val selected = newValue as String + val index = this.findIndexOfValue(selected) + preferences.edit().putInt(SHOW_R18_PREF, index).commit() + } + } val thumbsPref = ListPreference(screen.context).apply { key = SHOW_THUMBNAIL_PREF_Title title = SHOW_THUMBNAIL_PREF_Title @@ -583,10 +671,12 @@ abstract class Mangadex( } } + screen.addPreference(myPref) screen.addPreference(thumbsPref) screen.addPreference(serverPref) } + private fun getShowR18(): Int = preferences.getInt(SHOW_R18_PREF, 0) private fun getShowThumbnail(): Int = preferences.getInt(SHOW_THUMBNAIL_PREF, 0) private fun getServer(): String { val default = SERVER_PREF_ENTRY_VALUES.first() @@ -601,6 +691,7 @@ abstract class Mangadex( private class ContentList(contents: List) : Filter.Group("Content", contents) private class FormatList(formats: List) : Filter.Group("Format", formats) private class GenreList(genres: List) : Filter.Group("Genres", genres) + private class R18 : Filter.Select("R18+", arrayOf("Default", "Show all", "Show only", "Show none")) private class ScanGroup(name: String) : Filter.Text(name) private fun getDemographic() = listOf( @@ -631,6 +722,7 @@ abstract class Mangadex( override fun getFilterList() = FilterList( TextField("Author", "author"), TextField("Artist", "artist"), + R18(), SortFilter(), Demographic(getDemographic()), PublicationStatus(getPublicationStatus()), @@ -741,6 +833,14 @@ abstract class Mangadex( companion object { private val WHITESPACE_REGEX = "\\s".toRegex() + // This number matches to the cookie + private const val NO_R18 = 0 + private const val ALL = 1 + private const val ONLY_R18 = 2 + + private const val SHOW_R18_PREF_Title = "Default R18 Setting" + private const val SHOW_R18_PREF = "showR18Default" + private const val LOW_QUALITY = 1 private const val SHOW_THUMBNAIL_PREF_Title = "Default thumbnail quality" diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangadexFactory.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangadexFactory.kt index 493f2032b..51e236f7f 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangadexFactory.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangadexFactory.kt @@ -46,40 +46,40 @@ class MangadexFactory : SourceFactory { ) } -class MangaDexPolish : Mangadex("pl", "pl", 3) -class MangaDexItalian : Mangadex("it", "it", 6) -class MangaDexRussian : Mangadex("ru", "ru", 7) -class MangaDexGerman : Mangadex("de", "de", 8) -class MangaDexFrench : Mangadex("fr", "fr", 10) -class MangaDexVietnamese : Mangadex("vi", "vn", 12) -class MangaDexSpanishSpain : Mangadex("es", "es", 15) -class MangaDexSpanishLTAM : Mangadex("es-419", "mx", 29) -class MangaDexCatalan : Mangadex("ca", "ct", 33) -class MangaDexPortuguesePortugal : Mangadex("pt", "pt", 17) -class MangaDexPortugueseBrazil : Mangadex("pt-BR", "br", 16) -class MangaDexSwedish : Mangadex("sv", "se", 18) -class MangaDexTurkish : Mangadex("tr", "tr", 26) -class MangaDexIndonesian : Mangadex("id", "id", 27) -class MangaDexHungarian : Mangadex("hu", "hu", 9) -class MangaDexBulgarian : Mangadex("bg", "bg", 14) -class MangaDexFilipino : Mangadex("fil", "ph", 34) -class MangaDexDutch : Mangadex("nl", "nl", 5) -class MangaDexArabic : Mangadex("ar", "sa", 19) -class MangaDexChineseSimp : Mangadex("zh-Hans", "cn", 21) -class MangaDexChineseTrad : Mangadex("zh-Hant", "hk", 35) -class MangaDexThai : Mangadex("th", "th", 32) -class MangaDexBengali : Mangadex("bn", "bd", 22) -class MangaDexBurmese : Mangadex("my", "mm", 37) -class MangaDexCzech : Mangadex("cs", "cz", 24) -class MangaDexDanish : Mangadex("da", "dk", 20) -class MangaDexFinnish : Mangadex("fi", "fi", 11) -class MangaDexGreek : Mangadex("el", "gr", 13) -class MangaDexJapanese : Mangadex("ja", "jp", 2) -class MangaDexKorean : Mangadex("ko", "kr", 28) -class MangaDexLithuanian : Mangadex("lt", "lt", 38) -class MangaDexMalay : Mangadex("ms", "my", 31) -class MangaDexMongolian : Mangadex("mn", "mn", 25) -class MangaDexPersian : Mangadex("fa", "ir", 30) -class MangaDexRomanian : Mangadex("ro", "ro", 23) -class MangaDexSerboCroatian : Mangadex("sh", "rs", 4) -class MangaDexUkrainian : Mangadex("uk", "ua", 36) +class MangaDexPolish : Mangadex("pl", "pl") +class MangaDexItalian : Mangadex("it", "it") +class MangaDexRussian : Mangadex("ru", "ru") +class MangaDexGerman : Mangadex("de", "de") +class MangaDexFrench : Mangadex("fr", "fr") +class MangaDexVietnamese : Mangadex("vi", "vn") +class MangaDexSpanishSpain : Mangadex("es", "es") +class MangaDexSpanishLTAM : Mangadex("es-419", "mx") +class MangaDexCatalan : Mangadex("ca", "ct") +class MangaDexPortuguesePortugal : Mangadex("pt", "pt") +class MangaDexPortugueseBrazil : Mangadex("pt-BR", "br") +class MangaDexSwedish : Mangadex("sv", "se") +class MangaDexTurkish : Mangadex("tr", "tr") +class MangaDexIndonesian : Mangadex("id", "id") +class MangaDexHungarian : Mangadex("hu", "hu") +class MangaDexBulgarian : Mangadex("bg", "bg") +class MangaDexFilipino : Mangadex("fil", "ph") +class MangaDexDutch : Mangadex("nl", "nl") +class MangaDexArabic : Mangadex("ar", "sa") +class MangaDexChineseSimp : Mangadex("zh-Hans", "cn") +class MangaDexChineseTrad : Mangadex("zh-Hant", "hk") +class MangaDexThai : Mangadex("th", "th") +class MangaDexBengali : Mangadex("bn", "bd") +class MangaDexBurmese : Mangadex("my", "mm") +class MangaDexCzech : Mangadex("cs", "cz") +class MangaDexDanish : Mangadex("da", "dk") +class MangaDexFinnish : Mangadex("fi", "fi") +class MangaDexGreek : Mangadex("el", "gr") +class MangaDexJapanese : Mangadex("ja", "jp") +class MangaDexKorean : Mangadex("ko", "kr") +class MangaDexLithuanian : Mangadex("lt", "lt") +class MangaDexMalay : Mangadex("ms", "my") +class MangaDexMongolian : Mangadex("mn", "mn") +class MangaDexPersian : Mangadex("fa", "ir") +class MangaDexRomanian : Mangadex("ro", "ro") +class MangaDexSerboCroatian : Mangadex("sh", "rs") +class MangaDexUkrainian : Mangadex("uk", "ua") diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/en/mangadex/MangaDexEnglish.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/en/mangadex/MangaDexEnglish.kt index be03df6d2..8b45fbf84 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/en/mangadex/MangaDexEnglish.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/en/mangadex/MangaDexEnglish.kt @@ -1,3 +1,3 @@ package eu.kanade.tachiyomi.extension.all.mangadex -class MangaDexEnglish : Mangadex("en", "gb", 1) +class MangaDexEnglish : Mangadex("en", "gb")