From f9b228538d81a26ce6b83b6d7bfada211e3d65da Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 17 Nov 2019 20:39:58 +0800 Subject: [PATCH] up --- .../io/legado/app/data/entities/BookSource.kt | 12 +++--- .../io/legado/app/data/entities/RssArticle.kt | 9 +---- .../io/legado/app/data/entities/RssSource.kt | 38 +++++++++++++++++++ .../java/io/legado/app/model/rss/RssParser.kt | 5 --- .../legado/app/model/rss/RssParserByRule.kt | 9 +---- 5 files changed, 47 insertions(+), 26 deletions(-) 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 8dc3ee71b..f0213082b 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 @@ -25,16 +25,16 @@ import javax.script.SimpleBindings indices = [(Index(value = ["bookSourceUrl"], unique = false))] ) data class BookSource( - var bookSourceName: String = "", // 名称 - var bookSourceGroup: String? = null, // 分组 + var bookSourceName: String = "", // 名称 + var bookSourceGroup: String? = null, // 分组 @PrimaryKey - var bookSourceUrl: String = "", // 地址,包括 http/https - var bookSourceType: Int = 0, // 类型,0 文本,1 音频 - var bookUrlPattern: String? = null, + var bookSourceUrl: String = "", // 地址,包括 http/https + var bookSourceType: Int = 0, // 类型,0 文本,1 音频 + var bookUrlPattern: String? = null, //详情页url正则 var customOrder: Int = 0, // 手动排序编号 var enabled: Boolean = true, // 是否启用 var enabledExplore: Boolean = true, //启用发现 - var header: String? = null, // header + var header: String? = null, var loginUrl: String? = null, // 登录地址 var lastUpdateTime: Long = 0, // 最后更新时间,用于排序 var weight: Int = 0, // 智能排序的权重 diff --git a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt index 9e7f9f7a8..484129121 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt @@ -1,7 +1,6 @@ package io.legado.app.data.entities import androidx.room.Entity -import androidx.room.Ignore @Entity( @@ -17,12 +16,6 @@ data class RssArticle( var description: String? = null, var content: String? = null, var image: String? = null, - var categories: String? = null, var read: Boolean = false, var star: Boolean = false -) { - - @Ignore - var categoryList: MutableList = mutableListOf() - -} \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 8399e039e..a5c3b34db 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -4,7 +4,15 @@ import android.os.Parcelable import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey +import io.legado.app.App +import io.legado.app.constant.AppConst +import io.legado.app.help.JsExtensions +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.getPrefString import kotlinx.android.parcel.Parcelize +import java.util.* +import javax.script.SimpleBindings @Parcelize @Entity(tableName = "rssSources", indices = [(Index(value = ["sourceUrl"], unique = false))]) @@ -25,11 +33,41 @@ data class RssSource( var ruleImage: String? = null, var ruleLink: String? = null, var ruleContent: String? = null, + var header: String? = null, var enableJs: Boolean = false, var loadWithBaseUrl: Boolean = false, var customOrder: Int = 0 ) : Parcelable { + @Throws(Exception::class) + fun getHeaderMap(): Map { + val headerMap = HashMap() + headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent + header?.let { + val header1 = when { + it.startsWith("@js:", true) -> + evalJS(it.substring(4)).toString() + it.startsWith("", true) -> + evalJS(it.substring(4, it.lastIndexOf("<"))).toString() + else -> it + } + GSON.fromJsonObject>(header1)?.let { map -> + headerMap.putAll(map) + } + } + return headerMap + } + + /** + * 执行JS + */ + @Throws(Exception::class) + private fun evalJS(jsStr: String): Any { + val bindings = SimpleBindings() + bindings["java"] = JsExtensions + return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) + } + fun equal(source: RssSource): Boolean { return equal(sourceUrl, source.sourceUrl) && equal(sourceIcon, source.sourceIcon) diff --git a/app/src/main/java/io/legado/app/model/rss/RssParser.kt b/app/src/main/java/io/legado/app/model/rss/RssParser.kt index 22dbfdf32..4dde0eded 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParser.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParser.kt @@ -40,8 +40,6 @@ object RssParser { if (insideItem) currentArticle.title = xmlPullParser.nextText().trim() xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_LINK, true) -> if (insideItem) currentArticle.link = xmlPullParser.nextText().trim() - xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CATEGORY, true) -> - if (insideItem) currentArticle.categoryList.add(xmlPullParser.nextText().trim()) xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_THUMBNAIL, true) -> if (insideItem) currentArticle.image = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL) @@ -89,7 +87,6 @@ object RssParser { ) { // The item is correctly parsed insideItem = false - currentArticle.categories = currentArticle.categoryList.joinToString(",") currentArticle.origin = sourceUrl articleList.add(currentArticle) currentArticle = RssArticle() @@ -104,8 +101,6 @@ object RssParser { Debug.log(sourceUrl, "└${item.title}") Debug.log(sourceUrl, "┌获取时间") Debug.log(sourceUrl, "└${item.pubDate}") - Debug.log(sourceUrl, "┌获取类别") - Debug.log(sourceUrl, "└${item.categories}") Debug.log(sourceUrl, "┌获取描述") Debug.log(sourceUrl, "└${item.description}") Debug.log(sourceUrl, "┌获取图片url") diff --git a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt index dabfb8bb4..05cdc0daa 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt @@ -39,14 +39,13 @@ object RssParserByRule { Debug.log(sourceUrl, "└列表大小:${collections.size}") val ruleTitle = analyzeRule.splitSourceRule(rssSource.ruleTitle) val rulePubDate = analyzeRule.splitSourceRule(rssSource.rulePubDate) - val ruleCategories = analyzeRule.splitSourceRule(rssSource.ruleCategories) val ruleDescription = analyzeRule.splitSourceRule(rssSource.ruleDescription) val ruleImage = analyzeRule.splitSourceRule(rssSource.ruleImage) val ruleLink = analyzeRule.splitSourceRule(rssSource.ruleLink) for ((index, item) in collections.withIndex()) { getItem( - sourceUrl, item, analyzeRule, index == 0, ruleTitle, - rulePubDate, ruleCategories, ruleDescription, ruleImage, ruleLink + sourceUrl, item, analyzeRule, index == 0, + ruleTitle, rulePubDate, ruleDescription, ruleImage, ruleLink )?.let { it.origin = rssSource.sourceUrl articleList.add(it) @@ -69,7 +68,6 @@ object RssParserByRule { log: Boolean, ruleTitle: List, rulePubDate: List, - ruleCategories: List, ruleDescription: List, ruleImage: List, ruleLink: List @@ -82,9 +80,6 @@ object RssParserByRule { Debug.log(sourceUrl, "┌获取时间", log) rssArticle.pubDate = analyzeRule.getString(rulePubDate) Debug.log(sourceUrl, "└${rssArticle.pubDate}", log) - Debug.log(sourceUrl, "┌获取类别", log) - rssArticle.categories = analyzeRule.getString(ruleCategories) - Debug.log(sourceUrl, "└${rssArticle.categories}", log) Debug.log(sourceUrl, "┌获取描述", log) rssArticle.description = analyzeRule.getString(ruleDescription) Debug.log(sourceUrl, "└${rssArticle.description}", log)