pull/38/head
kunfei 5 years ago
parent 4bdbb4aadd
commit 36cd1a0ca7
  1. 8
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt
  2. 20
      app/src/main/java/io/legado/app/help/http/ByteConverter.kt
  3. 9
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  4. 28
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  5. 50
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  6. 51
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  7. 8
      app/src/main/java/io/legado/app/utils/NetworkUtils.kt

@ -42,4 +42,12 @@ interface IHttpPostApi {
@Body body: RequestBody, @Body body: RequestBody,
@HeaderMap headers: Map<String, String> @HeaderMap headers: Map<String, String>
): Call<String> ): Call<String>
@FormUrlEncoded
@POST
fun postMapByte(
@Url url: String,
@FieldMap(encoded = true) fieldMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Call<ByteArray>
} }

@ -0,0 +1,20 @@
package io.legado.app.help.http
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
class ByteConverter() : Converter.Factory() {
override fun responseBodyConverter(
type: Type?,
annotations: Array<Annotation>?,
retrofit: Retrofit?
): Converter<ResponseBody, ByteArray>? {
return Converter { value ->
value.bytes()
}
}
}

@ -51,6 +51,15 @@ object HttpHelper {
.build() .build()
} }
fun getByteRetrofit(baseUrl: String): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(ByteConverter())
//增加返回值为Observable<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
}
private fun getHeaderInterceptor(): Interceptor { private fun getHeaderInterceptor(): Interceptor {
return Interceptor { chain -> return Interceptor { chain ->
val request = chain.request() val request = chain.request()

@ -42,7 +42,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
if (content == null) throw AssertionError("Content cannot be null") if (content == null) throw AssertionError("Content cannot be null")
isJSON = content.toString().isJson() isJSON = content.toString().isJson()
this.content = content this.content = content
this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) this.baseUrl = baseUrl
objectChangedXP = true objectChangedXP = true
objectChangedJS = true objectChangedJS = true
objectChangedJP = true objectChangedJP = true
@ -150,19 +150,17 @@ class AnalyzeRule(private var book: BaseBook? = null) {
if (result is String) { if (result is String) {
result = listOf((result as String).htmlFormat().split("\n")) result = listOf((result as String).htmlFormat().split("\n"))
} }
baseUrl?.let { if (isUrl) {
if (isUrl && !TextUtils.isEmpty(it)) { val urlList = ArrayList<String>()
val urlList = ArrayList<String>() if (result is List<*>) {
if (result is List<*>) { for (url in result as List<*>) {
for (url in result as List<*>) { val absoluteURL = NetworkUtils.getAbsoluteURL(baseUrl, url.toString())
val absoluteURL = NetworkUtils.getAbsoluteURL(it, url.toString()) if (!absoluteURL.isNullOrEmpty() && !urlList.contains(absoluteURL)) {
if (!urlList.contains(absoluteURL)) { urlList.add(absoluteURL)
urlList.add(absoluteURL)
}
} }
} }
return urlList
} }
return urlList
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return result as? List<String> return result as? List<String>
@ -212,11 +210,9 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
} }
} }
if (result == null) return "" if (result == null) result = ""
baseUrl?.let { if (isUrl) {
return if (isUrl) { return NetworkUtils.getAbsoluteURL(baseUrl, result.toString()) ?: ""
NetworkUtils.getAbsoluteURL(it, result.toString())
} else result.toString()
} }
return result.toString() return result.toString()
} }

@ -255,23 +255,20 @@ class AnalyzeUrl(
return when { return when {
method == Method.POST -> { method == Method.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper.getApiService<IHttpPostApi>( HttpHelper
baseUrl .getApiService<IHttpPostApi>(baseUrl)
).postMap(url, fieldMap, headerMap) .postMap(url, fieldMap, headerMap)
} else { } else {
HttpHelper.getApiService<IHttpPostApi>( HttpHelper
baseUrl .getApiService<IHttpPostApi>(baseUrl)
).postBody( .postBody(url, body, headerMap)
url,
body,
headerMap
)
} }
} }
fieldMap.isEmpty() -> HttpHelper.getApiService<IHttpGetApi>( fieldMap.isEmpty() -> HttpHelper
baseUrl .getApiService<IHttpGetApi>(baseUrl)
).get(url, headerMap) .get(url, headerMap)
else -> HttpHelper.getApiService<IHttpGetApi>(baseUrl) else -> HttpHelper
.getApiService<IHttpGetApi>(baseUrl)
.getMap(url, fieldMap, headerMap) .getMap(url, fieldMap, headerMap)
} }
} }
@ -280,23 +277,20 @@ class AnalyzeUrl(
return when { return when {
method == Method.POST -> { method == Method.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper.getApiService<IHttpPostApi>( HttpHelper
baseUrl .getApiService<IHttpPostApi>(baseUrl)
).postMapAsync(url, fieldMap, headerMap) .postMapAsync(url, fieldMap, headerMap)
} else { } else {
HttpHelper.getApiService<IHttpPostApi>( HttpHelper
baseUrl .getApiService<IHttpPostApi>(baseUrl)
).postBodyAsync( .postBodyAsync(url, body, headerMap)
url,
body,
headerMap
)
} }
} }
fieldMap.isEmpty() -> HttpHelper.getApiService<IHttpGetApi>( fieldMap.isEmpty() -> HttpHelper
baseUrl .getApiService<IHttpGetApi>(baseUrl)
).getAsync(url, headerMap) .getAsync(url, headerMap)
else -> HttpHelper.getApiService<IHttpGetApi>(baseUrl) else -> HttpHelper
.getApiService<IHttpGetApi>(baseUrl)
.getMapAsync(url, fieldMap, headerMap) .getMapAsync(url, fieldMap, headerMap)
} }
} }

