MangaDex: restore R18 option, but remove unused language cookie header logic

MangaDex no longer uses that cookie, so it doesn't do anything.
This commit is contained in:
arkon 2020-02-17 13:39:10 -05:00
parent 2d8061a677
commit 030d2318b3
4 changed files with 146 additions and 46 deletions

View File

@ -5,7 +5,7 @@ ext {
appName = 'Tachiyomi: MangaDex' appName = 'Tachiyomi: MangaDex'
pkgNameSuffix = 'all.mangadex' pkgNameSuffix = 'all.mangadex'
extClass = '.MangadexFactory' extClass = '.MangadexFactory'
extVersionCode = 81 extVersionCode = 82
libVersion = '1.2' libVersion = '1.2'
} }

View File

@ -23,13 +23,14 @@ import org.jsoup.parser.Parser
import rx.Observable import rx.Observable
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.net.URLEncoder
import java.util.Date import java.util.Date
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.collections.set
abstract class Mangadex( abstract class Mangadex(
override val lang: String, override val lang: String,
private val internalLang: String, private val internalLang: String
private val langCode: Int
) : ConfigurableSource, ParsedHttpSource() { ) : ConfigurableSource, ParsedHttpSource() {
override val name = "MangaDex" override val name = "MangaDex"
@ -47,15 +48,39 @@ abstract class Mangadex(
private val rateLimitInterceptor = RateLimitInterceptor(4) private val rateLimitInterceptor = RateLimitInterceptor(4)
override val client: OkHttpClient = network.client.newBuilder() override val client: OkHttpClient = network.client.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addNetworkInterceptor(rateLimitInterceptor) .addNetworkInterceptor(rateLimitInterceptor)
.build() .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 { override fun headersBuilder() = Headers.Builder().apply {
add("User-Agent", "Tachiyomi " + System.getProperty("http.agent")) add("User-Agent", "Tachiyomi " + System.getProperty("http.agent"))
} }
private fun cookiesHeader(r18Toggle: Int): String {
val cookies = mutableMapOf<String, String>()
cookies["mangadex_h_toggle"] = r18Toggle.toString()
return buildCookies(cookies)
}
private fun buildCookies(cookies: Map<String, String>) = 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 popularMangaSelector() = "div.manga-entry"
override fun latestUpdatesSelector() = "tr a.manga_title" 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 searchMangaNextPageSelector() = ".pagination li:not(.disabled) span[title*=last page]:not(disabled)"
override fun fetchPopularManga(page: Int): Observable<MangasPage> {
return clientBuilder().newCall(popularMangaRequest(page))
.asObservableSuccess()
.map { response ->
popularMangaParse(response)
}
}
override fun fetchLatestUpdates(page: Int): Observable<MangasPage> {
return clientBuilder().newCall(latestUpdatesRequest(page))
.asObservableSuccess()
.map { response ->
latestUpdatesParse(response)
}
}
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> { override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> {
return if (query.startsWith(PREFIX_ID_SEARCH)) { return if (query.startsWith(PREFIX_ID_SEARCH)) {
val realQuery = query.removePrefix(PREFIX_ID_SEARCH) val realQuery = query.removePrefix(PREFIX_ID_SEARCH)
@ -121,7 +162,7 @@ abstract class Mangadex(
MangasPage(listOf(details), false) MangasPage(listOf(details), false)
} }
} else { } else {
client.newCall(searchMangaRequest(page, query, filters)) getSearchClient(filters).newCall(searchMangaRequest(page, query, filters))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
searchMangaParse(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 = "" private var groupSearch = ""
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
@ -280,7 +337,7 @@ abstract class Mangadex(
} }
override fun fetchMangaDetails(manga: SManga): Observable<SManga> { override fun fetchMangaDetails(manga: SManga): Observable<SManga> {
return client.newCall(apiRequest(manga)) return clientBuilder().newCall(apiRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
mangaDetailsParse(response).apply { initialized = true } mangaDetailsParse(response).apply { initialized = true }
@ -352,7 +409,7 @@ abstract class Mangadex(
override fun chapterListSelector() = "" override fun chapterListSelector() = ""
override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> { override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> {
return client.newCall(apiRequest(manga)) return clientBuilder().newCall(apiRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
chapterListParse(response) chapterListParse(response)
@ -522,6 +579,21 @@ abstract class Mangadex(
} }
override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { 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 { val thumbsPref = androidx.preference.ListPreference(screen.context).apply {
key = SHOW_THUMBNAIL_PREF_Title key = SHOW_THUMBNAIL_PREF_Title
title = SHOW_THUMBNAIL_PREF_Title title = SHOW_THUMBNAIL_PREF_Title
@ -550,11 +622,27 @@ abstract class Mangadex(
} }
} }
screen.addPreference(myPref)
screen.addPreference(thumbsPref) screen.addPreference(thumbsPref)
screen.addPreference(serverPref) screen.addPreference(serverPref)
} }
override fun setupPreferenceScreen(screen: PreferenceScreen) { 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 { val thumbsPref = ListPreference(screen.context).apply {
key = SHOW_THUMBNAIL_PREF_Title key = SHOW_THUMBNAIL_PREF_Title
title = SHOW_THUMBNAIL_PREF_Title title = SHOW_THUMBNAIL_PREF_Title
@ -583,10 +671,12 @@ abstract class Mangadex(
} }
} }
screen.addPreference(myPref)
screen.addPreference(thumbsPref) screen.addPreference(thumbsPref)
screen.addPreference(serverPref) 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 getShowThumbnail(): Int = preferences.getInt(SHOW_THUMBNAIL_PREF, 0)
private fun getServer(): String { private fun getServer(): String {
val default = SERVER_PREF_ENTRY_VALUES.first() val default = SERVER_PREF_ENTRY_VALUES.first()
@ -601,6 +691,7 @@ abstract class Mangadex(
private class ContentList(contents: List<Tag>) : Filter.Group<Tag>("Content", contents) private class ContentList(contents: List<Tag>) : Filter.Group<Tag>("Content", contents)
private class FormatList(formats: List<Tag>) : Filter.Group<Tag>("Format", formats) private class FormatList(formats: List<Tag>) : Filter.Group<Tag>("Format", formats)
private class GenreList(genres: List<Tag>) : Filter.Group<Tag>("Genres", genres) private class GenreList(genres: List<Tag>) : Filter.Group<Tag>("Genres", genres)
private class R18 : Filter.Select<String>("R18+", arrayOf("Default", "Show all", "Show only", "Show none"))
private class ScanGroup(name: String) : Filter.Text(name) private class ScanGroup(name: String) : Filter.Text(name)
private fun getDemographic() = listOf( private fun getDemographic() = listOf(
@ -631,6 +722,7 @@ abstract class Mangadex(
override fun getFilterList() = FilterList( override fun getFilterList() = FilterList(
TextField("Author", "author"), TextField("Author", "author"),
TextField("Artist", "artist"), TextField("Artist", "artist"),
R18(),
SortFilter(), SortFilter(),
Demographic(getDemographic()), Demographic(getDemographic()),
PublicationStatus(getPublicationStatus()), PublicationStatus(getPublicationStatus()),
@ -741,6 +833,14 @@ abstract class Mangadex(
companion object { companion object {
private val WHITESPACE_REGEX = "\\s".toRegex() 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 LOW_QUALITY = 1
private const val SHOW_THUMBNAIL_PREF_Title = "Default thumbnail quality" private const val SHOW_THUMBNAIL_PREF_Title = "Default thumbnail quality"

View File

@ -46,40 +46,40 @@ class MangadexFactory : SourceFactory {
) )
} }
class MangaDexPolish : Mangadex("pl", "pl", 3) class MangaDexPolish : Mangadex("pl", "pl")
class MangaDexItalian : Mangadex("it", "it", 6) class MangaDexItalian : Mangadex("it", "it")
class MangaDexRussian : Mangadex("ru", "ru", 7) class MangaDexRussian : Mangadex("ru", "ru")
class MangaDexGerman : Mangadex("de", "de", 8) class MangaDexGerman : Mangadex("de", "de")
class MangaDexFrench : Mangadex("fr", "fr", 10) class MangaDexFrench : Mangadex("fr", "fr")
class MangaDexVietnamese : Mangadex("vi", "vn", 12) class MangaDexVietnamese : Mangadex("vi", "vn")
class MangaDexSpanishSpain : Mangadex("es", "es", 15) class MangaDexSpanishSpain : Mangadex("es", "es")
class MangaDexSpanishLTAM : Mangadex("es-419", "mx", 29) class MangaDexSpanishLTAM : Mangadex("es-419", "mx")
class MangaDexCatalan : Mangadex("ca", "ct", 33) class MangaDexCatalan : Mangadex("ca", "ct")
class MangaDexPortuguesePortugal : Mangadex("pt", "pt", 17) class MangaDexPortuguesePortugal : Mangadex("pt", "pt")
class MangaDexPortugueseBrazil : Mangadex("pt-BR", "br", 16) class MangaDexPortugueseBrazil : Mangadex("pt-BR", "br")
class MangaDexSwedish : Mangadex("sv", "se", 18) class MangaDexSwedish : Mangadex("sv", "se")
class MangaDexTurkish : Mangadex("tr", "tr", 26) class MangaDexTurkish : Mangadex("tr", "tr")
class MangaDexIndonesian : Mangadex("id", "id", 27) class MangaDexIndonesian : Mangadex("id", "id")
class MangaDexHungarian : Mangadex("hu", "hu", 9) class MangaDexHungarian : Mangadex("hu", "hu")
class MangaDexBulgarian : Mangadex("bg", "bg", 14) class MangaDexBulgarian : Mangadex("bg", "bg")
class MangaDexFilipino : Mangadex("fil", "ph", 34) class MangaDexFilipino : Mangadex("fil", "ph")
class MangaDexDutch : Mangadex("nl", "nl", 5) class MangaDexDutch : Mangadex("nl", "nl")
class MangaDexArabic : Mangadex("ar", "sa", 19) class MangaDexArabic : Mangadex("ar", "sa")
class MangaDexChineseSimp : Mangadex("zh-Hans", "cn", 21) class MangaDexChineseSimp : Mangadex("zh-Hans", "cn")
class MangaDexChineseTrad : Mangadex("zh-Hant", "hk", 35) class MangaDexChineseTrad : Mangadex("zh-Hant", "hk")
class MangaDexThai : Mangadex("th", "th", 32) class MangaDexThai : Mangadex("th", "th")
class MangaDexBengali : Mangadex("bn", "bd", 22) class MangaDexBengali : Mangadex("bn", "bd")
class MangaDexBurmese : Mangadex("my", "mm", 37) class MangaDexBurmese : Mangadex("my", "mm")
class MangaDexCzech : Mangadex("cs", "cz", 24) class MangaDexCzech : Mangadex("cs", "cz")
class MangaDexDanish : Mangadex("da", "dk", 20) class MangaDexDanish : Mangadex("da", "dk")
class MangaDexFinnish : Mangadex("fi", "fi", 11) class MangaDexFinnish : Mangadex("fi", "fi")
class MangaDexGreek : Mangadex("el", "gr", 13) class MangaDexGreek : Mangadex("el", "gr")
class MangaDexJapanese : Mangadex("ja", "jp", 2) class MangaDexJapanese : Mangadex("ja", "jp")
class MangaDexKorean : Mangadex("ko", "kr", 28) class MangaDexKorean : Mangadex("ko", "kr")
class MangaDexLithuanian : Mangadex("lt", "lt", 38) class MangaDexLithuanian : Mangadex("lt", "lt")
class MangaDexMalay : Mangadex("ms", "my", 31) class MangaDexMalay : Mangadex("ms", "my")
class MangaDexMongolian : Mangadex("mn", "mn", 25) class MangaDexMongolian : Mangadex("mn", "mn")
class MangaDexPersian : Mangadex("fa", "ir", 30) class MangaDexPersian : Mangadex("fa", "ir")
class MangaDexRomanian : Mangadex("ro", "ro", 23) class MangaDexRomanian : Mangadex("ro", "ro")
class MangaDexSerboCroatian : Mangadex("sh", "rs", 4) class MangaDexSerboCroatian : Mangadex("sh", "rs")
class MangaDexUkrainian : Mangadex("uk", "ua", 36) class MangaDexUkrainian : Mangadex("uk", "ua")

View File

@ -1,3 +1,3 @@
package eu.kanade.tachiyomi.extension.all.mangadex package eu.kanade.tachiyomi.extension.all.mangadex
class MangaDexEnglish : Mangadex("en", "gb", 1) class MangaDexEnglish : Mangadex("en", "gb")