diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 315ddf6a5..61f66fdf9 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -15,36 +15,36 @@ import kotlinx.android.parcel.Parcelize @Parcelize @Entity(tableName = "books", indices = [(Index(value = ["descUrl"], unique = true))]) data class Book( - @PrimaryKey + @PrimaryKey var descUrl: String = "", // 详情页Url(本地书源存储完整文件路径) - var tocUrl: String = "", // 目录页Url (toc=table of Contents) - var origin: String = "", // 书源URL(默认BookType.local) - var name: String? = null, // 书籍名称(书源获取) - var customName: String? = null, // 书籍名称(用户修改) - var author: String? = null, // 作者名称(书源获取) - var customAuthor: String? = null, // 作者名称(用户修改) - var tag: String? = null, // 分类信息(书源获取) - var customTag: String? = null, // 分类信息(用户修改) - var coverUrl: String? = null, // 封面Url(书源获取) - var customCoverUrl: String? = null, // 封面Url(用户修改) - var description: String? = null, // 简介内容(书源获取) - var customDescription: String? = null, // 简介内容(用户修改) - var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍) - var type: Int = 0, // @BookType - var group: Int = 0, // 自定义分组索引号 - var latestChapterTitle: String? = null, // 最新章节标题 - var latestChapterTime: Long = 0, // 最新章节标题更新时间 - var lastCheckTime: Long = 0, // 最近一次更新书籍信息的时间 - var lastCheckCount: Int = 0, // 最近一次发现新章节的数量 - var totalChapterNum: Int = 0, // 书籍目录总数 - var durChapterTitle: String? = null, // 当前章节名称 - var durChapterIndex: Int = 0, // 当前章节索引 - var durChapterPos: Int = 0, // 当前阅读的进度(首行字符的索引位置) - var durChapterTime: Long = 0, // 最近一次阅读书籍的时间(打开正文的时间) - var canUpdate: Boolean = true, // 刷新书架时更新书籍信息 - var order: Int = 0, // 手动排序 - var useReplaceRule: Boolean = true, // 正文使用净化替换规则 - var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) + var tocUrl: String = "", // 目录页Url (toc=table of Contents) + var origin: String = "", // 书源URL(默认BookType.local) + var name: String? = null, // 书籍名称(书源获取) + var customName: String? = null, // 书籍名称(用户修改) + var author: String? = null, // 作者名称(书源获取) + var customAuthor: String? = null, // 作者名称(用户修改) + var tag: String? = null, // 分类信息(书源获取) + var customTag: String? = null, // 分类信息(用户修改) + var coverUrl: String? = null, // 封面Url(书源获取) + var customCoverUrl: String? = null, // 封面Url(用户修改) + var description: String? = null, // 简介内容(书源获取) + var customDescription: String? = null, // 简介内容(用户修改) + var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍) + var type: Int = 0, // @BookType + var group: Int = 0, // 自定义分组索引号 + var latestChapterTitle: String? = null, // 最新章节标题 + var latestChapterTime: Long = 0, // 最新章节标题更新时间 + var lastCheckTime: Long = 0, // 最近一次更新书籍信息的时间 + var lastCheckCount: Int = 0, // 最近一次发现新章节的数量 + var totalChapterNum: Int = 0, // 书籍目录总数 + var durChapterTitle: String? = null, // 当前章节名称 + var durChapterIndex: Int = 0, // 当前章节索引 + var durChapterPos: Int = 0, // 当前阅读的进度(首行字符的索引位置) + var durChapterTime: Long = 0, // 最近一次阅读书籍的时间(打开正文的时间) + var canUpdate: Boolean = true, // 刷新书架时更新书籍信息 + var order: Int = 0, // 手动排序 + var useReplaceRule: Boolean = true, // 正文使用净化替换规则 + var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) ) : Parcelable, BaseBook { @IgnoredOnParcel @Ignore diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 0457113ef..ef652f3e7 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -88,7 +88,7 @@ data class BookSource( return exploreRuleV!! } - fun getBookInfoRule(): BookInfoRule? { + fun getBookInfoRule(): BookInfoRule { bookInfoRuleV ?: let { bookInfoRuleV = GSON.fromJsonObject(ruleBookInfo) bookInfoRuleV ?: let { bookInfoRuleV = BookInfoRule() } @@ -96,7 +96,7 @@ data class BookSource( return bookInfoRuleV!! } - fun getTocRule(): TocRule? { + fun getTocRule(): TocRule { tocRuleV ?: let { tocRuleV = GSON.fromJsonObject(ruleToc) tocRuleV ?: let { tocRuleV = TocRule() } @@ -104,7 +104,7 @@ data class BookSource( return tocRuleV!! } - fun getContentRule(): ContentRule? { + fun getContentRule(): ContentRule { contentRuleV ?: let { contentRuleV = GSON.fromJsonObject(ruleContent) contentRuleV ?: let { contentRuleV = ContentRule() } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index e8fc5bb81..04d846529 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -19,7 +19,7 @@ import javax.script.SimpleBindings */ @Keep class AnalyzeRule(private var book: BaseBook? = null) { - private var `object`: Any? = null + private var content: Any? = null private var isJSON: Boolean = false private var baseUrl: String? = null @@ -36,10 +36,10 @@ class AnalyzeRule(private var book: BaseBook? = null) { } @JvmOverloads - fun setContent(body: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { - if (body == null) throw AssertionError("Content cannot be null") - isJSON = body.toString().isJson() - `object` = body + fun setContent(content: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { + if (content == null) throw AssertionError("Content cannot be null") + isJSON = content.toString().isJson() + this.content = content this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) objectChangedXP = true objectChangedJS = true @@ -51,7 +51,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { * 获取XPath解析类 */ private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { - return if (o != `object`) { + return if (o != content) { AnalyzeByXPath().parse(o) } else getAnalyzeByXPath() } @@ -59,7 +59,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { private fun getAnalyzeByXPath(): AnalyzeByXPath { if (analyzeByXPath == null || objectChangedXP) { analyzeByXPath = AnalyzeByXPath() - analyzeByXPath!!.parse(`object`!!) + analyzeByXPath!!.parse(content!!) objectChangedXP = false } return analyzeByXPath as AnalyzeByXPath @@ -69,7 +69,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { * 获取JSOUP解析类 */ private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { - return if (o != `object`) { + return if (o != content) { AnalyzeByJSoup().parse(o) } else getAnalyzeByJSoup() } @@ -77,7 +77,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { private fun getAnalyzeByJSoup(): AnalyzeByJSoup { if (analyzeByJSoup == null || objectChangedJS) { analyzeByJSoup = AnalyzeByJSoup() - analyzeByJSoup!!.parse(`object`!!) + analyzeByJSoup!!.parse(content!!) objectChangedJS = false } return analyzeByJSoup as AnalyzeByJSoup @@ -87,7 +87,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { * 获取JSON解析类 */ private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { - return if (o != `object`) { + return if (o != content) { AnalyzeByJSonPath().parse(o) } else getAnalyzeByJSonPath() } @@ -95,7 +95,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { private fun getAnalyzeByJSonPath(): AnalyzeByJSonPath { if (analyzeByJSonPath == null || objectChangedJP) { analyzeByJSonPath = AnalyzeByJSonPath() - analyzeByJSonPath!!.parse(`object`!!) + analyzeByJSonPath!!.parse(content!!) objectChangedJP = false } return analyzeByJSonPath as AnalyzeByJSonPath @@ -116,7 +116,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { @Throws(Exception::class) fun getStringList(ruleList: List, isUrl: Boolean): List? { var result: Any? = null - `object`?.let { o -> + content?.let { o -> if (ruleList.isNotEmpty()) { if (ruleList.isNotEmpty()) result = o for (rule in ruleList) { @@ -184,7 +184,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { @JvmOverloads fun getString(ruleList: List, isUrl: Boolean = false): String { var result: Any? = null - `object`?.let { o -> + content?.let { o -> if (ruleList.isNotEmpty()) result = o for (rule in ruleList) { result?.let { @@ -223,7 +223,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (TextUtils.isEmpty(ruleStr)) return null var result: Any? = null val ruleList = splitSourceRule(ruleStr) - `object`?.let { o -> + content?.let { o -> if (ruleList.isNotEmpty()) result = o for (rule in ruleList) { result?.let { @@ -250,13 +250,13 @@ class AnalyzeRule(private var book: BaseBook? = null) { fun getElements(ruleStr: String): List { var result: Any? = null val ruleList = splitSourceRule(ruleStr) - `object`?.let { o -> + content?.let { o -> if (ruleList.isNotEmpty()) result = o for (rule in ruleList) { result?.let { when (rule.mode) { Mode.Js -> { - if (result == null) result = `object` + if (result == null) result = content result = evalJS(rule.rule, result) } Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) @@ -352,7 +352,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { val sb = StringBuffer(ruleStr.length) val expMatcher = EXP_PATTERN.matcher(ruleStr) while (expMatcher.find()) { - jsEval = evalJS(expMatcher.group(1), `object`) + jsEval = evalJS(expMatcher.group(1), content) if (jsEval is String) { expMatcher.appendReplacement(sb, jsEval) } else if (jsEval is Double && jsEval % 1.0 == 0.0) { 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 c75fe3abe..c7d616872 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 @@ -18,7 +18,7 @@ class BookList { analyzeUrl: AnalyzeUrl, isSearch: Boolean = true ): ArrayList { - var bookList = ArrayList() + val bookList = ArrayList() val baseUrl: String = NetworkUtils.getUrl(response) val body: String? = response.body() body ?: throw Exception( @@ -31,7 +31,7 @@ class BookList { analyzer.setContent(body, baseUrl) bookSource.bookUrlPattern?.let { if (baseUrl.matches(it.toRegex())) { - getItem()?.let { searchBook -> + getItem(analyzer, bookSource)?.let { searchBook -> searchBook.bookInfoHtml = body bookList.add(searchBook) } @@ -41,8 +41,12 @@ class BookList { return bookList } - fun getItem(): SearchBook? { + private fun getItem(analyzeRule: AnalyzeRule, bookSource: BookSource): SearchBook? { val searchBook = SearchBook() + analyzeRule.setBook(searchBook) + bookSource.getBookInfoRule().init?.let { + analyzeRule.setContent(analyzeRule.getElement(it)) + } return null }