Update networking functions in Guya. (#6101)

This commit is contained in:
funkyhippo 2021-03-08 10:10:19 -08:00 committed by GitHub
parent 743d41a018
commit 7d4e7437bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 41 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'Guya' extName = 'Guya'
pkgNameSuffix = "en.guya" pkgNameSuffix = "en.guya"
extClass = '.Guya' extClass = '.Guya'
extVersionCode = 14 extVersionCode = 15
libVersion = '1.2' libVersion = '1.2'
} }

View File

@ -7,6 +7,7 @@ import android.support.v7.preference.ListPreference
import android.support.v7.preference.PreferenceScreen import android.support.v7.preference.PreferenceScreen
import eu.kanade.tachiyomi.extension.BuildConfig import eu.kanade.tachiyomi.extension.BuildConfig
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.asObservable
import eu.kanade.tachiyomi.network.asObservableSuccess import eu.kanade.tachiyomi.network.asObservableSuccess
import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.FilterList
@ -15,20 +16,16 @@ import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Headers import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import rx.Observable import rx.Observable
import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.io.IOException
import java.util.HashMap import java.util.HashMap
import java.util.concurrent.TimeUnit
open class Guya : ConfigurableSource, HttpSource() { open class Guya : ConfigurableSource, HttpSource() {
@ -72,14 +69,14 @@ open class Guya : ConfigurableSource, HttpSource() {
override fun fetchMangaDetails(manga: SManga): Observable<SManga> { override fun fetchMangaDetails(manga: SManga): Observable<SManga> {
return when { return when {
manga.url.startsWith(PROXY_PREFIX) -> { manga.url.startsWith(PROXY_PREFIX) -> {
clientBuilder().newCall(proxyChapterListRequest(manga)) client.newCall(proxyChapterListRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
proxyMangaDetailsParse(response, manga) proxyMangaDetailsParse(response, manga)
} }
} }
else -> { else -> {
clientBuilder().newCall(chapterListRequest(manga)) client.newCall(chapterListRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
mangaDetailsParse(response, manga) mangaDetailsParse(response, manga)
@ -109,14 +106,14 @@ open class Guya : ConfigurableSource, HttpSource() {
override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> { override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> {
return when { return when {
manga.url.startsWith(PROXY_PREFIX) -> { manga.url.startsWith(PROXY_PREFIX) -> {
clientBuilder().newCall(proxyChapterListRequest(manga)) client.newCall(proxyChapterListRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
proxyChapterListParse(response, manga) proxyChapterListParse(response, manga)
} }
} }
else -> { else -> {
clientBuilder().newCall(chapterListRequest(manga)) client.newCall(chapterListRequest(manga))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
chapterListParse(response, manga) chapterListParse(response, manga)
@ -144,14 +141,14 @@ open class Guya : ConfigurableSource, HttpSource() {
override fun fetchPageList(chapter: SChapter): Observable<List<Page>> { override fun fetchPageList(chapter: SChapter): Observable<List<Page>> {
return when { return when {
chapter.url.startsWith(PROXY_PREFIX) -> { chapter.url.startsWith(PROXY_PREFIX) -> {
clientBuilder().newCall(proxyPageListRequest(chapter)) client.newCall(proxyPageListRequest(chapter))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
proxyPageListParse(response, chapter) proxyPageListParse(response, chapter)
} }
} }
else -> { else -> {
clientBuilder().newCall(pageListRequest(chapter)) client.newCall(pageListRequest(chapter))
.asObservableSuccess() .asObservableSuccess()
.map { response -> .map { response ->
pageListParse(response, chapter) pageListParse(response, chapter)
@ -539,17 +536,13 @@ open class Guya : ConfigurableSource, HttpSource() {
return "$baseUrl/media/manga/$slug/chapters/$folder/$groupId/$filename" return "$baseUrl/media/manga/$slug/chapters/$folder/$groupId/$filename"
} }
private fun clientBuilder(): OkHttpClient = network.cloudflareClient.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()!!
inner class ScanlatorStore { inner class ScanlatorStore {
private val scanlatorMap = HashMap<String, String>() private val scanlatorMap = HashMap<String, String>()
private var polling = false private val TOTAL_RETRIES = 10
private var retryCount = 0
init { init {
update() update(false)
} }
fun getKeyFromValue(value: String): String { fun getKeyFromValue(value: String): String {
@ -575,29 +568,39 @@ open class Guya : ConfigurableSource, HttpSource() {
return scanlatorMap.keys return scanlatorMap.keys
} }
private fun update() { private fun onResponse(response: Response) {
if (scanlatorMap.isEmpty() && !polling) { if (!response.isSuccessful) {
polling = true retryCount++
clientBuilder().newCall(GET(scanlatorCacheUrl, headers)).enqueue( } else {
object : Callback {
override fun onResponse(call: Call, response: Response) {
try {
val json = JSONObject(response.body()!!.string()) val json = JSONObject(response.body()!!.string())
val iter = json.keys() val iter = json.keys()
while (iter.hasNext()) { while (iter.hasNext()) {
val scanId = iter.next() val scanId = iter.next()
scanlatorMap[scanId] = json.getString(scanId) scanlatorMap[scanId] = json.getString(scanId)
} }
}
}
private fun onError(error: Throwable) {
// Do nothing for now
}
private fun update(blocking: Boolean = true) {
if (scanlatorMap.isEmpty() && retryCount < TOTAL_RETRIES) {
try {
val call = client.newCall(GET(scanlatorCacheUrl, headers))
.asObservable()
if (blocking) {
call.toBlocking()
.subscribe({ res -> onResponse(res) }, { err -> onError(err) })
} else {
call.subscribeOn(Schedulers.io())
.subscribe({ res -> onResponse(res) }, { err -> onError(err) })
}
} catch (e: Exception) { } catch (e: Exception) {
// ScanlatorStore will fall back to using keys until update() succeeds // Prevent the extension from failing to load
} }
polling = false
}
override fun onFailure(call: Call, e: IOException) {
polling = false
}
}
)
} }
} }
} }