MD: Refresh URL suffix when required (#18459)

This commit is contained in:
Vetle Ledaal 2023-10-09 21:30:50 +00:00 committed by GitHub
parent 6e42bc0e51
commit 63fd5dd57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 9 deletions

View File

@ -0,0 +1,40 @@
# 1.4.6
### Fix
* Refresh URL suffix when required
# 1.4.5
### Features
* Dynamically update URL suffix
# 1.4.4
### Features
* Filters
### Fix
* Update base URL
# 1.4.3
### Fix
* Update selector
# 1.4.2
### Fix
* Fetch all chapter images
* Use GET on search, same as site
# 1.4.1
### Features
* First version

View File

@ -5,7 +5,7 @@ ext {
extName = 'Manga Demon'
pkgNameSuffix = 'en.mangademon'
extClass = '.MangaDemon'
extVersionCode = 5
extVersionCode = 6
isNsfw = false
}

View File

@ -18,6 +18,7 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import rx.Observable
import java.io.IOException
import java.net.URLEncoder
import java.text.SimpleDateFormat
import java.util.Locale
@ -34,16 +35,28 @@ class MangaDemon : ParsedHttpSource() {
.addInterceptor(::dynamicUrlInterceptor)
.build()
private var dynamicUrlSuffix: String? = null
// Cache suffix
private var dynamicUrlSuffix = ""
private var dynamicUrlSuffixUpdated: Long = 0
private val dynamicUrlSuffixValidity: Long = 10 * 60 // 10 minutes
private fun dynamicUrlInterceptor(chain: Interceptor.Chain): Response {
val request = chain.request()
val timeNow = System.currentTimeMillis() / 1000
// Check if request requires an up-to-date suffix
if (request.url.pathSegments[0] == "manga" && dynamicUrlSuffix != null) {
if (request.url.pathSegments[0] == "manga") {
// Force update suffix if required
if (timeNow - dynamicUrlSuffixUpdated > dynamicUrlSuffixValidity) {
client.newCall(GET(baseUrl)).execute()
if (timeNow - dynamicUrlSuffixUpdated > dynamicUrlSuffixValidity) {
throw IOException("Failed to update dynamic url suffix")
}
}
val newPath = request.url
.encodedPath
.replaceAfterLast("-", dynamicUrlSuffix!!)
.replaceAfterLast("-", dynamicUrlSuffix)
val newUrl = request.url.newBuilder()
.encodedPath(newPath)
@ -56,12 +69,8 @@ class MangaDemon : ParsedHttpSource() {
return chain.proceed(newRequest)
}
// Always update suffix
val response = chain.proceed(request)
if (dynamicUrlSuffix != null) {
return response
}
// Don't have suffix, get it from the page
val document = Jsoup.parse(
response.peekBody(Long.MAX_VALUE).string(),
request.url.toString(),
@ -77,6 +86,7 @@ class MangaDemon : ParsedHttpSource() {
if (suffix != null) {
dynamicUrlSuffix = suffix
dynamicUrlSuffixUpdated = timeNow
}
return response