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. 44
      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) { fun getChapterList(book: Book): Coroutine<List<BookChapter>> {
Coroutine.async { return Coroutine.async {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl) val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl)
val response = analyzeUrl.getResponseAsync().await() 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) val matcher = pagePattern.matcher(ruleUrl)
while (matcher.find()) { while (matcher.find()) {
val pages = val pages =
matcher.group(1).split(",".toRegex()).dropLastWhile { it.isEmpty() } matcher.group(1).splitNotBlank(",")
.toTypedArray()
ruleUrl = if (page <= pages.size) { ruleUrl = if (page <= pages.size) {
ruleUrl.replace(matcher.group(), pages[page - 1].trim { it <= ' ' }) ruleUrl.replace(matcher.group(), pages[page - 1].trim { it <= ' ' })
} else { } else {
@ -148,7 +147,7 @@ class AnalyzeUrl(
} }
when (method) { when (method) {
Method.GET -> { Method.GET -> {
urlArray = url.split("\\?".toRegex()) urlArray = url.split("?")
url = urlArray[0] url = urlArray[0]
if (urlArray.size > 1) { if (urlArray.size > 1) {
analyzeFields(urlArray[1]) analyzeFields(urlArray[1])
@ -179,9 +178,9 @@ class AnalyzeUrl(
@Throws(Exception::class) @Throws(Exception::class)
private fun analyzeFields(fieldsTxt: String) { private fun analyzeFields(fieldsTxt: String) {
queryStr = fieldsTxt queryStr = fieldsTxt
val queryS = fieldsTxt.split("&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() val queryS = fieldsTxt.splitNotBlank("&")
for (query in queryS) { 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 "" val value = if (queryM.size > 1) queryM[1] else ""
if (TextUtils.isEmpty(charset)) { if (TextUtils.isEmpty(charset)) {
if (NetworkUtils.hasUrlEncoded(value)) { 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.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.withContext
import retrofit2.Response import retrofit2.Response
import java.util.concurrent.atomic.AtomicInteger
object BookChapterList { object BookChapterList {
fun analyzeChapterList( suspend fun analyzeChapterList(
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
book: Book, book: Book,
response: Response<String>, response: Response<String>,
bookSource: BookSource, bookSource: BookSource,
analyzeUrl: AnalyzeUrl, analyzeUrl: AnalyzeUrl
success: (List<BookChapter>) -> Unit ): List<BookChapter> {
) {
val chapterList = arrayListOf<BookChapter>() val chapterList = arrayListOf<BookChapter>()
val baseUrl: String = NetworkUtils.getUrl(response) val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body() val body: String? = response.body()
@ -63,35 +61,27 @@ object BookChapterList {
} }
} }
if (reverse) chapterList.reverse() if (reverse) chapterList.reverse()
success(chapterList)
} else if (chapterData.nextUrl.size > 1) { } else if (chapterData.nextUrl.size > 1) {
val chapterDataList = arrayListOf<ChapterData<String>>()
for (item in chapterData.nextUrl) { for (item in chapterData.nextUrl) {
if (!nextUrlList.contains(item)) { if (!nextUrlList.contains(item)) {
val data = ChapterData(nextUrl = item) withContext(coroutineScope.coroutineContext) {
chapterDataList.add(data) val nextResponse = AnalyzeUrl(ruleUrl = item, book = book).getResponseAsync().await()
} val nextChapterData = analyzeChapterList(
} nextResponse.body() ?: "",
val successCount = AtomicInteger(0) item,
for (item in chapterDataList) { tocRule,
coroutineScope.launch { listRule,
val nextResponse = AnalyzeUrl(ruleUrl = item.nextUrl, book = book).getResponseAsync().await() book
val nextChapterData = )
analyzeChapterList(nextResponse.body() ?: "", item.nextUrl, tocRule, listRule, book) nextChapterData.chapterList?.let {
item.chapterList = nextChapterData.chapterList chapterList.addAll(it)
val nowCount = successCount.incrementAndGet()
if (nowCount == chapterDataList.size) {
for (newItem in chapterDataList) {
newItem.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.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -25,11 +27,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
val c = execute { val c = execute {
Log.e("TAG1", "start")
val response: String = HttpHelper.getApiService<CommonHttpApi>( val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com" "http://www.baidu.com"
).get("http://www.baidu.com").await() ).get("http://www.baidu.com").await()
Log.e("TAG1", "result: $response")
delay(2000L) delay(2000L)
response response
@ -80,6 +88,27 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
// c.cancel() // 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