Skip to content

Commit

Permalink
Only fetch png/jpeg images from the feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Oct 7, 2023
1 parent 1032cb5 commit 953eeaa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)!!,
Expand Down

0 comments on commit 953eeaa

Please sign in to comment.