From cbb2ef24aecd0f458ddd9d310915d7be5c953b0d Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 3 Oct 2019 09:40:25 +0800 Subject: [PATCH] up --- .../io/legado/app/data/entities/RssSource.kt | 2 + .../java/io/legado/app/model/rss/RssParser.kt | 3 +- .../legado/app/model/rss/RssParserByRule.kt | 79 +++++++++++++++++++ .../ui/rss/article/RssArticlesViewModel.kt | 14 +++- 4 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt 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 f15a14696..4537747a1 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 @@ -14,6 +14,8 @@ data class RssSource( var sourceIcon: String = "", var sourceGroup: String? = null, var enabled: Boolean = true, + //列表规则 + var ruleArticles: String? = null, var ruleGuid: String? = null, var ruleTitle: String? = null, var ruleAuthor: String? = null, 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 accd22de0..7363a349e 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 @@ -11,7 +11,7 @@ import java.io.StringReader object RssParser { @Throws(XmlPullParserException::class, IOException::class) - fun parseXML(xml: String): MutableList { + fun parseXML(xml: String, sourceUrl: String): MutableList { val articleList = mutableListOf() var currentArticle = RssArticle() @@ -93,6 +93,7 @@ object RssParser { // The item is correctly parsed insideItem = false currentArticle.categories = currentArticle.categoryList.joinToString(",") + currentArticle.origin = sourceUrl articleList.add(currentArticle) currentArticle = RssArticle() } 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 new file mode 100644 index 000000000..6880976ab --- /dev/null +++ b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt @@ -0,0 +1,79 @@ +package io.legado.app.model.rss + +import io.legado.app.data.entities.RssArticle +import io.legado.app.data.entities.RssSource +import io.legado.app.model.analyzeRule.AnalyzeRule +import org.xmlpull.v1.XmlPullParserException +import java.io.IOException + +object RssParserByRule { + + @Throws(XmlPullParserException::class, IOException::class) + fun parseXML(xml: String, rssSource: RssSource): MutableList { + + val articleList = mutableListOf() + + val analyzeRule = AnalyzeRule() + analyzeRule.setContent(xml) + + rssSource.ruleArticles?.let { ruleArticles -> + val collections = analyzeRule.getElements(ruleArticles) + val ruleGuid = analyzeRule.splitSourceRule(rssSource.ruleGuid ?: "") + val ruleTitle = analyzeRule.splitSourceRule(rssSource.ruleTitle ?: "") + val ruleAuthor = analyzeRule.splitSourceRule(rssSource.ruleAuthor ?: "") + 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 ruleContent = analyzeRule.splitSourceRule(rssSource.ruleContent ?: "") + val ruleLink = analyzeRule.splitSourceRule(rssSource.ruleLink ?: "") + for ((index, item) in collections.withIndex()) { + getItem( + item, + analyzeRule, + index == 0, + ruleGuid, + ruleTitle, + ruleAuthor, + rulePubDate, + ruleCategories, + ruleDescription, + ruleImage, + ruleContent, + ruleLink + ) + } + } ?: let { + return RssParser.parseXML(xml, rssSource.sourceUrl) + } + return articleList + } + + private fun getItem( + item: Any, + analyzeRule: AnalyzeRule, + printLog: Boolean, + ruleGuid: List, + ruleTitle: List, + ruleAuthor: List, + rulePubDate: List, + ruleCategories: List, + ruleDescription: List, + ruleImage: List, + ruleContent: List, + ruleLink: List + ): RssArticle? { + val rssArticle = RssArticle() + analyzeRule.setContent(item) + rssArticle.guid = analyzeRule.getString(ruleGuid) + rssArticle.title = analyzeRule.getString(ruleTitle) + rssArticle.author = analyzeRule.getString(ruleAuthor) + rssArticle.pubDate = analyzeRule.getString(rulePubDate) + rssArticle.categories = analyzeRule.getString(ruleCategories) + rssArticle.description = analyzeRule.getString(ruleDescription) + rssArticle.image = analyzeRule.getString(ruleImage) + rssArticle.content = analyzeRule.getString(ruleContent) + rssArticle.link = analyzeRule.getString(ruleLink) + return rssArticle + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index f96478bf6..39f0f22e2 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.model.rss.RssParser +import io.legado.app.model.rss.RssParserByRule import java.net.URL @@ -11,15 +12,20 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application fun loadContent(url: String, onFinally: () -> Unit) { execute { + val rssSource = App.db.rssSourceDao().getByKey(url) val xml = URL(url).readText() - RssParser.parseXML(xml).let { - it.forEach { rssArticle -> - rssArticle.origin = url + if (rssSource == null || rssSource.ruleArticles.isNullOrBlank()) { + RssParser.parseXML(xml, url).let { + App.db.rssArtivleDao().insert(*it.toTypedArray()) + } + } else { + RssParserByRule.parseXML(xml, rssSource).let { + App.db.rssArtivleDao().insert(*it.toTypedArray()) } - App.db.rssArtivleDao().insert(*it.toTypedArray()) } }.onFinally { onFinally() } } + } \ No newline at end of file