diff --git a/multisrc/overrides/wpmangastream/asurascans/src/AsuraScans.kt b/multisrc/overrides/wpmangastream/asurascans/src/AsuraScans.kt new file mode 100644 index 000000000..302e5eac5 --- /dev/null +++ b/multisrc/overrides/wpmangastream/asurascans/src/AsuraScans.kt @@ -0,0 +1,148 @@ +package eu.kanade.tachiyomi.extension.all.asurascans + +import android.app.Application +import android.content.SharedPreferences +import androidx.preference.PreferenceScreen +import androidx.preference.SwitchPreferenceCompat +import eu.kanade.tachiyomi.lib.ratelimit.RateLimitInterceptor +import eu.kanade.tachiyomi.multisrc.wpmangastream.WPMangaStream +import eu.kanade.tachiyomi.source.ConfigurableSource +import eu.kanade.tachiyomi.source.model.FilterList +import eu.kanade.tachiyomi.source.model.MangasPage +import eu.kanade.tachiyomi.source.model.SChapter +import eu.kanade.tachiyomi.source.model.SManga +import okhttp3.OkHttpClient +import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get +import java.text.SimpleDateFormat +import java.util.concurrent.TimeUnit + +open class AsuraScans( + override val baseUrl: String, + override val lang: String, + dateFormat: SimpleDateFormat +) : WPMangaStream( + "Asura Scans", + baseUrl, + lang, + dateFormat +), + ConfigurableSource { + + private val preferences: SharedPreferences by lazy { + Injekt.get().getSharedPreferences("source_$id", 0x0000) + } + + override val client: OkHttpClient = network.cloudflareClient.newBuilder() + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .addInterceptor(RateLimitInterceptor(1, 3, TimeUnit.SECONDS)) + .build() + + override fun fetchPopularManga(page: Int): Observable { + return super.fetchPopularManga(page).tempUrlToPermIfNeeded() + } + + override fun fetchLatestUpdates(page: Int): Observable { + return super.fetchLatestUpdates(page).tempUrlToPermIfNeeded() + } + + override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable { + return super.fetchSearchManga(page, query, filters).tempUrlToPermIfNeeded() + } + + private fun Observable.tempUrlToPermIfNeeded(): Observable { + return this.map { mangasPage -> + MangasPage( + mangasPage.mangas.map { it.tempUrlToPermIfNeeded() }, + mangasPage.hasNextPage + ) + } + } + + private fun SManga.tempUrlToPermIfNeeded(): SManga { + val turnTempUrlToPerm = preferences.getBoolean(getPermanentMangaUrlPreferenceKey(), false) + if (!turnTempUrlToPerm) return this + + val sMangaTitleFirstWord = this.title.split(" ")[0] + if (!this.url.contains("/$sMangaTitleFirstWord", ignoreCase = true)) { + this.url = this.url.replaceFirst(TEMP_TO_PERM_URL_REGEX, "$1") + } + return this + } + + override fun fetchChapterList(manga: SManga): Observable> { + manga.tempUrlToPermIfNeeded() + return super.fetchChapterList(manga).map { sChapterList -> + sChapterList.map { it.tempUrlToPermIfNeeded(manga) } + } + } + + private fun SChapter.tempUrlToPermIfNeeded(manga: SManga): SChapter { + val turnTempUrlToPerm = preferences.getBoolean(getPermanentChapterUrlPreferenceKey(), false) + if (!turnTempUrlToPerm) return this + + val sChapterNameFirstWord = this.name.split(" ")[0] + val sMangaTitleFirstWord = manga.title.split(" ")[0] + if ( + !this.url.contains("/$sChapterNameFirstWord", ignoreCase = true) || + !this.url.contains("/$sMangaTitleFirstWord", ignoreCase = true) + ) { + this.url = this.url.replaceFirst(TEMP_TO_PERM_URL_REGEX, "$1") + } + return this + } + + override fun setupPreferenceScreen(screen: PreferenceScreen) { + val permanentMangaUrlPref = SwitchPreferenceCompat(screen.context).apply { + key = getPermanentMangaUrlPreferenceKey() + title = PREF_PERM_MANGA_URL_TITLE + summary = PREF_PERM_MANGA_URL_SUMMARY + setDefaultValue(true) + + setOnPreferenceChangeListener { _, newValue -> + val checkValue = newValue as Boolean + preferences.edit() + .putBoolean(getPermanentMangaUrlPreferenceKey(), checkValue) + .commit() + } + } + val permanentChapterUrlPref = SwitchPreferenceCompat(screen.context).apply { + key = getPermanentChapterUrlPreferenceKey() + title = PREF_PERM_CHAPTER_URL_TITLE + summary = PREF_PERM_CHAPTER_URL_SUMMARY + setDefaultValue(true) + + setOnPreferenceChangeListener { _, newValue -> + val checkValue = newValue as Boolean + preferences.edit() + .putBoolean(getPermanentChapterUrlPreferenceKey(), checkValue) + .commit() + } + } + screen.addPreference(permanentMangaUrlPref) + screen.addPreference(permanentChapterUrlPref) + } + + private fun getPermanentMangaUrlPreferenceKey(): String { + return PREF_PERM_MANGA_URL_KEY_PREFIX + lang + } + + private fun getPermanentChapterUrlPreferenceKey(): String { + return PREF_PERM_CHAPTER_URL_KEY_PREFIX + lang + } + // Permanent Url for Manga/Chapter End + + companion object { + private const val PREF_PERM_MANGA_URL_KEY_PREFIX = "pref_permanent_manga_url_" + private const val PREF_PERM_MANGA_URL_TITLE = "Permanent Manga URL" + private const val PREF_PERM_MANGA_URL_SUMMARY = "Turns all manga urls into permanent ones." + + private const val PREF_PERM_CHAPTER_URL_KEY_PREFIX = "pref_permanent_chapter_url" + private const val PREF_PERM_CHAPTER_URL_TITLE = "Permanent Chapter URL" + private const val PREF_PERM_CHAPTER_URL_SUMMARY = "Turns all chapter urls into permanent one." + + private val TEMP_TO_PERM_URL_REGEX = Regex("""(/)\d+-""") + } +} diff --git a/multisrc/overrides/wpmangastream/asurascans/src/AsuraScansFactory.kt b/multisrc/overrides/wpmangastream/asurascans/src/AsuraScansFactory.kt index aec7c484f..f02b25767 100644 --- a/multisrc/overrides/wpmangastream/asurascans/src/AsuraScansFactory.kt +++ b/multisrc/overrides/wpmangastream/asurascans/src/AsuraScansFactory.kt @@ -1,15 +1,11 @@ package eu.kanade.tachiyomi.extension.all.asurascans -import eu.kanade.tachiyomi.lib.ratelimit.RateLimitInterceptor -import eu.kanade.tachiyomi.multisrc.wpmangastream.WPMangaStream import eu.kanade.tachiyomi.source.SourceFactory import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SManga -import okhttp3.OkHttpClient import org.jsoup.nodes.Document import java.text.SimpleDateFormat import java.util.Locale -import java.util.concurrent.TimeUnit class AsuraScansFactory : SourceFactory { override fun createSources() = listOf( @@ -18,21 +14,7 @@ class AsuraScansFactory : SourceFactory { ) } -abstract class AsuraScans( - override val baseUrl: String, - override val lang: String, - dateFormat: SimpleDateFormat -) : WPMangaStream("Asura Scans", baseUrl, lang, dateFormat) { - private val rateLimitInterceptor = RateLimitInterceptor(1, 3, TimeUnit.SECONDS) - - override val client: OkHttpClient = network.cloudflareClient.newBuilder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .addNetworkInterceptor(rateLimitInterceptor) - .build() -} - -class AsuraScansEn : AsuraScans("https://asurascans.com/", "en", SimpleDateFormat("MMM d, yyyy", Locale.US)) { +class AsuraScansEn : AsuraScans("https://asurascans.com", "en", SimpleDateFormat("MMM d, yyyy", Locale.US)) { override val pageSelector = "div.rdminimal > p > img" // Skip scriptPages @@ -43,7 +25,7 @@ class AsuraScansEn : AsuraScans("https://asurascans.com/", "en", SimpleDateForma } } -class AsuraScansTr : AsuraScans("https://tr.asurascans.com/", "tr", SimpleDateFormat("MMM d, yyyy", Locale("tr"))) { +class AsuraScansTr : AsuraScans("https://tr.asurascans.com", "tr", SimpleDateFormat("MMM d, yyyy", Locale("tr"))) { override fun mangaDetailsParse(document: Document): SManga { return SManga.create().apply { document.select("div.bigcontent").firstOrNull()?.let { infoElement -> diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/wpmangastream/WPMangaStreamGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/wpmangastream/WPMangaStreamGenerator.kt index 2fcb75112..70237fb0a 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/wpmangastream/WPMangaStreamGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/wpmangastream/WPMangaStreamGenerator.kt @@ -13,7 +13,7 @@ class WPMangaStreamGenerator : ThemeSourceGenerator { override val baseVersionCode: Int = 12 override val sources = listOf( - MultiLang("Asura Scans", "https://www.asurascans.com", listOf("en", "tr"), className = "AsuraScansFactory", pkgName = "asurascans", overrideVersionCode = 8), + MultiLang("Asura Scans", "https://www.asurascans.com", listOf("en", "tr"), className = "AsuraScansFactory", pkgName = "asurascans", overrideVersionCode = 9), SingleLang("Infernal Void Scans", "https://infernalvoidscans.com", "en", overrideVersionCode = 2), SingleLang("KlanKomik", "https://klankomik.com", "id", overrideVersionCode = 1), SingleLang("Kombatch", "https://kombatch.com", "id"),