From 953eeaa5b2fc0bc71965e05ea7729fa0b94a6a95 Mon Sep 17 00:00:00 2001 From: Sasikanth Miriyampalli Date: Sat, 7 Oct 2023 19:38:22 +0530 Subject: [PATCH] Only fetch png/jpeg images from the feeds --- .../dev/sasikanth/rss/reader/network/AndroidAtomParser.kt | 6 ++++++ .../dev/sasikanth/rss/reader/network/AndroidRssParser.kt | 6 ++++++ .../kotlin/dev/sasikanth/rss/reader/network/FeedParser.kt | 6 ++++++ .../dev/sasikanth/rss/reader/network/IOSAtomMapper.kt | 6 ++++++ .../kotlin/dev/sasikanth/rss/reader/network/IOSRssMapper.kt | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidAtomParser.kt b/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidAtomParser.kt index 2d9fc58dc..d29d1e09d 100644 --- a/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidAtomParser.kt +++ b/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidAtomParser.kt @@ -117,6 +117,12 @@ internal class AndroidAtomParser(private val parser: XmlPullParser, private val } ?: System.currentTimeMillis() + image?.let { + if (!FeedParser.isImagePngOrJpeg(it)) { + image = null + } + } + return PostPayload( title = FeedParser.cleanText(title).orEmpty(), link = FeedParser.cleanText(link).orEmpty(), diff --git a/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidRssParser.kt b/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidRssParser.kt index 9aa63fb1f..c09b69893 100644 --- a/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidRssParser.kt +++ b/shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/network/AndroidRssParser.kt @@ -128,6 +128,12 @@ internal class AndroidRssParser(private val parser: XmlPullParser, private val f contentParser.parseComplete(description.orEmpty()) + image?.let { + if (!FeedParser.isImagePngOrJpeg(it)) { + image = null + } + } + return PostPayload( title = FeedParser.cleanText(title).orEmpty(), link = FeedParser.cleanText(link).orEmpty(), diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedParser.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedParser.kt index b9c508614..7c021e350 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedParser.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/network/FeedParser.kt @@ -32,6 +32,8 @@ interface FeedParser { private val htmlTag = Regex("<.+?>") private val blankLine = Regex("(?m)^[ \t]*\r?\n") + private val pngRegex = Regex("\\.png$") + private val jpegRegex = Regex("\\.(jpe?g|jpg)$") val imageTags = setOf("media:content", "media:thumbnail") @@ -76,6 +78,10 @@ interface FeedParser { } } + fun isImagePngOrJpeg(url: String): Boolean { + return pngRegex.containsMatchIn(url) || jpegRegex.containsMatchIn(url) + } + private fun isAbsoluteUrl(url: String): Boolean { val pattern = """^(?:\w+:)?//""".toRegex() return pattern.containsMatchIn(url) diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSAtomMapper.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSAtomMapper.kt index 867fe45a8..417b3b15e 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSAtomMapper.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSAtomMapper.kt @@ -49,6 +49,12 @@ internal fun PostPayload.Companion.mapAtomPost( parser.parseComplete(data.orEmpty()) + imageUrl?.let { + if (!FeedParser.isImagePngOrJpeg(it)) { + imageUrl = null + } + } + return PostPayload( title = FeedParser.cleanText(atomMap["title"])!!, link = link!!, diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSRssMapper.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSRssMapper.kt index 01bcef6b4..2312766ac 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSRssMapper.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/network/IOSRssMapper.kt @@ -59,6 +59,12 @@ internal fun PostPayload.Companion.mapRssPost( contentParser.parseComplete(descriptionToParse.orEmpty()) + imageUrl?.let { + if (!FeedParser.isImagePngOrJpeg(it)) { + imageUrl = null + } + } + return PostPayload( title = FeedParser.cleanText(rssMap["title"])!!, link = FeedParser.cleanText(link)!!,