pull/35/head^2
kunfei 5 years ago
parent b5565a96c5
commit cbb2ef24ae
  1. 2
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  2. 3
      app/src/main/java/io/legado/app/model/rss/RssParser.kt
  3. 79
      app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt
  4. 14
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt

@ -14,6 +14,8 @@ data class RssSource(
var sourceIcon: String = "", var sourceIcon: String = "",
var sourceGroup: String? = null, var sourceGroup: String? = null,
var enabled: Boolean = true, var enabled: Boolean = true,
//列表规则
var ruleArticles: String? = null,
var ruleGuid: String? = null, var ruleGuid: String? = null,
var ruleTitle: String? = null, var ruleTitle: String? = null,
var ruleAuthor: String? = null, var ruleAuthor: String? = null,

@ -11,7 +11,7 @@ import java.io.StringReader
object RssParser { object RssParser {
@Throws(XmlPullParserException::class, IOException::class) @Throws(XmlPullParserException::class, IOException::class)
fun parseXML(xml: String): MutableList<RssArticle> { fun parseXML(xml: String, sourceUrl: String): MutableList<RssArticle> {
val articleList = mutableListOf<RssArticle>() val articleList = mutableListOf<RssArticle>()
var currentArticle = RssArticle() var currentArticle = RssArticle()
@ -93,6 +93,7 @@ object RssParser {
// The item is correctly parsed // The item is correctly parsed
insideItem = false insideItem = false
currentArticle.categories = currentArticle.categoryList.joinToString(",") currentArticle.categories = currentArticle.categoryList.joinToString(",")
currentArticle.origin = sourceUrl
articleList.add(currentArticle) articleList.add(currentArticle)
currentArticle = RssArticle() currentArticle = RssArticle()
} }

@ -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<RssArticle> {
val articleList = mutableListOf<RssArticle>()
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<AnalyzeRule.SourceRule>,
ruleTitle: List<AnalyzeRule.SourceRule>,
ruleAuthor: List<AnalyzeRule.SourceRule>,
rulePubDate: List<AnalyzeRule.SourceRule>,
ruleCategories: List<AnalyzeRule.SourceRule>,
ruleDescription: List<AnalyzeRule.SourceRule>,
ruleImage: List<AnalyzeRule.SourceRule>,
ruleContent: List<AnalyzeRule.SourceRule>,
ruleLink: List<AnalyzeRule.SourceRule>
): 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
}
}

@ -4,6 +4,7 @@ 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.rss.RssParser import io.legado.app.model.rss.RssParser
import io.legado.app.model.rss.RssParserByRule
import java.net.URL import java.net.URL
@ -11,15 +12,20 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
fun loadContent(url: String, onFinally: () -> Unit) { fun loadContent(url: String, onFinally: () -> Unit) {
execute { execute {
val rssSource = App.db.rssSourceDao().getByKey(url)
val xml = URL(url).readText() val xml = URL(url).readText()
RssParser.parseXML(xml).let { if (rssSource == null || rssSource.ruleArticles.isNullOrBlank()) {
it.forEach { rssArticle -> RssParser.parseXML(xml, url).let {
rssArticle.origin = url 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 {
onFinally() onFinally()
} }
} }
} }
Loading…
Cancel
Save