1
1
package com.funkymuse.aurora.searchresultdata
2
2
3
3
import android.content.Context
4
- import android.util.Log
5
4
import androidx.paging.PagingSource
6
5
import androidx.paging.PagingState
7
6
import com.crazylegend.collections.isNotNullOrEmpty
@@ -20,10 +19,7 @@ import it.skrape.fetcher.HttpFetcher
20
19
import it.skrape.fetcher.response
21
20
import it.skrape.fetcher.skrape
22
21
import kotlinx.coroutines.CoroutineDispatcher
23
- import kotlinx.coroutines.Dispatchers
24
22
import kotlinx.coroutines.withContext
25
- import org.jsoup.Jsoup
26
- import org.jsoup.nodes.Document
27
23
28
24
/* *
29
25
* Created by funkymuse on 5/11/21 to long live and prosper !
@@ -67,7 +63,7 @@ class SearchResultDataSource @AssistedInject constructor(
67
63
68
64
69
65
private suspend fun loadBooks (page : Int ): LoadResult .Page <Int , Book > {
70
- val list = fetch()
66
+ val list = fetch(page )
71
67
return if (list.isNullOrEmpty()) {
72
68
canNotLoadMoreContent()
73
69
} else {
@@ -77,36 +73,36 @@ class SearchResultDataSource @AssistedInject constructor(
77
73
}
78
74
}
79
75
80
- private suspend fun fetch (): List <Book > =
76
+ private suspend fun fetch (page : Int ): List <Book > =
81
77
skrape(HttpFetcher ) {
82
78
request {
83
79
timeout = DEFAULT_API_TIMEOUT
84
80
url = " $SEARCH_BASE_URL ?$REQ_CONST =${searchQuery.replace(" " , " +" )} &$SORT_QUERY =$sortQuery &$VIEW_QUERY =$VIEW_QUERY_PARAM &$RES_CONST =$PAGE_SIZE &" +
85
81
" &$COLUM_QUERY =${getFieldParamByPosition(searchInFieldsPosition)} &$SORT_TYPE =$sortType &" +
86
- " $SEARCH_WITH_MASK =${if (maskWord) SEARCH_WITH_MASK_YES else SEARCH_WITH_MASK_NO } "
87
- Log .d( " URL REQUESt " , url)
82
+ " $SEARCH_WITH_MASK =${if (maskWord) SEARCH_WITH_MASK_YES else SEARCH_WITH_MASK_NO } & " +
83
+ " $PAGE_CONST = $page "
88
84
}
89
85
response {
90
86
htmlDocument {
91
87
findAll(" table" ).asSequence().drop(2 ).map {
92
88
93
- val trs =
89
+ val elementList =
94
90
tryOrNull { it.findAll(" tr" ).filter { it.children.size >= 2 } }
95
91
?.map { it.findAll(" td" ) }?.flatten()?.map { it.children }
96
92
?.flatten()
97
93
98
- val res = if (! trs .isNullOrEmpty()) {
99
- trs .dropLast(1 ).mapNotNull {
94
+ val res = if (! elementList .isNullOrEmpty()) {
95
+ elementList .dropLast(1 ).mapNotNull {
100
96
val id = tryOrNull {
101
- trs [2 ].eachLink.values.firstOrNull()?.substringAfter(" md5=" )
97
+ elementList [2 ].eachLink.values.firstOrNull()?.substringAfter(" md5=" )
102
98
}
103
99
if (id == null ) {
104
100
null
105
101
} else {
106
102
Book (
107
- image = tryOrNull { trs [0 ].eachImage.values.firstOrNull() },
108
- title = tryOrNull { trs [2 ].text },
109
- author = tryOrNull { trs [5 ].text },
103
+ image = tryOrNull { elementList [0 ].eachImage.values.firstOrNull() },
104
+ title = tryOrNull { elementList [2 ].text },
105
+ author = tryOrNull { elementList [5 ].text },
110
106
id = id
111
107
)
112
108
}
0 commit comments