pull/35/head^2
kunfei 5 years ago
parent 8de1ff85ab
commit fa1c8ce18e
  1. 24
      app/src/main/java/io/legado/app/model/Rss.kt
  2. 20
      app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt
  3. 23
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt

@ -0,0 +1,24 @@
package io.legado.app.model
import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.RssParserByRule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
object Rss {
fun getArticles(
rssSource: RssSource,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<MutableList<RssArticle>> {
return Coroutine.async(scope, context) {
val response = AnalyzeUrl(rssSource.sourceUrl).getResponseAsync().await()
RssParserByRule.parseXML(response, rssSource)
}
}
}

@ -1,18 +1,28 @@
package io.legado.app.model.rss package io.legado.app.model.rss
import io.legado.app.App
import io.legado.app.R
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import org.xmlpull.v1.XmlPullParserException import retrofit2.Response
import java.io.IOException
object RssParserByRule { object RssParserByRule {
@Throws(XmlPullParserException::class, IOException::class) @Throws(Exception::class)
fun parseXML(xml: String, rssSource: RssSource): MutableList<RssArticle> { fun parseXML(response: Response<String>, rssSource: RssSource): MutableList<RssArticle> {
val articleList = mutableListOf<RssArticle>() val articleList = mutableListOf<RssArticle>()
val xml = response.body()
if (xml.isNullOrBlank()) {
throw Exception(
App.INSTANCE.getString(
R.string.error_get_web_content,
rssSource.sourceUrl
)
)
}
val analyzeRule = AnalyzeRule() val analyzeRule = AnalyzeRule()
analyzeRule.setContent(xml) analyzeRule.setContent(xml)

@ -4,9 +4,8 @@ import android.app.Application
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
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.analyzeRule.AnalyzeUrl import io.legado.app.data.entities.RssSource
import io.legado.app.model.rss.RssParser import io.legado.app.model.Rss
import io.legado.app.model.rss.RssParserByRule
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
@ -15,19 +14,15 @@ 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) var rssSource = App.db.rssSourceDao().getByKey(url)
rssSource?.let { if (rssSource == null) {
rssSource = RssSource(sourceUrl = url)
} else {
titleLiveData.postValue(rssSource.sourceName) titleLiveData.postValue(rssSource.sourceName)
} }
AnalyzeUrl(url).getResponseAsync().await().body()?.let { xml -> Rss.getArticles(rssSource, this).onSuccess {
if (rssSource == null || rssSource.ruleArticles.isNullOrBlank()) { it?.let {
RssParser.parseXML(xml, url).let { App.db.rssArtivleDao().insert(*it.toTypedArray())
App.db.rssArtivleDao().insert(*it.toTypedArray())
}
} else {
RssParserByRule.parseXML(xml, rssSource).let {
App.db.rssArtivleDao().insert(*it.toTypedArray())
}
} }
} }
}.onFinally { }.onFinally {

Loading…
Cancel
Save