diff --git a/app/src/main/assets/defaultData/httpTTS.json b/app/src/main/assets/defaultData/httpTTS.json
index 29a8c7dc5..d361dda1b 100644
--- a/app/src/main/assets/defaultData/httpTTS.json
+++ b/app/src/main/assets/defaultData/httpTTS.json
@@ -12,6 +12,9 @@
{
"id": -29,
"name": "3.阿里云语音",
- "url": "/*播音人Aiting可改其他https://cdn.jsdelivr.net/gh/Celeter/build/.github/scripts/speaker.json,详见https://ai.aliyun.com/nls/tts*/;eval(''+java.ajax('https://cdn.jsdelivr.net/gh/Celeter/build/.github/scripts/ttsDemo.js'));ttsDemo(speakText,speakSpeed,'Aiting')"
+ "url": "https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/tts,{\"method\": \"POST\",\"body\", {\"appkey\":\"31f932fb\",\"text\":\"{{speakText}}\",\"format\":\"mp3\"}}",
+ "loginUrl": "",
+ "loginUi": "",
+ "loginCheckJs": ""
}
]
\ No newline at end of file
diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt
index 1eb3651bf..e5dd147e7 100644
--- a/app/src/main/java/io/legado/app/help/JsExtensions.kt
+++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt
@@ -376,7 +376,7 @@ interface JsExtensions {
fun getZipByteArrayContent(url: String, path: String): ByteArray? {
val bytes = if (url.startsWith("http://") || url.startsWith("https://")) {
runBlocking {
- return@runBlocking okHttpClient.newCall { url(url) }.bytes()
+ return@runBlocking okHttpClient.newCallResponseBody { url(url) }.bytes()
}
} else {
StringUtils.hexStringToByte(url)
@@ -421,7 +421,7 @@ interface JsExtensions {
str.isAbsUrl() -> runBlocking {
var x = CacheManager.getByteArray(key)
if (x == null) {
- x = okHttpClient.newCall { url(str) }.bytes()
+ x = okHttpClient.newCallResponseBody { url(str) }.bytes()
x.let {
CacheManager.put(key, it)
}
diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
index b46b30b5a..e3774b3f1 100644
--- a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
+++ b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
@@ -16,7 +16,26 @@ import java.nio.charset.Charset
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
-suspend fun OkHttpClient.newCall(
+suspend fun OkHttpClient.newCallResponse(
+ retry: Int = 0,
+ builder: Request.Builder.() -> Unit
+): Response {
+ return withContext(IO) {
+ val requestBuilder = Request.Builder()
+ requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
+ requestBuilder.apply(builder)
+ var response: Response? = null
+ for (i in 0..retry) {
+ response = this@newCallResponse.newCall(requestBuilder.build()).await()
+ if (response.isSuccessful) {
+ return@withContext response
+ }
+ }
+ return@withContext response!!
+ }
+}
+
+suspend fun OkHttpClient.newCallResponseBody(
retry: Int = 0,
builder: Request.Builder.() -> Unit
): ResponseBody {
@@ -26,7 +45,7 @@ suspend fun OkHttpClient.newCall(
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
- response = this@newCall.newCall(requestBuilder.build()).await()
+ response = this@newCallResponseBody.newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return@withContext response.body!!
}
diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
index 6d5beb185..d2d488841 100644
--- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
+++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
@@ -1,6 +1,6 @@
package io.legado.app.lib.webdav
-import io.legado.app.help.http.newCall
+import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.printOnDebug
@@ -92,7 +92,7 @@ class WebDav(urlStr: String) {
val auth = HttpAuth.auth
if (url != null && auth != null) {
return kotlin.runCatching {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
addHeader("Depth", "1")
@@ -152,7 +152,7 @@ class WebDav(urlStr: String) {
if (url != null && auth != null) {
//防止报错
return kotlin.runCatching {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
method("MKCOL", null)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
@@ -198,7 +198,7 @@ class WebDav(urlStr: String) {
val auth = HttpAuth.auth
if (url != null && auth != null) {
return kotlin.runCatching {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
put(fileBody)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
@@ -215,7 +215,7 @@ class WebDav(urlStr: String) {
val auth = HttpAuth.auth
if (url != null && auth != null) {
return kotlin.runCatching {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
put(fileBody)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
@@ -230,7 +230,7 @@ class WebDav(urlStr: String) {
val auth = HttpAuth.auth
if (url != null && auth != null) {
return kotlin.runCatching {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
}.byteStream()
diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
index c90c20715..1a5c85673 100644
--- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
+++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
@@ -16,6 +16,7 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.http.*
import io.legado.app.model.ConcurrentException
import io.legado.app.utils.*
+import okhttp3.Response
import java.net.URLEncoder
import java.util.*
import java.util.regex.Pattern
@@ -244,6 +245,12 @@ class AnalyzeUrl(
return SCRIPT_ENGINE.eval(jsStr, bindings)
}
+ fun upHeader() {
+ source?.getHeaderMap(true)?.let {
+ headerMap.putAll(it)
+ }
+ }
+
fun put(key: String, value: String): String {
chapter?.putVariable(key, value)
?: ruleData?.putVariable(key, value)
@@ -373,6 +380,29 @@ class AnalyzeUrl(
}
}
+ /**
+ * 访问网站,返回Response
+ */
+ suspend fun getResponse(): Response {
+ judgmentConcurrent()
+ setCookie(source?.getKey())
+ @Suppress("BlockingMethodInNonBlockingContext")
+ return getProxyClient(proxy).newCallResponse(retry) {
+ addHeaders(headerMap)
+ when (method) {
+ RequestMethod.POST -> {
+ url(urlNoQuery)
+ if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
+ postForm(fieldMap, true)
+ } else {
+ postJson(body)
+ }
+ }
+ else -> get(urlNoQuery, fieldMap, true)
+ }
+ }
+ }
+
/**
* 访问网站,返回ByteArray
*/
@@ -380,7 +410,7 @@ class AnalyzeUrl(
judgmentConcurrent()
setCookie(source?.getKey())
@Suppress("BlockingMethodInNonBlockingContext")
- return getProxyClient(proxy).newCall(retry) {
+ return getProxyClient(proxy).newCallResponseBody(retry) {
addHeaders(headerMap)
when (method) {
RequestMethod.POST -> {
diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
index d70ee088a..960a866ab 100644
--- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
+++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
@@ -11,6 +11,7 @@ import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.*
import kotlinx.coroutines.*
+import okhttp3.Response
import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
@@ -103,13 +104,20 @@ class HttpReadAloudService : BaseReadAloudService(),
} else { //没有下载并且没有缓存文件
try {
createSpeakCacheFile(fileName)
- AnalyzeUrl(
+ val analyzeUrl = AnalyzeUrl(
httpTts.url,
speakText = item,
speakSpeed = AppConfig.ttsSpeechRate,
source = httpTts,
headerMapF = httpTts.getHeaderMap(true)
- ).getByteArray().let { bytes ->
+ )
+ var response = analyzeUrl.getResponse()
+ httpTts.loginCheckJs?.let { checkJs ->
+ if (checkJs.isNotBlank()) {
+ response = analyzeUrl.evalJS(checkJs, response) as Response
+ }
+ }
+ response.body!!.bytes().let { bytes ->
ensureActive()
val file = getSpeakFileAsMd5IfNotExist(fileName)
file.writeBytes(bytes)
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 0d1b38103..967791ae6 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
@@ -12,7 +12,7 @@ import io.legado.app.help.AppConfig
import io.legado.app.help.BookSourceAnalyzer
import io.legado.app.help.ContentProcessor
import io.legado.app.help.SourceHelp
-import io.legado.app.help.http.newCall
+import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.model.NoStackTraceException
@@ -126,7 +126,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
}
private suspend fun importSourceUrl(url: String) {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
}.text("utf-8").let { body ->
when {
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 1386e4bf9..7f8534743 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
@@ -7,7 +7,7 @@ import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.AppConfig
-import io.legado.app.help.http.newCall
+import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.storage.OldReplace
@@ -84,7 +84,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
fun import(text: String) {
execute {
if (text.isAbsUrl()) {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(text)
}.text("utf-8").let {
val rules = OldReplace.jsonToReplaceRules(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 da0f2c6eb..840ed67ce 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
@@ -10,7 +10,7 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource
import io.legado.app.help.AppConfig
import io.legado.app.help.SourceHelp
-import io.legado.app.help.http.newCall
+import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.model.NoStackTraceException
@@ -122,7 +122,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
}
private suspend fun importSourceUrl(url: String) {
- okHttpClient.newCall {
+ okHttpClient.newCallResponseBody {
url(url)
}.text("utf-8").let { body ->
val items: List