pull/48/head
kunfei 5 years ago
parent 41388f8a20
commit d8fd296e88
  1. 269
      app/src/main/java/io/legado/app/model/SourceDebug.kt
  2. 1
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  3. 1
      app/src/main/java/io/legado/app/model/webbook/BookContent.kt
  4. 1
      app/src/main/java/io/legado/app/model/webbook/BookInfo.kt
  5. 1
      app/src/main/java/io/legado/app/model/webbook/BookList.kt
  6. 175
      app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt
  7. 5
      app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt
  8. 5
      app/src/main/java/io/legado/app/web/controller/SourceDebugWebSocket.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)
}
}

@ -7,6 +7,7 @@ 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.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.TocRule 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.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope

@ -6,6 +6,7 @@ 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.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.ContentRule 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.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils

@ -4,6 +4,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.model.SourceDebug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat

@ -5,6 +5,7 @@ import io.legado.app.R
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.BookHelp 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.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils

@ -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)
}
}

@ -3,8 +3,8 @@ package io.legado.app.ui.book.source.debug
import android.app.Application import android.app.Application
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.model.SourceDebug
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.webbook.SourceDebug
class BookSourceDebugModel(application: Application) : BaseViewModel(application), class BookSourceDebugModel(application: Application) : BaseViewModel(application),
SourceDebug.Callback { SourceDebug.Callback {
@ -30,7 +30,8 @@ class BookSourceDebugModel(application: Application) : BaseViewModel(application
fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) { fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) {
webBook?.let { webBook?.let {
start?.invoke() start?.invoke()
SourceDebug(it, this).startDebug(key) SourceDebug.callback = this
SourceDebug.startDebug(it, key)
} ?: error?.invoke() } ?: error?.invoke()
} }

@ -5,8 +5,8 @@ import fi.iki.elonen.NanoHTTPD
import fi.iki.elonen.NanoWSD import fi.iki.elonen.NanoWSD
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.model.SourceDebug
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.webbook.SourceDebug
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.isJson import io.legado.app.utils.isJson
@ -56,7 +56,8 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
return return
} }
App.db.bookSourceDao().getBookSource(tag)?.let { App.db.bookSourceDao().getBookSource(tag)?.let {
SourceDebug(WebBook(it), this).startDebug(key) SourceDebug.callback = this
SourceDebug.startDebug(WebBook(it), key)
} }
} }
} }

Loading…
Cancel
Save