Asura Scans: Fix baseUrl and add permanent url pref (#10223)

This commit is contained in:
FourTOne5 2021-12-25 21:51:09 +06:00 committed by GitHub
parent a478a446a3
commit 4bce404938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 151 additions and 21 deletions

View File

@ -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<Application>().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<MangasPage> {
return super.fetchPopularManga(page).tempUrlToPermIfNeeded()
}
override fun fetchLatestUpdates(page: Int): Observable<MangasPage> {
return super.fetchLatestUpdates(page).tempUrlToPermIfNeeded()
}
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> {
return super.fetchSearchManga(page, query, filters).tempUrlToPermIfNeeded()
}
private fun Observable<MangasPage>.tempUrlToPermIfNeeded(): Observable<MangasPage> {
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<List<SChapter>> {
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+-""")
}
}

View File

@ -1,15 +1,11 @@
package eu.kanade.tachiyomi.extension.all.asurascans 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.SourceFactory
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import okhttp3.OkHttpClient
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit
class AsuraScansFactory : SourceFactory { class AsuraScansFactory : SourceFactory {
override fun createSources() = listOf( override fun createSources() = listOf(
@ -18,21 +14,7 @@ class AsuraScansFactory : SourceFactory {
) )
} }
abstract class AsuraScans( class AsuraScansEn : AsuraScans("https://asurascans.com", "en", SimpleDateFormat("MMM d, yyyy", Locale.US)) {
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)) {
override val pageSelector = "div.rdminimal > p > img" override val pageSelector = "div.rdminimal > p > img"
// Skip scriptPages // 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 { override fun mangaDetailsParse(document: Document): SManga {
return SManga.create().apply { return SManga.create().apply {
document.select("div.bigcontent").firstOrNull()?.let { infoElement -> document.select("div.bigcontent").firstOrNull()?.let { infoElement ->

View File

@ -13,7 +13,7 @@ class WPMangaStreamGenerator : ThemeSourceGenerator {
override val baseVersionCode: Int = 12 override val baseVersionCode: Int = 12
override val sources = listOf( 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("Infernal Void Scans", "https://infernalvoidscans.com", "en", overrideVersionCode = 2),
SingleLang("KlanKomik", "https://klankomik.com", "id", overrideVersionCode = 1), SingleLang("KlanKomik", "https://klankomik.com", "id", overrideVersionCode = 1),
SingleLang("Kombatch", "https://kombatch.com", "id"), SingleLang("Kombatch", "https://kombatch.com", "id"),