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