Zerobyw: update domain URL source (#16119)
This commit is contained in:
parent
992a989ee3
commit
3f1137cba5
|
@ -1,12 +1,11 @@
|
||||||
apply plugin: 'com.android.application'
|
apply plugin: 'com.android.application'
|
||||||
apply plugin: 'kotlin-android'
|
apply plugin: 'kotlin-android'
|
||||||
apply plugin: 'kotlinx-serialization'
|
|
||||||
|
|
||||||
ext {
|
ext {
|
||||||
extName = 'Zerobyw'
|
extName = 'Zerobyw'
|
||||||
pkgNameSuffix = 'zh.zerobyw'
|
pkgNameSuffix = 'zh.zerobyw'
|
||||||
extClass = '.Zerobyw'
|
extClass = '.Zerobyw'
|
||||||
extVersionCode = 14
|
extVersionCode = 15
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$rootDir/common.gradle"
|
apply from: "$rootDir/common.gradle"
|
||||||
|
|
|
@ -5,8 +5,6 @@ import android.content.SharedPreferences
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
import eu.kanade.tachiyomi.network.GET
|
import eu.kanade.tachiyomi.network.GET
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
@ -17,7 +15,7 @@ private const val DEFAULT_BASE_URL = "http://www.zerobyw4090.com"
|
||||||
|
|
||||||
private const val BASE_URL_PREF = "ZEROBYW_BASEURL"
|
private const val BASE_URL_PREF = "ZEROBYW_BASEURL"
|
||||||
private const val DEFAULT_BASE_URL_PREF = "defaultBaseUrl"
|
private const val DEFAULT_BASE_URL_PREF = "defaultBaseUrl"
|
||||||
private const val JSON_URL = "https://cdn.jsdelivr.net/gh/zerozzz123456/1/url.json"
|
private const val LATEST_DOMAIN_URL = "https://stevenyomi.github.io/source-domains/zerobyw.txt"
|
||||||
|
|
||||||
var SharedPreferences.baseUrl: String
|
var SharedPreferences.baseUrl: String
|
||||||
get() = getString(BASE_URL_PREF, DEFAULT_BASE_URL)!!
|
get() = getString(BASE_URL_PREF, DEFAULT_BASE_URL)!!
|
||||||
|
@ -36,7 +34,7 @@ fun getBaseUrlPreference(context: Context) = EditTextPreference(context).apply {
|
||||||
key = BASE_URL_PREF
|
key = BASE_URL_PREF
|
||||||
title = "网址"
|
title = "网址"
|
||||||
summary = "正常情况下会自动更新。" +
|
summary = "正常情况下会自动更新。" +
|
||||||
"如果出现错误,请在 GitHub 上报告,并且可以在 $JSON_URL 找到最新网址手动填写。" +
|
"如果出现错误,请在 GitHub 上报告,并且可以在 GitHub 仓库 zerozzz123456/1 找到最新网址手动填写。" +
|
||||||
"填写时按照 $DEFAULT_BASE_URL 格式。"
|
"填写时按照 $DEFAULT_BASE_URL 格式。"
|
||||||
setDefaultValue(DEFAULT_BASE_URL)
|
setDefaultValue(DEFAULT_BASE_URL)
|
||||||
|
|
||||||
|
@ -52,25 +50,16 @@ fun getBaseUrlPreference(context: Context) = EditTextPreference(context).apply {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ciGetUrl(client: OkHttpClient): String {
|
fun ciGetUrl(client: OkHttpClient): String {
|
||||||
println("[Zerobyw] CI detected, getting latest URL...")
|
|
||||||
return try {
|
return try {
|
||||||
val response = client.newCall(GET(JSON_URL)).execute()
|
val response = client.newCall(GET(LATEST_DOMAIN_URL)).execute()
|
||||||
parseJson(response).also { println("[Zerobyw] Latest URL is $it") }
|
response.body.string()
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
println("[Zerobyw] Failed to fetch latest URL")
|
println("::error ::Zerobyw: Failed to fetch latest URL")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
DEFAULT_BASE_URL
|
DEFAULT_BASE_URL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseJson(response: Response): String {
|
|
||||||
val string = response.body.string()
|
|
||||||
val json: HashMap<String, String> = Json.decodeFromString(string)
|
|
||||||
val newUrl = json["url"]!!.trim()
|
|
||||||
checkBaseUrl(newUrl)
|
|
||||||
return newUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun checkBaseUrl(url: String) {
|
private fun checkBaseUrl(url: String) {
|
||||||
require(url == url.trim() && !url.endsWith('/'))
|
require(url == url.trim() && !url.endsWith('/'))
|
||||||
val pathSegments = url.toHttpUrl().pathSegments
|
val pathSegments = url.toHttpUrl().pathSegments
|
||||||
|
@ -97,11 +86,10 @@ class UpdateUrlInterceptor(
|
||||||
}
|
}
|
||||||
|
|
||||||
val newUrl = try {
|
val newUrl = try {
|
||||||
val response = chain.proceed(GET(JSON_URL))
|
val newUrl = chain.proceed(GET(LATEST_DOMAIN_URL)).body.string()
|
||||||
val newUrl = parseJson(response)
|
|
||||||
require(newUrl != baseUrl)
|
require(newUrl != baseUrl)
|
||||||
newUrl
|
newUrl
|
||||||
} catch (e: Throwable) {
|
} catch (_: Throwable) {
|
||||||
return failedResult.getOrThrow()
|
return failedResult.getOrThrow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ class Zerobyw : ParsedHttpSource(), ConfigurableSource {
|
||||||
override val supportsLatest: Boolean get() = false
|
override val supportsLatest: Boolean get() = false
|
||||||
private val preferences: SharedPreferences =
|
private val preferences: SharedPreferences =
|
||||||
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
||||||
|
.clearOldBaseUrl()
|
||||||
|
|
||||||
override val client = network.cloudflareClient.newBuilder()
|
override val client = network.cloudflareClient.newBuilder()
|
||||||
.addInterceptor(UpdateUrlInterceptor(preferences))
|
.addInterceptor(UpdateUrlInterceptor(preferences))
|
||||||
|
|
Loading…
Reference in New Issue