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> = jsonPath.parse(body).read("$") 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 132133143..c164d8e71 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 @@ -9,7 +9,7 @@ import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.TxtTocRule import io.legado.app.help.ReadBookConfig import io.legado.app.help.ThemeConfig -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 @@ -25,7 +25,7 @@ class OnLineImportViewModel(app: Application) : BaseViewModel(app) { fun getText(url: String, success: (text: String) -> Unit) { execute { - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(url) }.text("utf-8") }.onSuccess { @@ -40,7 +40,7 @@ class OnLineImportViewModel(app: Application) : BaseViewModel(app) { fun getBytes(url: String, success: (bytes: ByteArray) -> Unit) { execute { @Suppress("BlockingMethodInNonBlockingContext") - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(url) }.bytes() }.onSuccess { @@ -140,7 +140,7 @@ class OnLineImportViewModel(app: Application) : BaseViewModel(app) { fun determineType(url: String, finally: (title: String, msg: String) -> Unit) { execute { - val rs = okHttpClient.newCall { + val rs = okHttpClient.newCallResponseBody { url(url) } when (rs.contentType()) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 5cb729d15..8ca29f81c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -16,7 +16,7 @@ import io.legado.app.databinding.DialogReadBgTextBinding import io.legado.app.databinding.ItemBgImageBinding import io.legado.app.help.DefaultData import io.legado.app.help.ReadBookConfig -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.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert @@ -295,7 +295,7 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) { private fun importNetConfig(url: String) { execute { @Suppress("BlockingMethodInNonBlockingContext") - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(url) }.bytes().let { importConfig(it) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index 4009b7cee..dec3b7bd0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -7,7 +7,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.HttpTTS import io.legado.app.help.DefaultData -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 @@ -25,7 +25,7 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application fun importOnLine(url: String) { execute { - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(url) }.text("utf-8").let { json -> import(json) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt index d1ce1ae26..2b5236e50 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt @@ -5,7 +5,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.TxtTocRule import io.legado.app.help.DefaultData -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.GSON @@ -30,7 +30,7 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) { fun importOnLine(url: String, finally: (msg: String) -> Unit) { execute { - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(url) }.text("utf-8").let { json -> GSON.fromJsonArray(json)?.let { diff --git a/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt b/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt index 13218cfc8..39c5b500b 100644 --- a/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt @@ -9,7 +9,7 @@ import androidx.documentfile.provider.DocumentFile import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppConst import io.legado.app.help.IntentData -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.model.NoStackTraceException import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -70,7 +70,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) { private suspend fun webData2bitmap(data: String): ByteArray? { return if (URLUtil.isValidUrl(data)) { @Suppress("BlockingMethodInNonBlockingContext") - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(data) }.bytes() } else { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index f1022171b..d32190b20 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -6,7 +6,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource -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 @@ -87,7 +87,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) val text = str.trim() when { text.isAbsUrl() -> { - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(text) }.text().let { importBookshelf(it, groupId) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 171bb55b9..23024a514 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -17,7 +17,7 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssStar -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.model.analyzeRule.AnalyzeUrl import io.legado.app.model.rss.Rss @@ -157,7 +157,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), private suspend fun webData2bitmap(data: String): ByteArray? { return if (URLUtil.isValidUrl(data)) { @Suppress("BlockingMethodInNonBlockingContext") - okHttpClient.newCall { + okHttpClient.newCallResponseBody { url(data) }.bytes() } else {