From 8dca2c885e98a2afae2dd81515fae4d4ce43cf05 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 11 Jul 2019 09:19:43 +0800 Subject: [PATCH 1/6] up --- .../io/legado/app/data/api/IHttpGetApi.java | 27 ------------ .../io/legado/app/data/api/IHttpGetApi.kt | 29 +++++++++++++ .../io/legado/app/data/api/IHttpPostApi.java | 32 --------------- .../io/legado/app/data/api/IHttpPostApi.kt | 29 +++++++++++++ .../main/java/io/legado/app/model/WebBook.kt | 41 +++++++++++++++++++ .../app/model/analyzeRule/AnalyzeUrl.kt | 20 +++++---- .../main/java/io/legado/app/model/book/All.kt | 6 --- .../legado/app/model/book/BookChapterList.kt | 5 --- .../io/legado/app/model/book/BookContent.kt | 5 --- .../java/io/legado/app/model/book/BookInfo.kt | 5 --- .../app/model/webbook/BookChapterList.kt | 5 +++ .../legado/app/model/webbook/BookContent.kt | 5 +++ .../io/legado/app/model/webbook/BookInfo.kt | 5 +++ .../app/model/{book => webbook}/BookList.kt | 2 +- .../legado/app/ui/search/SearchViewModel.kt | 2 + 15 files changed, 129 insertions(+), 89 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/data/api/IHttpGetApi.java create mode 100644 app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt delete mode 100644 app/src/main/java/io/legado/app/data/api/IHttpPostApi.java create mode 100644 app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt create mode 100644 app/src/main/java/io/legado/app/model/WebBook.kt delete mode 100644 app/src/main/java/io/legado/app/model/book/All.kt delete mode 100644 app/src/main/java/io/legado/app/model/book/BookChapterList.kt delete mode 100644 app/src/main/java/io/legado/app/model/book/BookContent.kt delete mode 100644 app/src/main/java/io/legado/app/model/book/BookInfo.kt create mode 100644 app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt create mode 100644 app/src/main/java/io/legado/app/model/webbook/BookContent.kt create mode 100644 app/src/main/java/io/legado/app/model/webbook/BookInfo.kt rename app/src/main/java/io/legado/app/model/{book => webbook}/BookList.kt (84%) 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/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt new file mode 100644 index 000000000..6af5d455d --- /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.help.http.HttpHelper +import io.legado.app.model.analyzeRule.AnalyzeUrl +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.launch + +class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { + + fun searchBook(key: String, page: Int?, success: (() -> Unit), error: (() -> Unit)) { + launch(IO) { + val searchRule = bookSource.getSearchRule() + searchRule?.searchUrl?.let { searchUrl -> + val analyzeUrl = AnalyzeUrl(searchUrl) + val res = 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() + } + + } + + } + } + + +} \ 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..85a63ae10 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 { 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/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/book/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt similarity index 84% rename from app/src/main/java/io/legado/app/model/book/BookList.kt rename to app/src/main/java/io/legado/app/model/webbook/BookList.kt index a4660a4d8..0fc7bae82 100644 --- a/app/src/main/java/io/legado/app/model/book/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -1,4 +1,4 @@ -package io.legado.app.model.book +package io.legado.app.model.webbook import io.legado.app.data.entities.SearchBook diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 376a47b76..7c0e2d0bf 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -31,6 +31,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { .onErrorReturn { "error return" } .onStart { Log.e("TAG!", "start") + start } .onSuccess { Log.e("TAG!", "success: $it") @@ -40,6 +41,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } .onFinally { Log.e("TAG!", "finally") + finally } } From ef3f7f770c7a74e30720a08ac4bca6d21484fe45 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 11 Jul 2019 16:19:56 +0800 Subject: [PATCH 2/6] up --- app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 2 ++ 1 file changed, 2 insertions(+) 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 85a63ae10..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 @@ -159,6 +159,8 @@ class AnalyzeUrl( builder.add(item.key, item.value) body = builder.build() } + } ?: let { + body = FormBody.Builder().build() } } } From 2f41e23d68636ae25951315a3ded71ec27c81d71 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 11 Jul 2019 23:12:52 +0800 Subject: [PATCH 3/6] up --- app/build.gradle | 2 +- .../io/legado/app/data/entities/BookSource.kt | 55 ++++++++++++++++--- .../main/java/io/legado/app/model/WebBook.kt | 9 +-- .../io/legado/app/model/webbook/BookList.kt | 4 +- build.gradle | 4 +- 5 files changed, 59 insertions(+), 15 deletions(-) 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/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 index 6af5d455d..adcdba288 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -3,8 +3,10 @@ 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.http.HttpHelper import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.webbook.BookList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.MainScope @@ -14,10 +16,9 @@ class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { fun searchBook(key: String, page: Int?, success: (() -> Unit), error: (() -> Unit)) { launch(IO) { - val searchRule = bookSource.getSearchRule() - searchRule?.searchUrl?.let { searchUrl -> + bookSource.getSearchRule().searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl(searchUrl) - val res = when { + val response = when { analyzeUrl.method == AnalyzeUrl.Method.POST -> HttpHelper.getApiService( analyzeUrl.baseUrl ).postBody( @@ -31,7 +32,7 @@ class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { else -> HttpHelper.getApiService(analyzeUrl.baseUrl) .getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await() } - + val bookList = BookList().analyzeBookList(response, bookSource) } } 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 index 0fc7bae82..d399e2386 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -1,10 +1,12 @@ package io.legado.app.model.webbook +import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook +import retrofit2.Response class BookList { - fun analyzeBookList(): ArrayList { + fun analyzeBookList(response: Response, bookSource: BookSource): ArrayList { var bookList = ArrayList() return bookList 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 From 13526640b8debba7b05dd26fbe9b537ddce35c60 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 12 Jul 2019 09:31:16 +0800 Subject: [PATCH 4/6] up --- .../main/java/io/legado/app/model/WebBook.kt | 11 +++++------ .../io/legado/app/model/webbook/BookList.kt | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index adcdba288..34fd2caf8 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -4,18 +4,17 @@ 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.Dispatchers.IO import kotlinx.coroutines.MainScope -import kotlinx.coroutines.launch class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { - fun searchBook(key: String, page: Int?, success: (() -> Unit), error: (() -> Unit)) { - launch(IO) { + fun searchBook(key: String, page: Int?): Coroutine> { + return Coroutine.with(this) { bookSource.getSearchRule().searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl(searchUrl) val response = when { @@ -32,9 +31,9 @@ class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { else -> HttpHelper.getApiService(analyzeUrl.baseUrl) .getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await() } - val bookList = BookList().analyzeBookList(response, bookSource) + return@with BookList().analyzeBookList(response, bookSource, analyzeUrl) } - + ArrayList() } } 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 index d399e2386..2078c93d2 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -1,13 +1,29 @@ 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.AnalyzeUrl import retrofit2.Response class BookList { - fun analyzeBookList(response: Response, bookSource: BookSource): ArrayList { + @Throws(Exception::class) + fun analyzeBookList( + response: Response, + bookSource: BookSource, + analyzeUrl: AnalyzeUrl, + isSearch: Boolean = true + ): ArrayList { var bookList = ArrayList() + val body: String? = response.body() + body ?: throw Exception( + App.INSTANCE.getString( + R.string.get_web_content_error, + analyzeUrl.url + ) + ) return bookList } From c65aabf4f7ae8bcfbde4307c7f6aa6bd4910c095 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 12 Jul 2019 09:34:20 +0800 Subject: [PATCH 5/6] up --- app/src/main/java/io/legado/app/model/WebBook.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 34fd2caf8..545f673ec 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -11,7 +11,7 @@ import io.legado.app.model.webbook.BookList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.MainScope -class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { +class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope() { fun searchBook(key: String, page: Int?): Coroutine> { return Coroutine.with(this) { From e3caa4f81f4b406d5874bfab82e80eb0ef36e52e Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 12 Jul 2019 14:31:03 +0800 Subject: [PATCH 6/6] up --- app/src/main/java/io/legado/app/model/WebBook.kt | 2 +- app/src/main/java/io/legado/app/model/webbook/BookList.kt | 7 ++++++- app/src/main/java/io/legado/app/utils/NetworkUtils.kt | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 545f673ec..b6f4b90dc 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -33,7 +33,7 @@ class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope( } return@with BookList().analyzeBookList(response, bookSource, analyzeUrl) } - ArrayList() + return@with ArrayList() } } 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 index 2078c93d2..ee66e20c0 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -4,7 +4,9 @@ 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 { @@ -17,13 +19,16 @@ class BookList { 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, - analyzeUrl.url + baseUrl ) ) + val analyzer = AnalyzeRule(null) + analyzer.setContent(body, baseUrl) return bookList } 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)