pull/32/head
kunfei 5 years ago
parent f56ad432d2
commit 44ab6dc900
  1. 2
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 27
      app/src/main/java/io/legado/app/data/api/IHttpGetApi.java
  3. 33
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.java
  4. 16
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  5. 37
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeHeaders.kt

@ -12,6 +12,8 @@ object AppConst {
const val APP_TAG = "Legado"
const val RC_IMPORT_YUEDU_DATA = 100
const val userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2357.134 Safari/537.36"
val SCRIPT_ENGINE = ScriptEngineManager().getEngineByName("rhino")
val NOT_AVAILABLE = App.INSTANCE.getString(R.string.not_available)

@ -0,0 +1,27 @@
package io.legado.app.data.api;
import java.util.Map;
import kotlinx.coroutines.Deferred;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* Created by GKF on 2018/1/21.
* get web content
*/
public interface IHttpGetApi {
@GET
Deferred<Response<String>> get(@Url String url,
@HeaderMap Map<String, String> headers);
@GET
Deferred<Response<String>> getMap(@Url String url,
@QueryMap(encoded = true) Map<String, String> queryMap,
@HeaderMap Map<String, String> headers);
}

@ -0,0 +1,33 @@
package io.legado.app.data.api;
import java.util.Map;
import kotlinx.coroutines.Deferred;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
import retrofit2.http.Url;
/**
* Created by GKF on 2018/1/29.
* post
*/
public interface IHttpPostApi {
@FormUrlEncoded
@POST
Deferred<Response<String>> postMap(@Url String url,
@FieldMap(encoded = true) Map<String, String> fieldMap,
@HeaderMap Map<String, String> headers);
@FormUrlEncoded
@POST
Deferred<Response<String>> postJson(@Url String url,
@Body RequestBody body,
@HeaderMap Map<String, String> headers);
}

@ -4,10 +4,14 @@ import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import io.legado.app.App
import io.legado.app.constant.AppConst.userAgent
import io.legado.app.data.entities.rule.*
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getPrefString
import kotlinx.android.parcel.Parcelize
import java.util.*
@Parcelize
@Entity(
@ -34,6 +38,18 @@ data class BookSource(
var ruleContent: String? = null // 正文页规则
) : Parcelable {
fun getHeaderMap(): Map<String, String> {
val headerMap = HashMap<String, String>()
headerMap["user_agent"] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
header?.let {
GSON.fromJsonObject<Map<String, String>>(header)?.let {
headerMap.putAll(it)
}
}
return headerMap
}
fun getSearchRule(): SearchRule? {
return GSON.fromJsonObject<SearchRule>(ruleSearch)
}

@ -1,37 +0,0 @@
package io.legado.app.model.analyzeRule
import android.text.TextUtils.isEmpty
import io.legado.app.App
import io.legado.app.R
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getPrefString
import java.util.*
/**
* Created by GKF on 2018/3/2.
* 解析Headers
*/
object AnalyzeHeaders {
private val defaultUserAgent: String
get() = App.INSTANCE.getPrefString("user_agent")
?: App.INSTANCE.getString(R.string.pv_user_agent)
fun getMap(bookSource: BookSource?): Map<String, String> {
val headerMap = HashMap<String, String>()
if (bookSource != null && !isEmpty(bookSource.header)) {
bookSource.header?.let {
val map: HashMap<String, String>? = GSON.fromJsonObject<HashMap<String, String>>(it)
map?.let { headerMap.putAll(map) }
}
}
if (bookSource != null) {
val cookie = App.db.sourceCookieDao().getCookieByUrl(bookSource.bookSourceUrl)
cookie?.let { headerMap["Cookie"] = cookie }
}
return headerMap
}
}
Loading…
Cancel
Save