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

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.AppConst
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine 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.service.help.CheckSource
import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -21,7 +23,7 @@ import kotlin.math.min
class CheckSourceService : BaseService() { class CheckSourceService : BaseService() {
private var threadCount = AppConfig.threadCount private var threadCount = AppConfig.threadCount
private var searchPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher() private var searchCoroutine = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private val allIds = ArrayList<String>() private val allIds = ArrayList<String>()
private val checkedIds = ArrayList<String>() private val checkedIds = ArrayList<String>()
@ -62,7 +64,7 @@ class CheckSourceService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
tasks.clear() tasks.clear()
searchPool.close() searchCoroutine.close()
postEvent(EventBus.CHECK_SOURCE_DONE, 0) postEvent(EventBus.CHECK_SOURCE_DONE, 0)
} }
@ -96,18 +98,34 @@ class CheckSourceService : BaseService() {
if (index < allIds.size) { if (index < allIds.size) {
val sourceUrl = allIds[index] val sourceUrl = allIds[index]
App.db.bookSourceDao.getBookSource(sourceUrl)?.let { source -> App.db.bookSourceDao.getBookSource(sourceUrl)?.let { source ->
if (source.searchUrl.isNullOrEmpty()) { check(source)
onNext(sourceUrl, source.bookSourceName)
} else {
CheckSource(source).check(this, searchPool) {
onNext(it, source.bookSourceName)
}
}
} ?: onNext(sourceUrl, "") } ?: 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) { private fun onNext(sourceUrl: String, sourceName: String) {
synchronized(this) { synchronized(this) {
check() check()

@ -2,64 +2,35 @@ package io.legado.app.service.help
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource 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 io.legado.app.service.CheckSourceService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import kotlin.coroutines.CoroutineContext
class CheckSource(val source: BookSource) { object CheckSource {
var keyword = "我的"
companion object { fun start(context: Context, sources: List<BookSource>) {
var keyword = "我的" if (sources.isEmpty()) {
context.toast(R.string.non_select)
fun start(context: Context, sources: List<BookSource>) { return
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)
}
} }
val selectedIds: ArrayList<String> = arrayListOf()
fun stop(context: Context) { sources.map {
Intent(context, CheckSourceService::class.java).let { selectedIds.add(it.bookSourceUrl)
it.action = IntentAction.stop }
context.startService(it) Intent(context, CheckSourceService::class.java).let {
} it.action = IntentAction.start
it.putExtra("selectIds", selectedIds)
context.startService(it)
} }
} }
fun check( fun stop(context: Context) {
scope: CoroutineScope, Intent(context, CheckSourceService::class.java).let {
context: CoroutineContext, it.action = IntentAction.stop
onNext: (sourceUrl: String) -> Unit context.startService(it)
): 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)
}
} }
} }
Loading…
Cancel
Save