diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 0d98ed75e..ab152e8a4 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.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) diff --git a/app/src/main/java/io/legado/app/data/api/IHttpGetApi.java b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.java new file mode 100644 index 000000000..499c97d17 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.java @@ -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> get(@Url String url, + @HeaderMap Map headers); + + @GET + Deferred> getMap(@Url String url, + @QueryMap(encoded = true) Map queryMap, + @HeaderMap Map headers); + +} diff --git a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.java b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.java new file mode 100644 index 000000000..0318d263f --- /dev/null +++ b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.java @@ -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> postMap(@Url String url, + @FieldMap(encoded = true) Map fieldMap, + @HeaderMap Map headers); + + @FormUrlEncoded + @POST + Deferred> postJson(@Url String url, + @Body RequestBody body, + @HeaderMap Map headers); +} diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index db9071165..6e52d5570 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -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 { + val headerMap = HashMap() + headerMap["user_agent"] = App.INSTANCE.getPrefString("user_agent") ?: userAgent + header?.let { + GSON.fromJsonObject>(header)?.let { + headerMap.putAll(it) + } + } + return headerMap + } + + fun getSearchRule(): SearchRule? { return GSON.fromJsonObject(ruleSearch) } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeHeaders.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeHeaders.kt deleted file mode 100644 index d96d1e2de..000000000 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeHeaders.kt +++ /dev/null @@ -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 { - val headerMap = HashMap() - if (bookSource != null && !isEmpty(bookSource.header)) { - bookSource.header?.let { - val map: HashMap? = GSON.fromJsonObject>(it) - map?.let { headerMap.putAll(map) } - } - } - if (bookSource != null) { - val cookie = App.db.sourceCookieDao().getCookieByUrl(bookSource.bookSourceUrl) - cookie?.let { headerMap["Cookie"] = cookie } - } - return headerMap - } -}