From 9e0c28fcceb8477558e27d6a314f2aececf52a36 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 8 Nov 2026 22:16:07 -0800 Subject: [PATCH] =?UTF-8?q?Use=20an=20item=E2=80=99s=20only=20Gravatar=20a?= =?UTF-8?q?s=20its=20only=20author=E2=80=99s=20avatar,=20even=20when=20its?= =?UTF-8?q?=20title=20is=20the=20username=20("halmueller"=20instead=20of?= =?UTF-8?q?=20"Hal=20Mueller,"=20for=20instance).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/RSParser/Feeds/XML/RSSItem.swift | 16 ++++++++++++---- .../RSParserTests/Feeds/XML/RSSParserTests.swift | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift index 98a5876df..d03473109 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSItem.swift @@ -115,12 +115,12 @@ final class RSSItem { ) } - /// WordPress adds each post author’s Gravatar as a `media:content` titled with the author’s name. + /// WordPress adds each post author’s Gravatar as a `media:content` titled with the author’s username. /// A match becomes the author’s avatar instead of an attachment. private func authorsWithGravatars() -> (authors: Set, avatarURLs: Set) { var avatarURLs = Set() let authorsWithAvatars = Set(authors.map { author in - guard author.avatarURL == nil, let name = author.name, let avatarURL = gravatarURL(authorName: name) else { + guard author.avatarURL == nil, let avatarURL = gravatarURL(author) else { return author } avatarURLs.insert(avatarURL) @@ -129,8 +129,16 @@ final class RSSItem { return (authorsWithAvatars, avatarURLs) } - private func gravatarURL(authorName: String) -> String? { - gravatarTitlesByURL.first { $0.value.caseInsensitiveCompare(authorName) == .orderedSame }?.key + /// The Gravatar titled with the author’s name, or, with one author and one Gravatar, that Gravatar. + /// The username often differs from the name, as with “halmueller” and “Hal Mueller”. + private func gravatarURL(_ author: ParsedAuthor) -> String? { + if let name = author.name, let matchingURL = gravatarTitlesByURL.first(where: { $0.value.caseInsensitiveCompare(name) == .orderedSame })?.key { + return matchingURL + } + guard authors.count == 1, gravatarTitlesByURL.count == 1 else { + return nil + } + return gravatarTitlesByURL.first?.key } /// Podcast feeds often list the same file as both `` and `media:content`, diff --git a/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift b/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift index 977784c30..7cc627030 100644 --- a/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/Feeds/XML/RSSParserTests.swift @@ -115,6 +115,22 @@ import RSParser ]) } + @Test func gravatarTitledWithUsernameBecomesAuthorAvatar() throws { + // Hal Mueller’s Blog: the Gravatar’s media:title is the username, which differs from dc:creator. + let gravatarURL = "https://2.gravatar.com/avatar/559e1cc83bb53cbd383cae605e1d28f720da7c925008e6c24eef5e0258f36878?s=96&d=https%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&r=G" + let rss = """ + Hal Mueller's Bloghttps://halmueller.wordpress.comelectionhttps://halmueller.wordpress.com/2024/11/06/election/http://halmueller.wordpress.com/?p=148halmueller + """ + let d = ParserData(url: "https://halmueller.wordpress.com/feed/", data: Data(rss.utf8)) + let parsedFeed = try #require(try FeedParser.parse(d)) + let item = try #require(parsedFeed.items.first) + + let author = try #require(item.authors?.first) + #expect(author.name == "Hal Mueller") + #expect(author.avatarURL == gravatarURL) + #expect(item.attachments == nil) + } + @Test func feedImageEnclosureIsSkipped() throws { // Letters from an American: Substack lists the feed’s image as the enclosure of a post with no image of its own. let feedImageURL = "https://substackcdn.com/image/fetch/$s_!yKDv!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b47f775-3df7-41dd-8bc9-a64cdd72d948_600x600.png"