Merge remote-tracking branch 'origin/master' into desirepath

# Conflicts:
#	app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt
pull/32/head
Administrator 5 years ago
commit a34d66ca1f
  1. 2
      app/build.gradle
  2. 27
      app/src/main/java/io/legado/app/data/api/IHttpGetApi.java
  3. 29
      app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt
  4. 32
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.java
  5. 29
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt
  6. 55
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  7. 41
      app/src/main/java/io/legado/app/model/WebBook.kt
  8. 22
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  9. 6
      app/src/main/java/io/legado/app/model/book/All.kt
  10. 5
      app/src/main/java/io/legado/app/model/book/BookChapterList.kt
  11. 5
      app/src/main/java/io/legado/app/model/book/BookContent.kt
  12. 5
      app/src/main/java/io/legado/app/model/book/BookInfo.kt
  13. 12
      app/src/main/java/io/legado/app/model/book/BookList.kt
  14. 5
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  15. 5
      app/src/main/java/io/legado/app/model/webbook/BookContent.kt
  16. 5
      app/src/main/java/io/legado/app/model/webbook/BookInfo.kt
  17. 35
      app/src/main/java/io/legado/app/model/webbook/BookList.kt
  18. 6
      app/src/main/java/io/legado/app/utils/NetworkUtils.kt
  19. 4
      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

@ -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<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,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<String, String>
): Deferred<Response<String>>
@GET
fun getMap(
@Url url: String,
@QueryMap(encoded = true) queryMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
}

@ -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<Response<String>> postMap(@Url String url,
@FieldMap(encoded = true) Map<String, String> fieldMap,
@HeaderMap Map<String, String> headers);
@POST
Deferred<Response<String>> postBody(@Url String url,
@Body RequestBody body,
@HeaderMap Map<String, String> headers);
}

@ -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<String, String>,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
@POST
fun postBody(
@Url url: String,
@Body body: RequestBody,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
}

@ -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<String, String> {
val headerMap = HashMap<String, String>()
@ -50,23 +71,43 @@ data class BookSource(
}
fun getSearchRule(): SearchRule? {
return GSON.fromJsonObject<SearchRule>(ruleSearch)
fun getSearchRule(): SearchRule {
searchRuleV?:let {
searchRuleV = GSON.fromJsonObject<SearchRule>(ruleSearch)
searchRuleV?:let { searchRuleV = SearchRule() }
}
return searchRuleV!!
}
fun getExploreRule(): ExploreRule? {
return GSON.fromJsonObject<ExploreRule>(ruleExplore)
fun getExploreRule(): ExploreRule {
exploreRuleV?:let {
exploreRuleV = GSON.fromJsonObject<ExploreRule>(ruleExplore)
exploreRuleV?:let { exploreRuleV = ExploreRule() }
}
return exploreRuleV!!
}
fun getBookInfoRule(): BookInfoRule? {
return GSON.fromJsonObject<BookInfoRule>(ruleBookInfo)
bookInfoRuleV?:let {
bookInfoRuleV = GSON.fromJsonObject<BookInfoRule>(ruleBookInfo)
bookInfoRuleV?:let { bookInfoRuleV = BookInfoRule() }
}
return bookInfoRuleV!!
}
fun getTocRule(): TocRule? {
return GSON.fromJsonObject<TocRule>(ruleToc)
tocRuleV?:let {
tocRuleV = GSON.fromJsonObject<TocRule>(ruleToc)
tocRuleV?:let { tocRuleV = TocRule() }
}
return tocRuleV!!
}
fun getContentRule(): ContentRule? {
return GSON.fromJsonObject<ContentRule>(ruleContent)
contentRuleV?:let {
contentRuleV = GSON.fromJsonObject<ContentRule>(ruleContent)
contentRuleV?:let { contentRuleV = ContentRule() }
}
return contentRuleV!!
}
}

@ -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<List<SearchBook>> {
return Coroutine.with(this) {
bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(searchUrl)
val response = when {
analyzeUrl.method == AnalyzeUrl.Method.POST -> HttpHelper.getApiService<IHttpPostApi>(
analyzeUrl.baseUrl
).postBody(
analyzeUrl.url,
analyzeUrl.body,
analyzeUrl.headerMap
).await()
analyzeUrl.fieldMap.isEmpty() -> HttpHelper.getApiService<IHttpGetApi>(
analyzeUrl.baseUrl
)[analyzeUrl.url, analyzeUrl.headerMap].await()
else -> HttpHelper.getApiService<IHttpGetApi>(analyzeUrl.baseUrl)
.getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await()
}
return@with BookList().analyzeBookList(response, bookSource, analyzeUrl)
}
return@with ArrayList<SearchBook>()
}
}
}

@ -27,7 +27,7 @@ class AnalyzeUrl(
key: String? = null,
page: Int? = null,
headerMapF: Map<String, String>? = 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<String, String>()
private val headerMap = HashMap<String, String>()
val fieldMap = LinkedHashMap<String, String>()
val headerMap = HashMap<String, String>()
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<Map<String, String>>(urlArray[1])
options?.let {
@ -155,6 +159,8 @@ class AnalyzeUrl(
builder.add(item.key, item.value)
body = builder.build()
}
} ?: let {
body = FormBody.Builder().build()
}
}
}

@ -1,6 +0,0 @@
package io.legado.app.model.book
class All {
}

@ -1,5 +0,0 @@
package io.legado.app.model.book
class BookChapterList {
}

@ -1,5 +0,0 @@
package io.legado.app.model.book
class BookContent {
}

@ -1,5 +0,0 @@
package io.legado.app.model.book
class BookInfo {
}

@ -1,12 +0,0 @@
package io.legado.app.model.book
import io.legado.app.data.entities.SearchBook
class BookList {
fun analyzeBookList(): ArrayList<SearchBook> {
var bookList = ArrayList<SearchBook>()
return bookList
}
}

@ -0,0 +1,5 @@
package io.legado.app.model.webbook
class BookChapterList {
}

@ -0,0 +1,5 @@
package io.legado.app.model.webbook
class BookContent {
}

@ -0,0 +1,5 @@
package io.legado.app.model.webbook
class BookInfo {
}

@ -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<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
isSearch: Boolean = true
): ArrayList<SearchBook> {
var bookList = ArrayList<SearchBook>()
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
}
}

@ -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)

@ -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

Loading…
Cancel
Save