Make MangaDex load images more consistently (#8102)

* Make fetching the md@h url less likely to break

* import TimeUnit
This commit is contained in:
Jared Irwin 2021-07-13 16:35:35 -07:00 committed by GitHub
parent 695cdcb57e
commit 955c0891f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'MangaDex' extName = 'MangaDex'
pkgNameSuffix = 'all.mangadex' pkgNameSuffix = 'all.mangadex'
extClass = '.MangaDexFactory' extClass = '.MangaDexFactory'
extVersionCode = 123 extVersionCode = 124
libVersion = '1.2' libVersion = '1.2'
containsNsfw = true containsNsfw = true
} }

View File

@ -19,6 +19,7 @@ import okhttp3.Request
import org.jsoup.parser.Parser import org.jsoup.parser.Parser
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit
class MangaDexHelper() { class MangaDexHelper() {
@ -106,6 +107,12 @@ class MangaDexHelper() {
// chapter url where we get the token, last request time // chapter url where we get the token, last request time
private val tokenTracker = hashMapOf<String, Long>() private val tokenTracker = hashMapOf<String, Long>()
companion object {
val USE_CACHE = CacheControl.Builder()
.maxStale(Integer.MAX_VALUE, TimeUnit.SECONDS)
.build()
}
// Check the token map to see if the md@home host is still valid // Check the token map to see if the md@home host is still valid
fun getValidImageUrlForPage(page: Page, headers: Headers, client: OkHttpClient): Request { fun getValidImageUrlForPage(page: Page, headers: Headers, client: OkHttpClient): Request {
val data = page.url.split(",") val data = page.url.split(",")
@ -123,7 +130,7 @@ class MangaDexHelper() {
) { ) {
CacheControl.FORCE_NETWORK CacheControl.FORCE_NETWORK
} else { } else {
CacheControl.FORCE_CACHE USE_CACHE
} }
getMdAtHomeUrl(tokenRequestUrl, client, headers, cacheControl) getMdAtHomeUrl(tokenRequestUrl, client, headers, cacheControl)
} }
@ -145,6 +152,14 @@ class MangaDexHelper() {
} }
val response = val response =
client.newCall(GET(tokenRequestUrl, headers, cacheControl)).execute() client.newCall(GET(tokenRequestUrl, headers, cacheControl)).execute()
// This check is for the error that causes pages to fail to load.
// It should never be entered, but in case it is, we retry the request.
if (response.code == 504) {
Log.wtf("MangaDex", "Failed to read cache for \"$tokenRequestUrl\"")
return getMdAtHomeUrl(tokenRequestUrl, client, headers, CacheControl.FORCE_NETWORK)
}
return json.decodeFromString<AtHomeDto>(response.body!!.string()).baseUrl return json.decodeFromString<AtHomeDto>(response.body!!.string()).baseUrl
} }