From d8fd296e88177c8eef58d1f37c0793852301b783 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 16 Nov 2019 18:01:12 +0800 Subject: [PATCH] up --- .../java/io/legado/app/model/SourceDebug.kt | 269 ++++++++++++++++++ .../app/model/webbook/BookChapterList.kt | 1 + .../legado/app/model/webbook/BookContent.kt | 1 + .../io/legado/app/model/webbook/BookInfo.kt | 1 + .../io/legado/app/model/webbook/BookList.kt | 1 + .../legado/app/model/webbook/SourceDebug.kt | 175 ------------ .../book/source/debug/BookSourceDebugModel.kt | 5 +- .../web/controller/SourceDebugWebSocket.kt | 5 +- 8 files changed, 279 insertions(+), 179 deletions(-) create mode 100644 app/src/main/java/io/legado/app/model/SourceDebug.kt delete mode 100644 app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt diff --git a/app/src/main/java/io/legado/app/model/SourceDebug.kt b/app/src/main/java/io/legado/app/model/SourceDebug.kt new file mode 100644 index 000000000..6844045c5 --- /dev/null +++ b/app/src/main/java/io/legado/app/model/SourceDebug.kt @@ -0,0 +1,269 @@ +package io.legado.app.model + +import android.annotation.SuppressLint +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter +import io.legado.app.help.coroutine.CompositeCoroutine +import io.legado.app.utils.htmlFormat +import io.legado.app.utils.isAbsUrl +import java.text.SimpleDateFormat +import java.util.* + +object SourceDebug { + var debugSource: String? = null + var callback: Callback? = null + private val tasks: CompositeCoroutine = CompositeCoroutine() + + @SuppressLint("ConstantLocale") + private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) + private var startTime: Long = System.currentTimeMillis() + + @Synchronized + fun printLog( + sourceUrl: String?, + msg: String?, + print: Boolean = true, + isHtml: Boolean = false, + showTime: Boolean = true, + state: Int = 1 + ) { + if (debugSource != sourceUrl || callback == null || !print) return + var printMsg = msg ?: "" + if (isHtml) { + printMsg = printMsg.htmlFormat() + } + if (showTime) { + printMsg = + "${DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - startTime))} $printMsg" + } + callback?.printLog(state, printMsg) + } + + fun cancelDebug(destroy: Boolean = false) { + tasks.clear() + + if (destroy) { + debugSource = null + callback = null + } + } + + fun startDebug(webBook: WebBook, key: String) { + cancelDebug() + startTime = System.currentTimeMillis() + when { + key.isAbsUrl() -> { + val book = Book() + book.origin = webBook.sourceUrl + book.bookUrl = key + printLog( + webBook.sourceUrl, + "⇒开始访问详情页:$key" + ) + infoDebug(webBook, book) + } + key.contains("::") -> { + val url = key.substring(key.indexOf("::") + 2) + printLog( + webBook.sourceUrl, + "⇒开始访问发现页:$url" + ) + exploreDebug(webBook, url) + } + else -> { + printLog( + webBook.sourceUrl, + "⇒开始搜索关键字:$key" + ) + searchDebug(webBook, key) + } + } + } + + private fun exploreDebug(webBook: WebBook, url: String) { + printLog( + debugSource, + "︾开始解析发现页" + ) + val explore = webBook.exploreBook(url, 1) + .onSuccess { exploreBooks -> + exploreBooks?.let { + if (exploreBooks.isNotEmpty()) { + printLog( + debugSource, + "︽发现页解析完成" + ) + printLog( + debugSource, + "", + showTime = false + ) + infoDebug( + webBook, + exploreBooks[0].toBook() + ) + } else { + printLog( + debugSource, + "︽未获取到书籍", + state = -1 + ) + } + } + } + .onError { + printLog( + debugSource, + it.localizedMessage, + state = -1 + ) + } + tasks.add(explore) + } + + private fun searchDebug(webBook: WebBook, key: String) { + printLog( + debugSource, + "︾开始解析搜索页" + ) + val search = webBook.searchBook(key, 1) + .onSuccess { searchBooks -> + searchBooks?.let { + if (searchBooks.isNotEmpty()) { + printLog( + debugSource, + "︽搜索页解析完成" + ) + printLog( + debugSource, + "", + showTime = false + ) + infoDebug( + webBook, + searchBooks[0].toBook() + ) + } else { + printLog( + debugSource, + "︽未获取到书籍", + state = -1 + ) + } + } + } + .onError { + printLog( + debugSource, + it.localizedMessage, + state = -1 + ) + } + tasks.add(search) + } + + private fun infoDebug(webBook: WebBook, book: Book) { + printLog( + debugSource, + "︾开始解析详情页" + ) + val info = webBook.getBookInfo(book) + .onSuccess { + printLog( + debugSource, + "︽详情页解析完成" + ) + printLog( + debugSource, + "", + showTime = false + ) + tocDebug(webBook, book) + } + .onError { + printLog( + debugSource, + it.localizedMessage, + state = -1 + ) + } + tasks.add(info) + } + + private fun tocDebug(webBook: WebBook, book: Book) { + printLog( + debugSource, + "︾开始解析目录页" + ) + val chapterList = webBook.getChapterList(book) + .onSuccess { chapterList -> + chapterList?.let { + if (it.isNotEmpty()) { + printLog( + debugSource, + "︽目录页解析完成" + ) + printLog( + debugSource, + "", + showTime = false + ) + val nextChapterUrl = if (it.size > 1) it[1].url else null + contentDebug( + webBook, + book, + it[0], + nextChapterUrl + ) + } else { + printLog( + debugSource, + "︽目录列表为空", + state = -1 + ) + } + } + } + .onError { + printLog( + debugSource, + it.localizedMessage, + state = -1 + ) + } + tasks.add(chapterList) + } + + private fun contentDebug( + webBook: WebBook, + book: Book, + bookChapter: BookChapter, + nextChapterUrl: String? + ) { + printLog( + debugSource, + "︾开始解析正文页" + ) + val content = webBook.getContent(book, bookChapter, nextChapterUrl) + .onSuccess { + printLog( + debugSource, + "︽正文页解析完成", + state = 1000 + ) + } + .onError { + printLog( + debugSource, + it.localizedMessage, + state = -1 + ) + } + tasks.add(content) + } + + interface Callback { + fun printLog(state: Int, msg: String) + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt index 37cfd690c..437fe303a 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt @@ -7,6 +7,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.TocRule +import io.legado.app.model.SourceDebug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import kotlinx.coroutines.CoroutineScope diff --git a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt index 6e05d8e5d..9ee87e281 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt @@ -6,6 +6,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.ContentRule +import io.legado.app.model.SourceDebug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils diff --git a/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt index ff556130f..29dc3ad60 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt @@ -4,6 +4,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource +import io.legado.app.model.SourceDebug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.utils.NetworkUtils import io.legado.app.utils.htmlFormat diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt index 85f6d5424..743a2951c 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -5,6 +5,7 @@ import io.legado.app.R import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.help.BookHelp +import io.legado.app.model.SourceDebug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils diff --git a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt deleted file mode 100644 index 807d1c12e..000000000 --- a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt +++ /dev/null @@ -1,175 +0,0 @@ -package io.legado.app.model.webbook - -import android.annotation.SuppressLint -import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookChapter -import io.legado.app.help.coroutine.CompositeCoroutine -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 { - private var debugSource: String? = null - private var callback: Callback? = null - private val tasks: CompositeCoroutine = CompositeCoroutine() - - @SuppressLint("ConstantLocale") - private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) - private var startTime: Long = System.currentTimeMillis() - - @Synchronized - fun printLog( - sourceUrl: String?, - msg: String?, - print: Boolean = true, - isHtml: Boolean = false, - showTime: Boolean = true, - state: Int = 1 - ) { - if (debugSource != sourceUrl || callback == null || !print) return - var printMsg = msg ?: "" - if (isHtml) { - printMsg = printMsg.htmlFormat() - } - if (showTime) { - printMsg = - "${DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - startTime))} $printMsg" - } - callback?.printLog(state, printMsg) - } - - fun cancelDebug(destroy: Boolean = false) { - tasks.clear() - - if (destroy) { - debugSource = null - callback = null - } - } - - } - - init { - debugSource = webBook.sourceUrl - SourceDebug.callback = callback - } - - fun startDebug(key: String) { - cancelDebug() - startTime = System.currentTimeMillis() - when { - key.isAbsUrl() -> { - val book = Book() - book.origin = webBook.sourceUrl - book.bookUrl = key - printLog(webBook.sourceUrl, "⇒开始访问详情页:$key") - infoDebug(book) - } - key.contains("::") -> { - val url = key.substring(key.indexOf("::") + 2) - printLog(webBook.sourceUrl, "⇒开始访问发现页:$url") - exploreDebug(url) - } - else -> { - printLog(webBook.sourceUrl, "⇒开始搜索关键字:$key") - searchDebug(key) - } - } - } - - private fun exploreDebug(url: String) { - printLog(debugSource, "︾开始解析发现页") - val explore = webBook.exploreBook(url, 1) - .onSuccess { exploreBooks -> - exploreBooks?.let { - if (exploreBooks.isNotEmpty()) { - printLog(debugSource, "︽发现页解析完成") - printLog(debugSource, "", showTime = false) - infoDebug(exploreBooks[0].toBook()) - } else { - printLog(debugSource, "︽未获取到书籍", state = -1) - } - } - } - .onError { - printLog(debugSource, it.localizedMessage, state = -1) - } - tasks.add(explore) - } - - private fun searchDebug(key: String) { - printLog(debugSource, "︾开始解析搜索页") - val search = webBook.searchBook(key, 1) - .onSuccess { searchBooks -> - searchBooks?.let { - if (searchBooks.isNotEmpty()) { - printLog(debugSource, "︽搜索页解析完成") - printLog(debugSource, "", showTime = false) - infoDebug(searchBooks[0].toBook()) - } else { - printLog(debugSource, "︽未获取到书籍", state = -1) - } - } - } - .onError { - printLog(debugSource, it.localizedMessage, state = -1) - } - tasks.add(search) - } - - private fun infoDebug(book: Book) { - printLog(debugSource, "︾开始解析详情页") - val info = webBook.getBookInfo(book) - .onSuccess { - printLog(debugSource, "︽详情页解析完成") - printLog(debugSource, "", showTime = false) - tocDebug(book) - } - .onError { - printLog(debugSource, it.localizedMessage, state = -1) - } - tasks.add(info) - } - - private fun tocDebug(book: Book) { - printLog(debugSource, "︾开始解析目录页") - val chapterList = webBook.getChapterList(book) - .onSuccess { chapterList -> - chapterList?.let { - if (it.isNotEmpty()) { - printLog(debugSource, "︽目录页解析完成") - printLog(debugSource, "", showTime = false) - val nextChapterUrl = if (it.size > 1) it[1].url else null - contentDebug(book, it[0], nextChapterUrl) - } else { - printLog(debugSource, "︽目录列表为空", state = -1) - } - } - } - .onError { - printLog(debugSource, it.localizedMessage, state = -1) - } - tasks.add(chapterList) - } - - private fun contentDebug(book: Book, bookChapter: BookChapter, nextChapterUrl: String?) { - printLog(debugSource, "︾开始解析正文页") - val content = webBook.getContent(book, bookChapter, nextChapterUrl) - .onSuccess { - printLog(debugSource, "︽正文页解析完成", state = 1000) - } - .onError { - printLog(debugSource, it.localizedMessage, state = -1) - } - tasks.add(content) - } - - interface Callback { - fun printLog(state: Int, msg: String) - } - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt index ced985aa9..489b9fcde 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt @@ -3,8 +3,8 @@ package io.legado.app.ui.book.source.debug import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.model.SourceDebug import io.legado.app.model.WebBook -import io.legado.app.model.webbook.SourceDebug class BookSourceDebugModel(application: Application) : BaseViewModel(application), SourceDebug.Callback { @@ -30,7 +30,8 @@ class BookSourceDebugModel(application: Application) : BaseViewModel(application fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) { webBook?.let { start?.invoke() - SourceDebug(it, this).startDebug(key) + SourceDebug.callback = this + SourceDebug.startDebug(it, key) } ?: error?.invoke() } diff --git a/app/src/main/java/io/legado/app/web/controller/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/controller/SourceDebugWebSocket.kt index 999ae7872..ae50a5de1 100644 --- a/app/src/main/java/io/legado/app/web/controller/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/controller/SourceDebugWebSocket.kt @@ -5,8 +5,8 @@ import fi.iki.elonen.NanoHTTPD import fi.iki.elonen.NanoWSD import io.legado.app.App import io.legado.app.R +import io.legado.app.model.SourceDebug import io.legado.app.model.WebBook -import io.legado.app.model.webbook.SourceDebug import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import io.legado.app.utils.isJson @@ -56,7 +56,8 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : return } App.db.bookSourceDao().getBookSource(tag)?.let { - SourceDebug(WebBook(it), this).startDebug(key) + SourceDebug.callback = this + SourceDebug.startDebug(WebBook(it), key) } } }