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,
@HeaderMap headers: Map<String, 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()
}
fun getByteRetrofit(baseUrl: String): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(ByteConverter())
//增加返回值为Observable<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
}
private fun getHeaderInterceptor(): Interceptor {
return Interceptor { chain ->
val request = chain.request()

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

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

@ -3,10 +3,20 @@ package io.legado.app.service
import android.app.PendingIntent
import android.media.MediaPlayer
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.http.HttpHelper
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString
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(),
MediaPlayer.OnPreparedListener,
@ -27,19 +37,46 @@ class HttpReadAloudService : BaseReadAloudService(),
mediaPlayer.release()
}
private fun getAudioPath(): String {
val audioText = contentList[nowSpeak]
private fun getAudioBody(): Map<String, String> {
val spd = (getPrefInt("ttsSpeechRate", 25) + 5) / 5
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() {
if (contentList.isEmpty()) return
if (requestFocus()) {
mediaPlayer.reset()
mediaPlayer.setDataSource(getAudioPath())
mediaPlayer.prepareAsync()
launch(IO) {
if (requestFocus()) {
val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com")
.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
import android.text.TextUtils
import retrofit2.Response
import java.net.URL
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
if (TextUtils.isEmpty(baseURL)) return relativePath
try {
val absoluteUrl = URL(baseURL)
val absoluteUrl = URL(baseURL.substringBefore(","))
val parseUrl = URL(absoluteUrl, relativePath)
relativeUrl = parseUrl.toString()
return relativeUrl

Loading…
Cancel
Save