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
import io.legado.app.App
import io.legado.app.R
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
import retrofit2.Response
object RssParserByRule {
@Throws(XmlPullParserException::class, IOException::class)
fun parseXML(xml: String, rssSource: RssSource): MutableList<RssArticle> {
@Throws(Exception::class)
fun parseXML(response: Response<String>, rssSource: RssSource): MutableList<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()
analyzeRule.setContent(xml)

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

Loading…
Cancel
Save