From 7fc0445474465ba7ed7fb9551ef22c0ebfd8c71c Mon Sep 17 00:00:00 2001 From: bapeey <90949336+bapeey@users.noreply.github.com> Date: Wed, 8 Jan 2025 09:05:01 -0500 Subject: [PATCH] Olympus Scanlation: Update domain automatically (#7060) * update domain * here too * cache pref --- src/es/olympusscanlation/build.gradle | 2 +- .../es/olympusscanlation/OlympusScanlation.kt | 117 ++++++++++++++++-- 2 files changed, 111 insertions(+), 8 deletions(-) diff --git a/src/es/olympusscanlation/build.gradle b/src/es/olympusscanlation/build.gradle index 9fcd2850b..75985470c 100644 --- a/src/es/olympusscanlation/build.gradle +++ b/src/es/olympusscanlation/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Olympus Scanlation' extClass = '.OlympusScanlation' - extVersionCode = 12 + extVersionCode = 13 } apply from: "$rootDir/common.gradle" diff --git a/src/es/olympusscanlation/src/eu/kanade/tachiyomi/extension/es/olympusscanlation/OlympusScanlation.kt b/src/es/olympusscanlation/src/eu/kanade/tachiyomi/extension/es/olympusscanlation/OlympusScanlation.kt index 421645898..759b0a762 100644 --- a/src/es/olympusscanlation/src/eu/kanade/tachiyomi/extension/es/olympusscanlation/OlympusScanlation.kt +++ b/src/es/olympusscanlation/src/eu/kanade/tachiyomi/extension/es/olympusscanlation/OlympusScanlation.kt @@ -1,7 +1,14 @@ package eu.kanade.tachiyomi.extension.es.olympusscanlation +import android.app.Application +import android.content.SharedPreferences +import android.widget.Toast +import androidx.preference.CheckBoxPreference +import androidx.preference.EditTextPreference +import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.interceptor.rateLimitHost +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 @@ -9,33 +16,67 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.util.asJsoup import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Request import okhttp3.Response +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.text.SimpleDateFormat import java.util.Locale import java.util.TimeZone import kotlin.concurrent.thread -class OlympusScanlation : HttpSource() { +class OlympusScanlation : HttpSource(), ConfigurableSource { override val versionId = 2 + private val isCi = System.getenv("CI") == "true" - override val baseUrl: String = "https://olympuscomic.com" - private val apiBaseUrl: String = "https://dashboard.olympuscomic.com" + override val baseUrl: String get() = when { + isCi -> defaultBaseUrl + else -> preferences.prefBaseUrl + } + + private val defaultBaseUrl: String = "https://olympuslectura.com" + + private val fetchedDomainUrl: String by lazy { + if (!preferences.fetchDomainPref()) return@lazy preferences.prefBaseUrl + try { + val initClient = network.cloudflareClient + val headers = super.headersBuilder().build() + val document = initClient.newCall(GET("https://olympus.pages.dev", headers)).execute().asJsoup() + val domain = document.selectFirst("meta[property=og:url]")?.attr("content") + ?: return@lazy preferences.prefBaseUrl + val host = initClient.newCall(GET(domain, headers)).execute().request.url.host + val newDomain = "https://$host" + preferences.prefBaseUrl = newDomain + newDomain + } catch (_: Exception) { + preferences.prefBaseUrl + } + } + + private val apiBaseUrl by lazy { + fetchedDomainUrl.replace("https://", "https://dashboard.") + } override val lang: String = "es" override val name: String = "Olympus Scanlation" override val supportsLatest: Boolean = true - override val client = network.cloudflareClient.newBuilder() - .rateLimitHost(baseUrl.toHttpUrl(), 1, 2) - .rateLimitHost(apiBaseUrl.toHttpUrl(), 2, 1) - .build() + private val preferences: SharedPreferences = + Injekt.get().getSharedPreferences("source_$id", 0x0000) + + override val client by lazy { + network.cloudflareClient.newBuilder() + .rateLimitHost(fetchedDomainUrl.toHttpUrl(), 1, 2) + .rateLimitHost(apiBaseUrl.toHttpUrl(), 2, 1) + .build() + } private val json: Json by injectLazy() @@ -268,4 +309,66 @@ class OlympusScanlation : HttpSource() { } private enum class FiltersState { NOT_FETCHED, FETCHING, FETCHED } + + override fun setupPreferenceScreen(screen: PreferenceScreen) { + CheckBoxPreference(screen.context).apply { + key = FETCH_DOMAIN_PREF + title = FETCH_DOMAIN_PREF_TITLE + summary = FETCH_DOMAIN_PREF_SUMMARY + setDefaultValue(FETCH_DOMAIN_PREF_DEFAULT) + }.also { screen.addPreference(it) } + + EditTextPreference(screen.context).apply { + key = BASE_URL_PREF + title = BASE_URL_PREF_TITLE + summary = BASE_URL_PREF_SUMMARY + dialogTitle = BASE_URL_PREF_TITLE + dialogMessage = "URL por defecto:\n$defaultBaseUrl" + setDefaultValue(defaultBaseUrl) + setOnPreferenceChangeListener { _, _ -> + Toast.makeText(screen.context, RESTART_APP_MESSAGE, Toast.LENGTH_LONG).show() + true + } + }.also { screen.addPreference(it) } + } + + private var _cachedBaseUrl: String? = null + private var SharedPreferences.prefBaseUrl: String + get() { + if (_cachedBaseUrl == null) { + _cachedBaseUrl = getString(BASE_URL_PREF, defaultBaseUrl)!! + } + return _cachedBaseUrl!! + } + set(value) { + _cachedBaseUrl = value + edit().putString(BASE_URL_PREF, value).apply() + } + + private fun SharedPreferences.fetchDomainPref() = getBoolean(FETCH_DOMAIN_PREF, FETCH_DOMAIN_PREF_DEFAULT) + + companion object { + + private const val BASE_URL_PREF = "overrideBaseUrl" + private const val BASE_URL_PREF_TITLE = "Editar URL de la fuente" + private const val BASE_URL_PREF_SUMMARY = "Para uso temporal, si la extensión se actualiza se perderá el cambio." + private const val DEFAULT_BASE_URL_PREF = "defaultBaseUrl" + private const val RESTART_APP_MESSAGE = "Reinicie la aplicación para aplicar los cambios" + + private const val FETCH_DOMAIN_PREF = "fetchDomain" + private const val FETCH_DOMAIN_PREF_TITLE = "Buscar dominio automáticamente" + private const val FETCH_DOMAIN_PREF_SUMMARY = "Intenta buscar el dominio automáticamente al abrir la fuente." + private const val FETCH_DOMAIN_PREF_DEFAULT = true + } + + init { + preferences.getString(DEFAULT_BASE_URL_PREF, null).let { domain -> + if (domain != defaultBaseUrl) { + preferences.edit() + .putString(BASE_URL_PREF, defaultBaseUrl) + .putString(DEFAULT_BASE_URL_PREF, defaultBaseUrl) + .apply() + } + } + } }