mirror of
https://github.com/keiyoushi/extensions-source.git
synced 2024-11-25 11:42:47 +01:00
zaimanhua: revalidate token if error code not 0 (#5785)
This commit is contained in:
parent
c37b370553
commit
5d2badc707
@ -1,7 +1,7 @@
|
||||
ext {
|
||||
extName = 'Zaimanhua'
|
||||
extClass = '.Zaimanhua'
|
||||
extVersionCode = 2
|
||||
extVersionCode = 3
|
||||
}
|
||||
|
||||
apply from: "$rootDir/common.gradle"
|
||||
|
@ -4,6 +4,7 @@ import eu.kanade.tachiyomi.source.model.SManga
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
val json: Json by injectLazy()
|
||||
@ -12,6 +13,10 @@ inline fun <reified T> Response.parseAs(): T {
|
||||
return json.decodeFromString(body.string())
|
||||
}
|
||||
|
||||
inline fun <reified T> ResponseBody.parseAs(): T {
|
||||
return json.decodeFromString(this.string())
|
||||
}
|
||||
|
||||
fun parseStatus(status: String): Int = when (status) {
|
||||
"连载中" -> SManga.ONGOING
|
||||
"已完结" -> SManga.COMPLETED
|
||||
|
@ -44,6 +44,7 @@ class Zaimanhua : HttpSource(), ConfigurableSource {
|
||||
override val baseUrl = "https://manhua.zaimanhua.com"
|
||||
private val apiUrl = "https://v4api.zaimanhua.com/app/v1"
|
||||
private val accountApiUrl = "https://account-api.zaimanhua.com/v1"
|
||||
private val checkTokenRegex = Regex("""$apiUrl/comic/(detail|chapter)""")
|
||||
|
||||
private val json by injectLazy<Json>()
|
||||
|
||||
@ -58,12 +59,18 @@ class Zaimanhua : HttpSource(), ConfigurableSource {
|
||||
|
||||
private fun authIntercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
if (request.url.host != "v4api.zaimanhua.com" || !request.headers["authorization"].isNullOrBlank()) {
|
||||
if (request.url.host != "v4api.zaimanhua.com" ||
|
||||
(!request.headers["authorization"].isNullOrBlank() && !request.url.toString().contains(checkTokenRegex))
|
||||
) {
|
||||
return chain.proceed(request)
|
||||
}
|
||||
|
||||
val response = chain.proceed(request)
|
||||
if (!request.headers["authorization"].isNullOrBlank() && response.peekBody(Long.MAX_VALUE).parseAs<SimpleResponseDto>().errno == 0) {
|
||||
return response
|
||||
}
|
||||
var token: String = preferences.getString("TOKEN", "")!!
|
||||
if (token.isBlank() || !isValid(token)) {
|
||||
if (!isValid(token)) {
|
||||
val username = preferences.getString("USERNAME", "")!!
|
||||
val password = preferences.getString("PASSWORD", "")!!
|
||||
token = getToken(username, password)
|
||||
@ -71,12 +78,15 @@ class Zaimanhua : HttpSource(), ConfigurableSource {
|
||||
preferences.edit().putString("TOKEN", "").apply()
|
||||
preferences.edit().putString("USERNAME", "").apply()
|
||||
preferences.edit().putString("PASSWORD", "").apply()
|
||||
return chain.proceed(request)
|
||||
return response
|
||||
} else {
|
||||
preferences.edit().putString("TOKEN", token).apply()
|
||||
apiHeaders = apiHeaders.newBuilder().setToken(token).build()
|
||||
}
|
||||
} else if (!request.headers["authorization"].isNullOrBlank() && request.headers["authorization"] == "Bearer $token") {
|
||||
return response
|
||||
}
|
||||
|
||||
val authRequest = request.newBuilder().apply {
|
||||
header("authorization", "Bearer $token")
|
||||
}.build()
|
||||
@ -90,6 +100,7 @@ class Zaimanhua : HttpSource(), ConfigurableSource {
|
||||
private var apiHeaders = headersBuilder().setToken(preferences.getString("TOKEN", "")!!).build()
|
||||
|
||||
private fun isValid(token: String): Boolean {
|
||||
if (token.isBlank()) return false
|
||||
val response = client.newCall(
|
||||
GET(
|
||||
"$accountApiUrl/userInfo/get",
|
||||
|
@ -140,6 +140,11 @@ class DataWrapperDto<T>(
|
||||
val data: T?,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class SimpleResponseDto(
|
||||
val errno: Int = 0,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class ResponseDto<T>(
|
||||
val errno: Int = 0,
|
||||
|
Loading…
Reference in New Issue
Block a user