pull/32/head
kunfei 5 years ago
parent 0c7a81324a
commit b081d83f67
  1. 13
      app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt
  2. 16
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt
  3. 20
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 14
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt

@ -1,6 +1,7 @@
package io.legado.app.data.api package io.legado.app.data.api
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import retrofit2.Call
import retrofit2.Response import retrofit2.Response
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.HeaderMap import retrofit2.http.HeaderMap
@ -26,4 +27,16 @@ interface IHttpGetApi {
@HeaderMap headers: Map<String, String> @HeaderMap headers: Map<String, String>
): Deferred<Response<String>> ): Deferred<Response<String>>
@GET
fun get(
@Url url: String,
@HeaderMap headers: Map<String, String>
): Call<String>
@GET
fun getMap(
@Url url: String,
@QueryMap(encoded = true) queryMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Call<String>
} }

@ -2,6 +2,7 @@ package io.legado.app.data.api
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import okhttp3.RequestBody import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response import retrofit2.Response
import retrofit2.http.* import retrofit2.http.*
@ -26,4 +27,19 @@ interface IHttpPostApi {
@Body body: RequestBody, @Body body: RequestBody,
@HeaderMap headers: Map<String, String> @HeaderMap headers: Map<String, String>
): Deferred<Response<String>> ): Deferred<Response<String>>
@FormUrlEncoded
@POST
fun postMap(
@Url url: String,
@FieldMap(encoded = true) fieldMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Call<String>
@POST
fun postBody(
@Url url: String,
@Body body: RequestBody,
@HeaderMap headers: Map<String, String>
): Call<String>
} }

@ -14,6 +14,7 @@ import kotlinx.coroutines.Deferred
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.MediaType import okhttp3.MediaType
import okhttp3.RequestBody import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response import retrofit2.Response
import java.net.URLEncoder import java.net.URLEncoder
import java.util.* import java.util.*
@ -212,7 +213,7 @@ class AnalyzeUrl(
fun getResponseAsync(): Deferred<Response<String>> { fun getResponseAsync(): Deferred<Response<String>> {
return when { return when {
method == AnalyzeUrl.Method.POST -> HttpHelper.getApiService<IHttpPostApi>( method == Method.POST -> HttpHelper.getApiService<IHttpPostApi>(
baseUrl baseUrl
).postBodyAsync( ).postBodyAsync(
url, url,
@ -226,4 +227,21 @@ class AnalyzeUrl(
.getMapAsync(url, fieldMap, headerMap) .getMapAsync(url, fieldMap, headerMap)
} }
} }
fun getResponse(): Call<String> {
return when {
method == Method.POST -> HttpHelper.getApiService<IHttpPostApi>(
baseUrl
).postBody(
url,
body,
headerMap
)
fieldMap.isEmpty() -> HttpHelper.getApiService<IHttpGetApi>(
baseUrl
).get(url, headerMap)
else -> HttpHelper.getApiService<IHttpGetApi>(baseUrl)
.getMap(url, fieldMap, headerMap)
}
}
} }

@ -29,16 +29,26 @@ class BookChapterList {
) )
val tocRule = bookSource.getTocRule() val tocRule = bookSource.getTocRule()
val nextUrlList = arrayListOf(baseUrl) val nextUrlList = arrayListOf(baseUrl)
val chapterData = analyzeChapterList(body, tocRule) var chapterData = analyzeChapterList(body, tocRule)
chapterList.addAll(chapterData.chapterList) chapterList.addAll(chapterData.chapterList)
if (chapterData.nextUrlList.size == 1) { if (chapterData.nextUrlList.size == 1) {
var nextUrl = chapterData.nextUrlList[0] var nextUrl = chapterData.nextUrlList[0]
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
nextUrlList.add(nextUrl) nextUrlList.add(nextUrl)
AnalyzeUrl(ruleUrl = nextUrl, book = book).getResponse().execute()
.body()?.let {
chapterData = analyzeChapterList(it, tocRule)
nextUrl = if (chapterData.nextUrlList.isEmpty()) {
""
} else {
chapterData.nextUrlList[0]
}
chapterList.addAll(chapterData.chapterList)
} }
} }
} else if (chapterData.nextUrlList.size > 1) {
}
return chapterList return chapterList
} }

Loading…
Cancel
Save