|
|
|
@ -4,23 +4,26 @@ import android.annotation.SuppressLint |
|
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
|
import io.legado.app.help.coroutine.Coroutine |
|
|
|
|
import io.legado.app.model.WebBook |
|
|
|
|
import io.legado.app.utils.htmlFormat |
|
|
|
|
import io.legado.app.utils.isAbsUrl |
|
|
|
|
import java.text.SimpleDateFormat |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
class SourceDebug(private val webBook: WebBook, callback: Callback) { |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
var debugSource: String? = null |
|
|
|
|
var callback: Callback? = null |
|
|
|
|
private var debugSource: String? = null |
|
|
|
|
private var callback: Callback? = null |
|
|
|
|
private val tasks: MutableList<Coroutine<*>> = mutableListOf() |
|
|
|
|
|
|
|
|
|
@SuppressLint("ConstantLocale") |
|
|
|
|
private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) |
|
|
|
|
private val startTime: Long = System.currentTimeMillis() |
|
|
|
|
|
|
|
|
|
fun printLog(sourceUrl: String?, state: Int, msg: String, print: Boolean = true, isHtml: Boolean = false) { |
|
|
|
|
if (debugSource != sourceUrl) return |
|
|
|
|
if (!print) return |
|
|
|
|
if (debugSource != sourceUrl || callback == null || !print) return |
|
|
|
|
var printMsg = msg |
|
|
|
|
if (isHtml) { |
|
|
|
|
printMsg = printMsg.htmlFormat() |
|
|
|
@ -29,18 +32,46 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { |
|
|
|
|
String.format("%s %s", DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - startTime)), printMsg) |
|
|
|
|
callback?.printLog(state, printMsg) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun cancelDebug(destroy: Boolean = false) { |
|
|
|
|
tasks.forEach { |
|
|
|
|
if (!it.isCancelled) { |
|
|
|
|
it.cancel() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
tasks.clear() |
|
|
|
|
|
|
|
|
|
if (destroy) { |
|
|
|
|
debugSource = null |
|
|
|
|
callback = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface Callback { |
|
|
|
|
fun printLog(state: Int, msg: String) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
debugSource = webBook.sourceUrl |
|
|
|
|
SourceDebug.callback = callback |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun startDebug(key: String) { |
|
|
|
|
cancelDebug() |
|
|
|
|
with(webBook) { |
|
|
|
|
if (key.isAbsUrl()) { |
|
|
|
|
val book = Book() |
|
|
|
|
book.origin = sourceUrl |
|
|
|
|
book.bookUrl = key |
|
|
|
|
printLog(sourceUrl, 1, "开始访问$key") |
|
|
|
|
infoDebug(book) |
|
|
|
|
} else { |
|
|
|
|
printLog(sourceUrl, 1, "开始搜索关键字$key") |
|
|
|
|
searchDebug(key) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun searchDebug(key: String) { |
|
|
|
|
webBook.searchBook(key, 1) |
|
|
|
|
val search = webBook.searchBook(key, 1) |
|
|
|
|
.onSuccess { searchBooks -> |
|
|
|
|
searchBooks?.let { |
|
|
|
|
if (searchBooks.isNotEmpty()) { |
|
|
|
@ -51,20 +82,22 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { |
|
|
|
|
.onError { |
|
|
|
|
printLog(debugSource, -1, it.localizedMessage) |
|
|
|
|
} |
|
|
|
|
tasks.add(search) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun infoDebug(book: Book) { |
|
|
|
|
webBook.getBookInfo(book) |
|
|
|
|
val info = webBook.getBookInfo(book) |
|
|
|
|
.onSuccess { |
|
|
|
|
tocDebug(book) |
|
|
|
|
} |
|
|
|
|
.onError { |
|
|
|
|
printLog(debugSource, -1, it.localizedMessage) |
|
|
|
|
} |
|
|
|
|
tasks.add(info) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun tocDebug(book: Book) { |
|
|
|
|
webBook.getChapterList(book) |
|
|
|
|
val chapterList = webBook.getChapterList(book) |
|
|
|
|
.onSuccess { chapterList -> |
|
|
|
|
chapterList?.let { |
|
|
|
|
if (it.isNotEmpty()) { |
|
|
|
@ -75,10 +108,11 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { |
|
|
|
|
.onError { |
|
|
|
|
printLog(debugSource, -1, it.localizedMessage) |
|
|
|
|
} |
|
|
|
|
tasks.add(chapterList) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun contentDebug(book: Book, bookChapter: BookChapter) { |
|
|
|
|
webBook.getContent(book, bookChapter) |
|
|
|
|
val content = webBook.getContent(book, bookChapter) |
|
|
|
|
.onSuccess { content -> |
|
|
|
|
content?.let { |
|
|
|
|
printLog(debugSource, 1000, it) |
|
|
|
@ -87,6 +121,11 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { |
|
|
|
|
.onError { |
|
|
|
|
printLog(debugSource, -1, it.localizedMessage) |
|
|
|
|
} |
|
|
|
|
tasks.add(content) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface Callback { |
|
|
|
|
fun printLog(state: Int, msg: String) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun printLog( |
|
|
|
|