pull/32/head
kunfei 5 years ago
parent 2f41e23d68
commit 13526640b8
  1. 11
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 18
      app/src/main/java/io/legado/app/model/webbook/BookList.kt

@ -4,18 +4,17 @@ import io.legado.app.data.api.IHttpGetApi
import io.legado.app.data.api.IHttpPostApi import io.legado.app.data.api.IHttpPostApi
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook 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.help.http.HttpHelper
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.webbook.BookList import io.legado.app.model.webbook.BookList
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() { class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() {
fun searchBook(key: String, page: Int?, success: (() -> Unit), error: (() -> Unit)) { fun searchBook(key: String, page: Int?): Coroutine<List<SearchBook>> {
launch(IO) { return Coroutine.with(this) {
bookSource.getSearchRule().searchUrl?.let { searchUrl -> bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(searchUrl) val analyzeUrl = AnalyzeUrl(searchUrl)
val response = when { val response = when {
@ -32,9 +31,9 @@ class WebBook(val bookSource: BookSource) : CoroutineScope by MainScope() {
else -> HttpHelper.getApiService<IHttpGetApi>(analyzeUrl.baseUrl) else -> HttpHelper.getApiService<IHttpGetApi>(analyzeUrl.baseUrl)
.getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await() .getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await()
} }
val bookList = BookList().analyzeBookList(response, bookSource) return@with BookList().analyzeBookList(response, bookSource, analyzeUrl)
} }
ArrayList()
} }
} }

@ -1,13 +1,29 @@
package io.legado.app.model.webbook 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.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.model.analyzeRule.AnalyzeUrl
import retrofit2.Response import retrofit2.Response
class BookList { class BookList {
fun analyzeBookList(response: Response<String>, bookSource: BookSource): ArrayList<SearchBook> { @Throws(Exception::class)
fun analyzeBookList(
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
isSearch: Boolean = true
): ArrayList<SearchBook> {
var bookList = ArrayList<SearchBook>() var bookList = ArrayList<SearchBook>()
val body: String? = response.body()
body ?: throw Exception(
App.INSTANCE.getString(
R.string.get_web_content_error,
analyzeUrl.url
)
)
return bookList return bookList
} }

Loading…
Cancel
Save