校验书源改为从搜索到正文

pull/823/head
gedoor 4 years ago
parent aad94b06cd
commit cfab2bd55d
  1. 36
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  2. 67
      app/src/main/java/io/legado/app/service/help/CheckSource.kt

@ -8,9 +8,11 @@ import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.CheckSource
import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.utils.postEvent
@ -21,7 +23,7 @@ import kotlin.math.min
class CheckSourceService : BaseService() {
private var threadCount = AppConfig.threadCount
private var searchPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
private var searchCoroutine = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
private var tasks = CompositeCoroutine()
private val allIds = ArrayList<String>()
private val checkedIds = ArrayList<String>()
@ -62,7 +64,7 @@ class CheckSourceService : BaseService() {
override fun onDestroy() {
super.onDestroy()
tasks.clear()
searchPool.close()
searchCoroutine.close()
postEvent(EventBus.CHECK_SOURCE_DONE, 0)
}
@ -96,18 +98,34 @@ class CheckSourceService : BaseService() {
if (index < allIds.size) {
val sourceUrl = allIds[index]
App.db.bookSourceDao.getBookSource(sourceUrl)?.let { source ->
if (source.searchUrl.isNullOrEmpty()) {
onNext(sourceUrl, source.bookSourceName)
} else {
CheckSource(source).check(this, searchPool) {
onNext(it, source.bookSourceName)
}
}
check(source)
} ?: onNext(sourceUrl, "")
}
}
}
fun check(source: BookSource) {
execute(context = searchCoroutine) {
val webBook = WebBook(source)
val books = webBook.searchBookAwait(this, CheckSource.keyword)
val book = webBook.getBookInfoAwait(this, books.first().toBook())
val toc = webBook.getChapterListAwait(this, book)
val content = webBook.getContentAwait(this, book, toc.first())
if (content.isBlank()) {
throw Exception("正文内容为空")
}
}.timeout(60000L)
.onError {
source.addGroup("失效")
App.db.bookSourceDao.update(source)
}.onSuccess {
source.removeGroup("失效")
App.db.bookSourceDao.update(source)
}.onFinally {
onNext(source.bookSourceUrl, source.bookSourceName)
}
}
private fun onNext(sourceUrl: String, sourceName: String) {
synchronized(this) {
check()

@ -2,64 +2,35 @@ package io.legado.app.service.help
import android.content.Context
import android.content.Intent
import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CheckSourceService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.jetbrains.anko.toast
import kotlin.coroutines.CoroutineContext
class CheckSource(val source: BookSource) {
object CheckSource {
var keyword = "我的"
companion object {
var keyword = "我的"
fun start(context: Context, sources: List<BookSource>) {
if (sources.isEmpty()) {
context.toast(R.string.non_select)
return
}
val selectedIds: ArrayList<String> = arrayListOf()
sources.map {
selectedIds.add(it.bookSourceUrl)
}
Intent(context, CheckSourceService::class.java).let {
it.action = IntentAction.start
it.putExtra("selectIds", selectedIds)
context.startService(it)
}
fun start(context: Context, sources: List<BookSource>) {
if (sources.isEmpty()) {
context.toast(R.string.non_select)
return
}
fun stop(context: Context) {
Intent(context, CheckSourceService::class.java).let {
it.action = IntentAction.stop
context.startService(it)
}
val selectedIds: ArrayList<String> = arrayListOf()
sources.map {
selectedIds.add(it.bookSourceUrl)
}
Intent(context, CheckSourceService::class.java).let {
it.action = IntentAction.start
it.putExtra("selectIds", selectedIds)
context.startService(it)
}
}
fun check(
scope: CoroutineScope,
context: CoroutineContext,
onNext: (sourceUrl: String) -> Unit
): Coroutine<*> {
val webBook = WebBook(source)
return webBook
.searchBook(scope, keyword, context = context)
.timeout(60000L)
.onError(Dispatchers.IO) {
source.addGroup("失效")
App.db.bookSourceDao.update(source)
}.onSuccess(Dispatchers.IO) {
source.removeGroup("失效")
App.db.bookSourceDao.update(source)
}.onFinally(Dispatchers.IO) {
onNext(source.bookSourceUrl)
}
fun stop(context: Context) {
Intent(context, CheckSourceService::class.java).let {
it.action = IntentAction.stop
context.startService(it)
}
}
}
Loading…
Cancel
Save