Skip to content

Commit

Permalink
Add support for parsing enclosed images in RSS feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Aug 9, 2023
1 parent 51a27d7 commit b9aa8a8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class AndroidFeedParserTest {
imageUrl = null,
date = 1684924200000
),
PostPayload(
title = "Post with enclosure image",
link = "https://example.com/fourth-post",
description = "Fourth post description.",
imageUrl = "https://example.com/enclosure-image",
date = 1684924200000
),
)
)

Expand Down Expand Up @@ -128,6 +135,13 @@ class AndroidFeedParserTest {
<pubDate>Wed, 24 May 2023 10:30:00 +0000</pubDate>
<enclosure url="https://example.com/third-post" />
</item>
<item>
<title>Post with enclosure image</title>
<description>Fourth post description.</description>
<pubDate>Wed, 24 May 2023 10:30:00 +0000</pubDate>
<enclosure url="https://example.com/fourth-post" />
<enclosure type="image/jpeg" url="https://example.com/enclosure-image" />
</item>
</channel>
</rss>
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ internal class AndroidRssParser(private val parser: XmlPullParser, private val f
name == "enclosure" && link.isNullOrBlank() -> link = readAttrText("url", parser)
name == "description" -> description = readTagText("description", parser)
name == "pubDate" -> date = readTagText("pubDate", parser)
FeedParser.imageTags.contains(name) && image.isNullOrBlank() ->
image = readAttrText("url", parser)
image.isNullOrBlank() && hasRssImageUrl(name, parser) -> image = readAttrText("url", parser)
else -> skip(parser)
}
}
Expand All @@ -101,4 +100,9 @@ internal class AndroidRssParser(private val parser: XmlPullParser, private val f
date = dateLong
)
}

private fun hasRssImageUrl(name: String, parser: XmlPullParser) =
(FeedParser.imageTags.contains(name) ||
(name == "enclosure" && parser.getAttributeValue(namespace, "type") == "image/jpeg")) &&
!parser.getAttributeValue(namespace, "url").isNullOrBlank()
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private class IOSXmlFeedParser(
currentElement = didStartElement

when {
hasRssImageUrl(attributes) -> {
!currentItemData.containsKey("imageUrl") && hasRssImageUrl(attributes) -> {
currentItemData["imageUrl"] = attributes["url"] as String
}
hasPodcastRssUrl() -> {
Expand Down Expand Up @@ -123,8 +123,8 @@ private class IOSXmlFeedParser(
currentElement == "enclosure" && currentItemData["link"].isNullOrBlank()

private fun hasRssImageUrl(attributes: Map<Any?, *>) =
imageTags.contains(currentElement) &&
!currentItemData.containsKey("imageUrl") &&
(imageTags.contains(currentElement) ||
(currentElement == "enclosure" && attributes["type"] == "image/jpeg")) &&
attributes.containsKey("url")

private fun PostPayload.Companion.mapRssPost(rssMap: Map<String, String>): PostPayload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class IOSFeedParserTest {
imageUrl = null,
date = 1684924200000
),
PostPayload(
title = "Post with enclosure image",
link = "https://example.com/fourth-post",
description = "Fourth post description.",
imageUrl = "https://example.com/enclosure-image",
date = 1684924200000
),
)
)

Expand Down Expand Up @@ -85,6 +92,13 @@ class IOSFeedParserTest {
<pubDate>Wed, 24 May 2023 10:30:00 +0000</pubDate>
<enclosure url="https://example.com/third-post" />
</item>
<item>
<title>Post with enclosure image</title>
<description>Fourth post description.</description>
<pubDate>Wed, 24 May 2023 10:30:00 +0000</pubDate>
<enclosure url="https://example.com/fourth-post" />
<enclosure type="image/jpeg" url="https://example.com/enclosure-image" />
</item>
</channel>
</rss>
"""
Expand Down

0 comments on commit b9aa8a8

Please sign in to comment.