@ -3,10 +3,20 @@ package io.legado.app.service
import android.app.PendingIntent import android.app.PendingIntent
import android.media.MediaPlayer import android.media.MediaPlayer
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.data.api.IHttpPostApi
import io.legado.app.help.FileHelp
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.http.HttpHelper
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
import java.io.File
import java.io.FileInputStream
class HttpReadAloudService : BaseReadAloudService(), class HttpReadAloudService : BaseReadAloudService(),
MediaPlayer.OnPreparedListener, MediaPlayer.OnPreparedListener,
@ -27,19 +37,46 @@ class HttpReadAloudService : BaseReadAloudService(),
mediaPlayer.release() mediaPlayer.release()
} }
private fun getAudioPath(): String { private fun getAudioBody(): Map<String, String> {
val audioText = contentList[nowSpeak]
val spd = (getPrefInt("ttsSpeechRate", 25) + 5) / 5 val spd = (getPrefInt("ttsSpeechRate", 25) + 5) / 5
val per = getPrefString("ttsSpeechPer") ?: "0" val per = getPrefString("ttsSpeechPer") ?: "0"
return "http://tts.baidu.com/text2audio?idx=1&tex=$audioText&cuid=baidu_speech_demo&cod=2&lan=zh&ctp=1&pdt=1&spd=$spd&per=$per&vol=5&pit=5&_res_tag_=audio" return mapOf(
Pair("idx", "1"),
Pair("tex", contentList[nowSpeak]),
Pair("cuid", "baidu_speech_demo "),
Pair("cod", "2"),
Pair("lan", "zh"),
Pair("ctp", "1"),
Pair("pdt", "1"),
Pair("spd", spd.toString()),
Pair("per", per),
Pair("vol", "5"),
Pair("pit", "5"),
Pair("_res_tag_", "audio")
)
} }
override fun play() { override fun play() {
if (contentList.isEmpty()) return if (contentList.isEmpty()) return
if (requestFocus()) { launch(IO) {
mediaPlayer.reset() if (requestFocus()) {
mediaPlayer.setDataSource(getAudioPath()) val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com")
mediaPlayer.prepareAsync() .create(IHttpPostApi::class.java)
.postMapByte("http://tts.baidu.com/text2audio", getAudioBody(), mapOf())
.execute().body()
if (bytes == null) {
withContext(Main) {
toast("访问失败")
}
} else {
val file =
FileHelp.getFile(cacheDir.absolutePath + File.separator + "bdTts.mp3")
file.writeBytes(bytes)
mediaPlayer.reset()
mediaPlayer.setDataSource(FileInputStream(file).fd)
mediaPlayer.prepareAsync()
}
}
} }
} }

@ -1,6 +1,5 @@
package io.legado.app.utils package io.legado.app.utils
import android.text.TextUtils
import retrofit2.Response import retrofit2.Response
import java.net.URL import java.net.URL
import java.util.* import java.util.*
@ -71,11 +70,12 @@ object NetworkUtils {
/** /**
* 获取绝对地址 * 获取绝对地址
*/ */
fun getAbsoluteURL(baseURL: String, relativePath: String): String { fun getAbsoluteURL(baseURL: String?, relativePath: String?): String? {
if (baseURL.isNullOrEmpty()) return relativePath
if (relativePath.isNullOrEmpty()) return baseURL
var relativeUrl = relativePath var relativeUrl = relativePath
if (TextUtils.isEmpty(baseURL)) return relativePath
try { try {
val absoluteUrl = URL(baseURL) val absoluteUrl = URL(baseURL.substringBefore(","))
val parseUrl = URL(absoluteUrl, relativePath) val parseUrl = URL(absoluteUrl, relativePath)
relativeUrl = parseUrl.toString() relativeUrl = parseUrl.toString()
return relativeUrl return relativeUrl

Loading…
Cancel
Save