Merge pull request #1217 from ag2s20150909/master

字典:中文使用百度汉语词典,英文使用海词词典。
pull/1219/head
kunfei 3 years ago committed by GitHub
commit 2f6444035c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. BIN
      app/cronetlib/cronet_api.jar
  2. BIN
      app/cronetlib/cronet_impl_common_java.jar
  3. BIN
      app/cronetlib/src/cronet_api-src.jar
  4. BIN
      app/cronetlib/src/cronet_impl_common_java-src.jar
  5. 5
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  6. 52
      app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt

Binary file not shown.

@ -27,7 +27,12 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
response
} catch (e: Exception) {
//遇到Cronet处理有问题时的情况,如证书过期等等,回退到okhttp处理
if (e.message.toString().contains("ERR_CERT_DATE_INVALID", true)) {
chain.proceed(original)
} else {
throw e
}
}

@ -8,12 +8,27 @@ import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient
import io.legado.app.utils.toastOnUi
import org.jsoup.Jsoup
import java.util.regex.Pattern
class DictViewModel(application: Application) : BaseViewModel(application) {
var dictHtmlData: MutableLiveData<String> = MutableLiveData()
fun dict(word: String) {
if(isChinese(word)){
baiduDict(word)
}else{
haiciDict(word)
}
}
/**
* 海词英文词典
*
* @param word
*/
private fun haiciDict(word: String) {
execute {
val body = okHttpClient.newCallStrResponse {
get("https://apii.dict.cn/mini.php", mapOf(Pair("q", word)))
@ -25,7 +40,44 @@ class DictViewModel(application: Application) : BaseViewModel(application) {
}.onError {
context.toastOnUi(it.localizedMessage)
}
}
/**
* 百度汉语词典
*
* @param word
*/
private fun baiduDict(word: String) {
execute {
val body = okHttpClient.newCallStrResponse {
get("https://dict.baidu.com/s", mapOf(Pair("wd", word)))
}.body
val jsoup = Jsoup.parse(body!!)
jsoup.select("script").remove()//移除script
jsoup.select("#word-header").remove()//移除单字的header
jsoup.select("#term-header").remove()//移除词语的header
jsoup.select(".more-button").remove()//移除展示更多
jsoup.select(".disactive").remove()
jsoup.select("#download-wrapper").remove()//移除下载广告
jsoup.select("#right-panel").remove()//移除右侧广告
jsoup.select("#content-panel")
}.onSuccess {
dictHtmlData.postValue(it.html())
}.onError {
context.toastOnUi(it.localizedMessage)
}
}
/**
* 判断是否包含汉字
* @param str
* @return
*/
fun isChinese(str: String): Boolean {
val p = Pattern.compile("[\u4e00-\u9fa5]")
val m = p.matcher(str)
return m.find()
}
}
Loading…
Cancel
Save