pull/32/head
Administrator 5 years ago
parent 7ca558e09f
commit f604693b64
  1. 8
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 9
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 42
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  4. 29
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -43,11 +43,13 @@ class WebBook(private val bookSource: BookSource) {
}
}
fun getChapterList(book: Book, success: (List<BookChapter>) -> Unit) {
Coroutine.async {
fun getChapterList(book: Book): Coroutine<List<BookChapter>> {
return Coroutine.async {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl)
val response = analyzeUrl.getResponseAsync().await()
BookChapterList.analyzeChapterList(this, book, response, bookSource, analyzeUrl, success)
BookChapterList.analyzeChapterList(this, book, response, bookSource, analyzeUrl)
}
}
}

@ -89,8 +89,7 @@ class AnalyzeUrl(
val matcher = pagePattern.matcher(ruleUrl)
while (matcher.find()) {
val pages =
matcher.group(1).split(",".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
matcher.group(1).splitNotBlank(",")
ruleUrl = if (page <= pages.size) {
ruleUrl.replace(matcher.group(), pages[page - 1].trim { it <= ' ' })
} else {
@ -148,7 +147,7 @@ class AnalyzeUrl(
}
when (method) {
Method.GET -> {
urlArray = url.split("\\?".toRegex())
urlArray = url.split("?")
url = urlArray[0]
if (urlArray.size > 1) {
analyzeFields(urlArray[1])
@ -179,9 +178,9 @@ class AnalyzeUrl(
@Throws(Exception::class)
private fun analyzeFields(fieldsTxt: String) {
queryStr = fieldsTxt
val queryS = fieldsTxt.split("&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val queryS = fieldsTxt.splitNotBlank("&")
for (query in queryS) {
val queryM = query.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val queryM = query.splitNotBlank("=")
val value = if (queryM.size > 1) queryM[1] else ""
if (TextUtils.isEmpty(charset)) {
if (NetworkUtils.hasUrlEncoded(value)) {

@ -10,20 +10,18 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Response
import java.util.concurrent.atomic.AtomicInteger
object BookChapterList {
fun analyzeChapterList(
suspend fun analyzeChapterList(
coroutineScope: CoroutineScope,
book: Book,
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
success: (List<BookChapter>) -> Unit
) {
analyzeUrl: AnalyzeUrl
): List<BookChapter> {
val chapterList = arrayListOf<BookChapter>()
val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body()
@ -63,35 +61,27 @@ object BookChapterList {
}
}
if (reverse) chapterList.reverse()
success(chapterList)
} else if (chapterData.nextUrl.size > 1) {
val chapterDataList = arrayListOf<ChapterData<String>>()
for (item in chapterData.nextUrl) {
if (!nextUrlList.contains(item)) {
val data = ChapterData(nextUrl = item)
chapterDataList.add(data)
}
}
val successCount = AtomicInteger(0)
for (item in chapterDataList) {
coroutineScope.launch {
val nextResponse = AnalyzeUrl(ruleUrl = item.nextUrl, book = book).getResponseAsync().await()
val nextChapterData =
analyzeChapterList(nextResponse.body() ?: "", item.nextUrl, tocRule, listRule, book)
item.chapterList = nextChapterData.chapterList
val nowCount = successCount.incrementAndGet()
if (nowCount == chapterDataList.size) {
for (newItem in chapterDataList) {
newItem.chapterList?.let {
withContext(coroutineScope.coroutineContext) {
val nextResponse = AnalyzeUrl(ruleUrl = item, book = book).getResponseAsync().await()
val nextChapterData = analyzeChapterList(
nextResponse.body() ?: "",
item,
tocRule,
listRule,
book
)
nextChapterData.chapterList?.let {
chapterList.addAll(it)
}
}
if (reverse) chapterList.reverse()
success(chapterList)
}
}
}
if (reverse) chapterList.reverse()
}
return chapterList
}

@ -8,9 +8,11 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook
import io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -25,11 +27,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
}
val c = execute {
Log.e("TAG1", "start")
val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com"
).get("http://www.baidu.com").await()
Log.e("TAG1", "result: $response")
delay(2000L)
response
@ -80,6 +88,27 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
// c.cancel()
}
launch {
val list = test()
println("size: ${list.size} $list")
}
}
suspend fun test(): MutableList<String> {
val list = mutableListOf<String>()
repeat(10) {
withContext(Dispatchers.IO) {
val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com"
).get("http://www.baidu.com").await()
list.add(response)
}
}
return list
}
}

Loading…
Cancel
Save