diff --git a/app/src/main/java/io/legado/app/model/Rss.kt b/app/src/main/java/io/legado/app/model/Rss.kt new file mode 100644 index 000000000..cd76e24fe --- /dev/null +++ b/app/src/main/java/io/legado/app/model/Rss.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> { + return Coroutine.async(scope, context) { + val response = AnalyzeUrl(rssSource.sourceUrl).getResponseAsync().await() + RssParserByRule.parseXML(response, rssSource) + } + } +} \ No newline at end of file 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 6880976ab..c1609c852 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 @@ -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 { - + @Throws(Exception::class) + fun parseXML(response: Response, rssSource: RssSource): MutableList { val articleList = mutableListOf() + 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) 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 b2d052c29..5d9470f71 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,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 {