Merge pull request #439 from AndyBernie/master

正文图片下载和cookie设置
pull/440/head
kunfei 4 years ago committed by GitHub
commit 625c83e9b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 8
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 15
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  4. 18
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -101,7 +101,7 @@ object BookHelp {
downloadImages.add(src) downloadImages.add(src)
val analyzeUrl = AnalyzeUrl(src) val analyzeUrl = AnalyzeUrl(src)
try { try {
analyzeUrl.getImageBytes(book.origin)?.let { analyzeUrl.getResponseBytes(book.origin)?.let {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,

@ -4,6 +4,7 @@ import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.help.http.SSLHelper import io.legado.app.help.http.SSLHelper
import io.legado.app.help.http.CookieStore
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import org.jsoup.Connection import org.jsoup.Connection
@ -119,6 +120,13 @@ interface JsExtensions {
.execute() .execute()
} }
/**
*js实现读取cookie
*/
fun getCookie(tag: String): String {
return CookieStore.getCookie(tag)
}
/** /**
* js实现解码,不能删 * js实现解码,不能删
*/ */

@ -48,21 +48,6 @@ object HttpHelper {
return null return null
} }
fun getBytes(
url: String,
queryMap: Map<String, String>,
headers: Map<String, String>
): ByteArray? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
return getByteRetrofit(baseUrl)
.create(HttpGetApi::class.java)
.getMapByte(url, queryMap, headers)
.execute()
.body()
}
return null
}
suspend fun simpleGetAsync(url: String, encode: String? = null): String? { suspend fun simpleGetAsync(url: String, encode: String? = null): String? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
val response = getApiService<HttpGetApi>(baseUrl, encode) val response = getApiService<HttpGetApi>(baseUrl, encode)

@ -281,7 +281,7 @@ class AnalyzeUrl(
fun getResponse(tag: String): Call<String> { fun getResponse(tag: String): Call<String> {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] = cookie headerMap["Cookie"] += ";${cookie}"
} }
return when { return when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
@ -324,7 +324,7 @@ class AnalyzeUrl(
} }
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] = cookie headerMap["Cookie"] += ";${cookie}"
} }
val res = when { val res = when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
@ -352,23 +352,11 @@ class AnalyzeUrl(
return Res(NetworkUtils.getUrl(res), res.body()) return Res(NetworkUtils.getUrl(res), res.body())
} }
fun getImageBytes(tag: String): ByteArray? {
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {
headerMap["Cookie"] += cookie
}
return if (fieldMap.isEmpty()) {
HttpHelper.getBytes(url, mapOf(), headerMap)
} else {
HttpHelper.getBytes(url, fieldMap, headerMap)
}
}
suspend fun getResponseBytes(tag: String? = null): ByteArray? { suspend fun getResponseBytes(tag: String? = null): ByteArray? {
if (tag != null) { if (tag != null) {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] = cookie headerMap["Cookie"] += ";${cookie}"
} }
} }
val response = when { val response = when {

Loading…
Cancel
Save