# Conflicts: # app/src/main/java/io/legado/app/ui/search/SearchViewModel.ktpull/32/head
						commit
						a34d66ca1f
					
				| @ -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>> | ||||||
|  | } | ||||||
| @ -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>() | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -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 | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue