Use an item’s only Gravatar as its only author’s avatar, even when its title is the username ("halmueller" instead of "Hal Mueller," for instance).

This commit is contained in:
Brent Simmons committed 2026-11-08 22:16:07 -08:00
1 parent 898c44f961
commit 9e0c28fcce
2 files changed
+28 -4

No files matched your search

@@ -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<ParsedAuthor>, avatarURLs: Set<String>) {
var avatarURLs = Set<String>()
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 `<enclosure>` and `media:content`,
@@ -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 = """
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Hal Mueller&#039;s Blog</title><link>https://halmueller.wordpress.com</link><item><title>election</title><link>https://halmueller.wordpress.com/2024/11/06/election/</link><dc:creator><![CDATA[Hal Mueller]]></dc:creator><guid isPermaLink="false">http://halmueller.wordpress.com/?p=148</guid><description><![CDATA[Really, people? You can&#8217;t vote for a woman????]]></description><media:content url="https://2.gravatar.com/avatar/559e1cc83bb53cbd383cae605e1d28f720da7c925008e6c24eef5e0258f36878?s=96&#38;d=https%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image"><media:title type="html">halmueller</media:title></media:content></item></channel></rss>
"""
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"