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