diff --git a/app/build.gradle b/app/build.gradle index c15bcc12f..0a9e9b726 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -83,7 +83,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.1.0-rc01' implementation 'androidx.preference:preference:1.1.0-rc01' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1' - implementation 'com.google.android.material:material:1.1.0-alpha07' + implementation 'com.google.android.material:material:1.1.0-alpha08' implementation 'com.google.android:flexbox:1.1.0' //lifecycle 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 deleted file mode 100644 index 499c97d17..000000000 --- a/app/src/main/java/io/legado/app/data/api/IHttpGetApi.java +++ /dev/null @@ -1,27 +0,0 @@ -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/IHttpGetApi.kt b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt new file mode 100644 index 000000000..f1f01b8a3 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt @@ -0,0 +1,29 @@ +package io.legado.app.data.api + +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 + */ + +interface IHttpGetApi { + @GET + operator fun get( + @Url url: String, + @HeaderMap headers: Map + ): Deferred> + + @GET + fun getMap( + @Url url: String, + @QueryMap(encoded = true) queryMap: Map, + @HeaderMap headers: Map + ): Deferred> + +} 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 deleted file mode 100644 index 1b10207ff..000000000 --- a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.java +++ /dev/null @@ -1,32 +0,0 @@ -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); - - @POST - Deferred> postBody(@Url String url, - @Body RequestBody body, - @HeaderMap Map headers); -} diff --git a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt new file mode 100644 index 000000000..2cfb5927b --- /dev/null +++ b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt @@ -0,0 +1,29 @@ +package io.legado.app.data.api + +import kotlinx.coroutines.Deferred +import okhttp3.RequestBody +import retrofit2.Response +import retrofit2.http.* + +/** + * Created by GKF on 2018/1/29. + * post + */ + +interface IHttpPostApi { + + @FormUrlEncoded + @POST + fun postMap( + @Url url: String, + @FieldMap(encoded = true) fieldMap: Map, + @HeaderMap headers: Map + ): Deferred> + + @POST + fun postBody( + @Url url: String, + @Body body: RequestBody, + @HeaderMap headers: Map + ): Deferred> +} 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 6e52d5570..eaf37c9be 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 @@ -2,6 +2,7 @@ package io.legado.app.data.entities import android.os.Parcelable import androidx.room.Entity +import androidx.room.Ignore import androidx.room.Index import androidx.room.PrimaryKey import io.legado.app.App @@ -10,6 +11,7 @@ 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.IgnoredOnParcel import kotlinx.android.parcel.Parcelize import java.util.* @@ -37,6 +39,25 @@ data class BookSource( var ruleToc: String? = null, // 目录页规则 var ruleContent: String? = null // 正文页规则 ) : Parcelable { + @Ignore + @IgnoredOnParcel + var searchRuleV:SearchRule? = null + + @Ignore + @IgnoredOnParcel + var exploreRuleV:ExploreRule? = null + + @Ignore + @IgnoredOnParcel + var bookInfoRuleV:BookInfoRule? = null + + @Ignore + @IgnoredOnParcel + var tocRuleV:TocRule? = null + + @Ignore + @IgnoredOnParcel + var contentRuleV:ContentRule? = null fun getHeaderMap(): Map { val headerMap = HashMap() @@ -50,23 +71,43 @@ data class BookSource( } - fun getSearchRule(): SearchRule? { - return GSON.fromJsonObject(ruleSearch) + fun getSearchRule(): SearchRule { + searchRuleV?:let { + searchRuleV = GSON.fromJsonObject(ruleSearch) + searchRuleV?:let { searchRuleV = SearchRule() } + } + return searchRuleV!! } - fun getExploreRule(): ExploreRule? { - return GSON.fromJsonObject(ruleExplore) + fun getExploreRule(): ExploreRule { + exploreRuleV?:let { + exploreRuleV = GSON.fromJsonObject(ruleExplore) + exploreRuleV?:let { exploreRuleV = ExploreRule() } + } + return exploreRuleV!! } fun getBookInfoRule(): BookInfoRule? { - return GSON.fromJsonObject(ruleBookInfo) + bookInfoRuleV?:let { + bookInfoRuleV = GSON.fromJsonObject(ruleBookInfo) + bookInfoRuleV?:let { bookInfoRuleV = BookInfoRule() } + } + return bookInfoRuleV!! } fun getTocRule(): TocRule? { - return GSON.fromJsonObject(ruleToc) + tocRuleV?:let { + tocRuleV = GSON.fromJsonObject(ruleToc) + tocRuleV?:let { tocRuleV = TocRule() } + } + return tocRuleV!! } fun getContentRule(): ContentRule? { - return GSON.fromJsonObject(ruleContent) + contentRuleV?:let { + contentRuleV = GSON.fromJsonObject(ruleContent) + contentRuleV?:let { contentRuleV = ContentRule() } + } + return contentRuleV!! } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt new file mode 100644 index 000000000..b6f4b90dc --- /dev/null +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -0,0 +1,41 @@ +package io.legado.app.model + +import io.legado.app.data.api.IHttpGetApi +import io.legado.app.data.api.IHttpPostApi +import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.SearchBook +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.http.HttpHelper +import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.webbook.BookList +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.MainScope + +class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope() { + + fun searchBook(key: String, page: Int?): Coroutine> { + return Coroutine.with(this) { + bookSource.getSearchRule().searchUrl?.let { searchUrl -> + val analyzeUrl = AnalyzeUrl(searchUrl) + val response = when { + analyzeUrl.method == AnalyzeUrl.Method.POST -> HttpHelper.getApiService( + analyzeUrl.baseUrl + ).postBody( + analyzeUrl.url, + analyzeUrl.body, + analyzeUrl.headerMap + ).await() + analyzeUrl.fieldMap.isEmpty() -> HttpHelper.getApiService( + analyzeUrl.baseUrl + )[analyzeUrl.url, analyzeUrl.headerMap].await() + else -> HttpHelper.getApiService(analyzeUrl.baseUrl) + .getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await() + } + return@with BookList().analyzeBookList(response, bookSource, analyzeUrl) + } + return@with ArrayList() + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 28067b32a..8f7c02698 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -27,7 +27,7 @@ class AnalyzeUrl( key: String? = null, page: Int? = null, headerMapF: Map? = null, - var baseUrl: String? = null, + baseUrl: String? = null, book: Book? = null ) { companion object { @@ -35,19 +35,19 @@ class AnalyzeUrl( private val jsonType = MediaType.parse("application/json; charset=utf-8") } - lateinit var url: String + lateinit var baseUrl: String private set - var host: String? = null + lateinit var url: String private set var path: String? = null private set var queryStr: String? = null private set - private val fieldMap = LinkedHashMap() - private val headerMap = HashMap() + val fieldMap = LinkedHashMap() + val headerMap = HashMap() private var charset: String? = null private var bodyTxt: String? = null - var body: RequestBody? = null + lateinit var body: RequestBody private set var method = Method.GET private set @@ -64,7 +64,9 @@ class AnalyzeUrl( } init { - baseUrl = baseUrl?.split(",\n*".toRegex(), 1)?.get(0) + baseUrl?.let { + this.baseUrl = it.split(",\n*".toRegex(), 1)[0] + } headerMapF?.let { headerMap.putAll(it) } //替换参数 replaceKeyPageJs(key, page, book) @@ -124,7 +126,9 @@ class AnalyzeUrl( private fun initUrl() { var urlArray = ruleUrl.split(",[^\\{]*".toRegex(), 2) url = urlArray[0] - host = NetworkUtils.getBaseUrl(url) + NetworkUtils.getBaseUrl(url)?.let { + baseUrl = it + } if (urlArray.size > 1) { val options = GSON.fromJsonObject>(urlArray[1]) options?.let { @@ -155,6 +159,8 @@ class AnalyzeUrl( builder.add(item.key, item.value) body = builder.build() } + } ?: let { + body = FormBody.Builder().build() } } } diff --git a/app/src/main/java/io/legado/app/model/book/All.kt b/app/src/main/java/io/legado/app/model/book/All.kt deleted file mode 100644 index e75b4ac5a..000000000 --- a/app/src/main/java/io/legado/app/model/book/All.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.legado.app.model.book - -class All { - - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/book/BookChapterList.kt b/app/src/main/java/io/legado/app/model/book/BookChapterList.kt deleted file mode 100644 index 38e297239..000000000 --- a/app/src/main/java/io/legado/app/model/book/BookChapterList.kt +++ /dev/null @@ -1,5 +0,0 @@ -package io.legado.app.model.book - -class BookChapterList { - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/book/BookContent.kt b/app/src/main/java/io/legado/app/model/book/BookContent.kt deleted file mode 100644 index 8d6341b32..000000000 --- a/app/src/main/java/io/legado/app/model/book/BookContent.kt +++ /dev/null @@ -1,5 +0,0 @@ -package io.legado.app.model.book - -class BookContent { - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/book/BookInfo.kt b/app/src/main/java/io/legado/app/model/book/BookInfo.kt deleted file mode 100644 index 6dd7e1e07..000000000 --- a/app/src/main/java/io/legado/app/model/book/BookInfo.kt +++ /dev/null @@ -1,5 +0,0 @@ -package io.legado.app.model.book - -class BookInfo { - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/book/BookList.kt b/app/src/main/java/io/legado/app/model/book/BookList.kt deleted file mode 100644 index a4660a4d8..000000000 --- a/app/src/main/java/io/legado/app/model/book/BookList.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.legado.app.model.book - -import io.legado.app.data.entities.SearchBook - -class BookList { - - fun analyzeBookList(): ArrayList { - var bookList = ArrayList() - - return bookList - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt new file mode 100644 index 000000000..9ec26f599 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt @@ -0,0 +1,5 @@ +package io.legado.app.model.webbook + +class BookChapterList { + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt new file mode 100644 index 000000000..76473a236 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt @@ -0,0 +1,5 @@ +package io.legado.app.model.webbook + +class BookContent { + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt new file mode 100644 index 000000000..f08400e74 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt @@ -0,0 +1,5 @@ +package io.legado.app.model.webbook + +class BookInfo { + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt new file mode 100644 index 000000000..ee66e20c0 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -0,0 +1,35 @@ +package io.legado.app.model.webbook + +import io.legado.app.App +import io.legado.app.R +import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.SearchBook +import io.legado.app.model.analyzeRule.AnalyzeRule +import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.utils.NetworkUtils +import retrofit2.Response + +class BookList { + + @Throws(Exception::class) + fun analyzeBookList( + response: Response, + bookSource: BookSource, + analyzeUrl: AnalyzeUrl, + isSearch: Boolean = true + ): ArrayList { + var bookList = ArrayList() + val baseUrl: String = NetworkUtils.getUrl(response) + val body: String? = response.body() + body ?: throw Exception( + App.INSTANCE.getString( + R.string.get_web_content_error, + baseUrl + ) + ) + val analyzer = AnalyzeRule(null) + analyzer.setContent(body, baseUrl) + + return bookList + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index ae2d42d47..90e68f004 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -1,10 +1,16 @@ package io.legado.app.utils import android.text.TextUtils +import retrofit2.Response import java.net.URL import java.util.* object NetworkUtils { + fun getUrl(response: Response<*>): String { + val networkResponse = response.raw().networkResponse() + return networkResponse?.request()?.url()?.toString() + ?: response.raw().request().url().toString() + } private val notNeedEncoding: BitSet by lazy { val bitSet = BitSet(256) diff --git a/build.gradle b/build.gradle index dededb69b..4dfd23870 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.31' + ext.kotlin_version = '1.3.41' repositories { google() jcenter() maven { url 'https://maven.fabric.io/public' } } dependencies { - classpath 'com.android.tools.build:gradle:3.4.1' + classpath 'com.android.tools.build:gradle:3.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files