From ccbd81a55cbf8f30b73358b95b41eb001e72351a Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 1 Apr 2023 21:02:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/UpdateTest.kt | 51 +++++++++++++++++++ .../io/legado/app/help/AppUpdateGitHub.kt | 11 ++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 app/src/androidTest/java/io/legado/app/UpdateTest.kt diff --git a/app/src/androidTest/java/io/legado/app/UpdateTest.kt b/app/src/androidTest/java/io/legado/app/UpdateTest.kt new file mode 100644 index 000000000..60d3163c3 --- /dev/null +++ b/app/src/androidTest/java/io/legado/app/UpdateTest.kt @@ -0,0 +1,51 @@ +package io.legado.app + +import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.http.okHttpClient +import io.legado.app.utils.channel +import io.legado.app.utils.jsonPath +import okhttp3.Request +import org.junit.Test +import splitties.init.appCtx + +class UpdateTest { + + private val lastReleaseUrl = "https://api.github.com/repos/gedoor/legado/releases/latest" + + @Test + fun updateApp() { + val body = okHttpClient.newCall(Request.Builder().url(lastReleaseUrl).build()).execute() + .body!!.string() + val rootDoc = jsonPath.parse(body) + val downloadUrl = + rootDoc.read>("\$.assets[?(@.name =~ /legado_app_.*?apk\$/)].browser_download_url") + print(downloadUrl) + } + + @Test + fun updateLollipop() { + val body = okHttpClient.newCall(Request.Builder().url(lastReleaseUrl).build()).execute() + .body!!.string() + val rootDoc = jsonPath.parse(body) + val downloadUrl = + rootDoc.read>("\$.assets[?(@.name =~ /legado_lollipop_.*?apk\$/)].browser_download_url") + print(downloadUrl) + } + + @Test + fun updateChannel() { + val body = okHttpClient.newCall(Request.Builder().url(lastReleaseUrl).build()).execute() + .body!!.string() + val rootDoc = jsonPath.parse(body) + val path = "\$.assets[?(@.name =~ /legado_${appCtx.channel}_.*?apk\$/)]" + val downloadUrl = rootDoc.read>("${path}.browser_download_url") + .firstOrNull() + ?: throw NoStackTraceException("获取新版本出错") + val fileName = rootDoc.read>("${path}.name") + .firstOrNull() + ?: throw NoStackTraceException("获取新版本出错") + print(downloadUrl) + print(fileName) + } + +} \ No newline at end of file diff --git a/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt b/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt index a7d570863..4ffd03804 100644 --- a/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt +++ b/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt @@ -6,13 +6,15 @@ import io.legado.app.exception.NoStackTraceException import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.okHttpClient +import io.legado.app.utils.channel import io.legado.app.utils.jsonPath import io.legado.app.utils.readString import kotlinx.coroutines.CoroutineScope +import splitties.init.appCtx @Keep @Suppress("unused") -object AppUpdateGitHub: AppUpdate.AppUpdateInterface { +object AppUpdateGitHub : AppUpdate.AppUpdateInterface { override fun check( scope: CoroutineScope, @@ -31,9 +33,12 @@ object AppUpdateGitHub: AppUpdate.AppUpdateInterface { if (tagName > AppConst.appInfo.versionName) { val updateBody = rootDoc.readString("$.body") ?: throw NoStackTraceException("获取新版本出错") - val downloadUrl = rootDoc.readString("$.assets[0].browser_download_url") + val path = "\$.assets[?(@.name =~ /legado_${appCtx.channel}_.*?apk\$/)]" + val downloadUrl = rootDoc.read>("${path}.browser_download_url") + .firstOrNull() ?: throw NoStackTraceException("获取新版本出错") - val fileName = rootDoc.readString("$.assets[0].name") + val fileName = rootDoc.read>("${path}.name") + .firstOrNull() ?: throw NoStackTraceException("获取新版本出错") return@async AppUpdate.UpdateInfo(tagName, updateBody, downloadUrl, fileName) } else {