通过增加特定标识使用无UA请求,适用于网络导入书源等

用法:http://xxx.xxx/xxxx#requestWithoutUA
pull/2605/head
kunfei 2 years ago
parent 181209ad59
commit 3f7db8be7c
  1. 8
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  2. 8
      app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt
  3. 8
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt
  4. 8
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt
  5. 8
      app/src/main/java/io/legado/app/ui/association/ImportThemeViewModel.kt
  6. 8
      app/src/main/java/io/legado/app/ui/association/ImportTxtTocRuleViewModel.kt
  7. 22
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.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())
}

@ -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)
}

@ -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)
}

@ -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<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) {

@ -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)
}

@ -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)
}

@ -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(),

Loading…
Cancel
Save