diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift index d03473109..399f2ba57 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift @@ -143,17 +143,30 @@ final class RSSItem { /// Podcast feeds often list the same file as both `` and `media:content`, /// so attachments with the same URL are merged into one, in the place of the first. + /// Renditions of one image at different widths are merged into the widest, in the place of the first. /// `itunes:duration` applies to audio and video attachments that don’t have their own duration. private func mergedAttachments(_ attachments: [ParsedAttachment]) -> [ParsedAttachment]? { var mergedAttachments = [ParsedAttachment]() var indexesByURL = [String: Int]() + var indexesByRenditionKey = [String: Int]() for attachment in attachments { if let index = indexesByURL[attachment.url] { mergedAttachments[index] = mergedAttachments[index].filledIn(from: attachment) - } else { - indexesByURL[attachment.url] = mergedAttachments.count - mergedAttachments.append(attachment) + continue } + let renditionKey = Self.renditionKey(attachment) + if let renditionKey, let index = indexesByRenditionKey[renditionKey], let mergedWidth = mergedAttachments[index].width, let width = attachment.width { + if width > mergedWidth { + mergedAttachments[index] = attachment + } + indexesByURL[attachment.url] = index + continue + } + indexesByURL[attachment.url] = mergedAttachments.count + if let renditionKey { + indexesByRenditionKey[renditionKey] = mergedAttachments.count + } + mergedAttachments.append(attachment) } guard !mergedAttachments.isEmpty else { return nil @@ -170,6 +183,17 @@ final class RSSItem { } } + /// The URL without its query, for an attachment with a width and a query. + /// The Guardian lists each image three times, as sibling `media:content` elements that differ only in `?width=`. + private static func renditionKey(_ attachment: ParsedAttachment) -> String? { + guard attachment.width != nil, var components = URLComponents(string: attachment.url), components.query != nil else { + return nil + } + components.query = nil + components.fragment = nil + return components.string + } + private static func isAudioOrVideo(_ attachment: ParsedAttachment) -> Bool { if attachment.medium == .audio || attachment.medium == .video { return true diff --git a/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift b/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift index 7cc627030..3e04fdffc 100644 --- a/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift @@ -96,6 +96,22 @@ import RSParser #expect(attachment.durationInSeconds == 125) } + @Test func guardianImageRenditionsMergedIntoWidest() throws { + // The Guardian lists each image at widths 140, 460, and 700, as sibling media:content elements. + let rss = """ + The Guardianhttps://www.theguardian.com/uk‘A global political awakening’: how gen Z protests are sweeping the worldhttps://www.theguardian.com/world/2026/nov/06/gen-z-protests-sweeping-the-world<p>From Serbia to Bangladesh and India to France, young people are pushing back against authoritarianism</p>https://www.theguardian.com/world/2026/nov/06/gen-z-protests-sweeping-the-worldPhotograph: Uros Arsic/ReutersPhotograph: Uros Arsic/ReutersPhotograph: Uros Arsic/Reuters + """ + let d = ParserData(url: "https://www.theguardian.com/uk/rss", data: Data(rss.utf8)) + let parsedFeed = try #require(try FeedParser.parse(d)) + let item = try #require(parsedFeed.items.first) + + let attachments = try #require(item.attachments) + #expect(attachments.count == 1) + let attachment = try #require(attachments.first) + #expect(attachment.url == "https://i.guim.co.uk/img/media/d8c802e22e952ef416894c5d0a6ecca11d13b496/459_0_4582_3666/master/4582.jpg?width=700&quality=85&auto=format&fit=max&s=0e6444e5dfc451bcc781cb563d8ef201") + #expect(attachment.width == 700) + } + @Test func gravatarBecomesAuthorAvatar() throws { // WordPress adds the author’s Gravatar as a media:content titled with the author’s name. let d = parserData("postpunkmonk", "rss", "https://postpunkmonk.com/")