mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 16:26:04 +00:00
Handle Guardian image enclosures that don’t use a media:group and that differ only by ?width=. Pick the widest one.
This commit is contained in:
1 parent
d3a3aa16da
commit
39cd96839e
2 files changed
+43
-3
No files matched your search
@@ -143,17 +143,30 @@ final class RSSItem {
|
||||
|
||||
/// Podcast feeds often list the same file as both `<enclosure>` 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
|
||||
|
||||
@@ -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 = """
|
||||
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>The Guardian</title><link>https://www.theguardian.com/uk</link><item><title>‘A global political awakening’: how gen Z protests are sweeping the world</title><link>https://www.theguardian.com/world/2026/nov/06/gen-z-protests-sweeping-the-world</link><description><p>From Serbia to Bangladesh and India to France, young people are pushing back against authoritarianism</p></description><guid>https://www.theguardian.com/world/2026/nov/06/gen-z-protests-sweeping-the-world</guid><media:content width="140" url="https://i.guim.co.uk/img/media/d8c802e22e952ef416894c5d0a6ecca11d13b496/459_0_4582_3666/master/4582.jpg?width=140&quality=85&auto=format&fit=max&s=92a98b53493301af4a38acb36c24d9e4"><media:credit scheme="urn:ebu">Photograph: Uros Arsic/Reuters</media:credit></media:content><media:content width="460" url="https://i.guim.co.uk/img/media/d8c802e22e952ef416894c5d0a6ecca11d13b496/459_0_4582_3666/master/4582.jpg?width=460&quality=85&auto=format&fit=max&s=696a83c3a860b507c37e09ffd47ae7c3"><media:credit scheme="urn:ebu">Photograph: Uros Arsic/Reuters</media:credit></media:content><media:content width="700" 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"><media:credit scheme="urn:ebu">Photograph: Uros Arsic/Reuters</media:credit></media:content></item></channel></rss>
|
||||
"""
|
||||
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/")
|
||||
|
||||
Reference in new issue
Block a user