Revert Madara Host Preference (#7239)

* Revert Madara Host Preference

* forgot a line
This commit is contained in:
loocool2 2021-05-26 12:45:12 -07:00 committed by GitHub
parent 0a842f6152
commit 1c12e1801c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 114 deletions

View File

@ -1,16 +1,8 @@
package eu.kanade.tachiyomi.multisrc.madara
import android.app.Application
import android.content.SharedPreferences
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.asObservable
import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.model.Filter
import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.model.MangasPage
@ -30,8 +22,6 @@ import okhttp3.Response
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import rx.Observable
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
@ -45,53 +35,10 @@ abstract class Madara(
override val baseUrl: String,
override val lang: String,
private val dateFormat: SimpleDateFormat = SimpleDateFormat("MMMM dd, yyyy", Locale.US)
) : ParsedHttpSource(), ConfigurableSource {
) : ParsedHttpSource() {
override val supportsLatest = true
// Preferences Code
private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) {
val chooseHostPref = androidx.preference.ListPreference(screen.context).apply {
key = CHOOSE_HOST_Key
title = CHOOSE_HOST_Title
entries = prefsEntries
entryValues = prefsEntryValues
summary = "%s"
setOnPreferenceChangeListener { _, newValue ->
val selected = newValue as String
val index = this.findIndexOfValue(selected)
val entry = entryValues[index] as String
hostValues = prefsEntryValues.toMutableList()
hostValues.removeAt(index)
preferences.edit().putString(CHOOSE_HOST_Key, entry).commit()
}
}
screen.addPreference(chooseHostPref)
}
private fun chooseHostPref() = preferences.getString(CHOOSE_HOST_Key, "")
private fun cleanHostValues(): MutableList<String> {
val hostsList = prefsEntryValues.toMutableList()
hostsList.remove(chooseHostPref())
return hostsList
}
companion object {
private const val CHOOSE_HOST_Title = "Choose the host/server you prefer for images"
private const val CHOOSE_HOST_Key = "choose_host"
private val prefsEntries = arrayOf("Default", "Local", "Amazon", "Imgur", "Flickr", "Picasa (Blogspot)", "Google Photos")
private val prefsEntryValues = arrayOf("", "local", "amazon", "imgur", "flickr", "picasa", "gphotos")
}
private var hostValues = cleanHostValues()
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
@ -508,23 +455,17 @@ abstract class Madara(
with(element) {
select(chapterUrlSelector).first()?.let { urlElement ->
val url = urlElement.attr("abs:href").let {
chapter.url = urlElement.attr("abs:href").let {
it.substringBefore("?style=paged") + if (!it.endsWith(chapterUrlSuffix)) chapterUrlSuffix else ""
}.toHttpUrlOrNull()!!.newBuilder()
if (chooseHostPref() != "")
url.addQueryParameter("host", chooseHostPref())
chapter.url = url.toString()
}
chapter.name = urlElement.text()
}
// Dates can be part of a "new" graphic or plain text
chapter.date_upload = select("img").firstOrNull()?.attr("alt")?.let { parseRelativeDate(it) }
?: parseChapterDate(select("span.chapter-release-date i").firstOrNull()?.text())
}
return chapter
}
@ -606,45 +547,6 @@ abstract class Madara(
return super.pageListRequest(chapter)
}
override fun fetchPageList(chapter: SChapter): Observable<List<Page>> {
var observable = super.fetchPageList(chapter)
var pages = emptyList<Page>()
var index = 0
while (pages.isEmpty() && index < hostValues.size) {
observable.subscribe {
pages = it
}
if (pages.isNotEmpty()) {
if (index > 0) {
// put validHost in first position
val validHost = hostValues[index - 1]
hostValues.removeAt(index - 1)
hostValues.add(0, validHost)
val hostName = prefsEntries[prefsEntryValues.indexOf(validHost)]
Handler(Looper.getMainLooper()).post {
val toast = Toast.makeText(Injekt.get<Application>().applicationContext, "Host : $hostName\nYou may want to switch to this host to avoid unnecessary loading time", Toast.LENGTH_SHORT)
val view = toast.view?.findViewById<TextView>(android.R.id.message)
view?.let { it.gravity = Gravity.CENTER_HORIZONTAL }
toast.show()
}
}
return observable
}
val url = chapter.url.toHttpUrlOrNull()!!.newBuilder()
url.setQueryParameter("host", hostValues[index])
chapter.url = url.toString()
observable = super.fetchPageList(chapter)
index++
}
return observable
}
open val pageListParseSelector = "div.page-break, li.blocks-gallery-item"
override fun pageListParse(document: Document): List<Page> {
@ -660,17 +562,7 @@ abstract class Madara(
}
override fun imageRequest(page: Page): Request {
val headers = headersBuilder()
headers.apply {
add("Referer", page.url)
}
if (page.imageUrl!!.contains("amazonaws.com")) {
headers.apply {
add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
}
}
return GET(page.imageUrl!!, headers.build())
return GET(page.imageUrl!!, headers.newBuilder().set("Referer", page.url).build())
}
override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("Not used")

View File

@ -10,7 +10,7 @@ class MadaraGenerator : ThemeSourceGenerator {
override val themeClass = "Madara"
override val baseVersionCode: Int = 4
override val baseVersionCode: Int = 5
override val sources = listOf(
SingleLang("24hRomance", "https://24hromance.com", "en", className = "Romance24h"),