From 3f7db8be7c686d8cbf2a4e846312333e97f2da64 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 20 Nov 2022 23:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=A2=9E=E5=8A=A0=E7=89=B9?= =?UTF-8?q?=E5=AE=9A=E6=A0=87=E8=AF=86=E4=BD=BF=E7=94=A8=E6=97=A0UA?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=EF=BC=8C=E9=80=82=E7=94=A8=E4=BA=8E=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E5=AF=BC=E5=85=A5=E4=B9=A6=E6=BA=90=E7=AD=89=20?= =?UTF-8?q?=E7=94=A8=E6=B3=95=EF=BC=9Ahttp://xxx.xxx/xxxx#requestWithoutUA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../association/ImportBookSourceViewModel.kt | 8 ++++++- .../ui/association/ImportHttpTtsViewModel.kt | 8 ++++++- .../association/ImportReplaceRuleViewModel.kt | 8 ++++++- .../association/ImportRssSourceViewModel.kt | 8 ++++++- .../ui/association/ImportThemeViewModel.kt | 8 ++++++- .../association/ImportTxtTocRuleViewModel.kt | 8 ++++++- .../ui/association/OnLineImportViewModel.kt | 22 ++++++++++++++++--- 7 files changed, 61 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 29f457496..12cb43594 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData import com.jayway.jsonpath.JsonPath import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource @@ -128,7 +129,12 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { private suspend fun importSourceUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.byteStream().let { allSources.addAll(BookSource.fromJsonArray(it).getOrThrow()) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt index 52bdd597c..05f21811c 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.HttpTTS import io.legado.app.exception.NoStackTraceException @@ -89,7 +90,12 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) { private suspend fun importSourceUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.text().let { importSourceAwait(it) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt index a4e050f22..aabdd09b4 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.association import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.ReplaceRule @@ -111,7 +112,12 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { private suspend fun importUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.text("utf-8").let { importAwait(it) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 73cd1314c..ea45ad928 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData import com.jayway.jsonpath.JsonPath import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.RssSource @@ -122,7 +123,12 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { private suspend fun importSourceUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.byteStream().let { body -> val items: List> = jsonPath.parse(body).read("$") for (item in items) { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportThemeViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportThemeViewModel.kt index a611fe8a9..a2d23156c 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportThemeViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportThemeViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.exception.NoStackTraceException import io.legado.app.help.config.ThemeConfig import io.legado.app.help.http.newCallResponseBody @@ -84,7 +85,12 @@ class ImportThemeViewModel(app: Application) : BaseViewModel(app) { private suspend fun importSourceUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.text().let { importSourceAwait(it) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportTxtTocRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportTxtTocRuleViewModel.kt index 938373d6d..3332ddefc 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportTxtTocRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportTxtTocRuleViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.TxtTocRule import io.legado.app.exception.NoStackTraceException @@ -87,7 +88,12 @@ class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) { private suspend fun importSourceUrl(url: String) { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.text().let { importSourceAwait(it) } diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index ee0591db5..4292815d2 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.association import android.app.Application import androidx.core.net.toUri import io.legado.app.R +import io.legado.app.constant.AppConst import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.okHttpClient @@ -17,7 +18,12 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { fun getText(url: String, success: (text: String) -> Unit) { execute { okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.text("utf-8") }.onSuccess { success.invoke(it) @@ -32,7 +38,12 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { execute { @Suppress("BlockingMethodInNonBlockingContext") okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } }.bytes() }.onSuccess { success.invoke(it) @@ -67,7 +78,12 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { fun determineType(url: String, finally: (title: String, msg: String) -> Unit) { execute { val rs = okHttpClient.newCallResponseBody { - url(url) + if (url.endsWith("#requestWithoutUA")) { + url(url.substringBeforeLast("#requestWithoutUA")) + header(AppConst.UA_NAME, "null") + } else { + url(url) + } } when (rs.contentType()) { "application/zip".toMediaType(),