mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 10:47:21 +00:00
Skip enclosure images that are the same as the feed image (as with Letters from an American), so they don’t become redundant timeline thumbnails or primary images.
This commit is contained in:
1 parent
6ba1b38e17
commit
d8417db7d5
7 files changed
+53
-5
No files matched your search
@@ -79,7 +79,7 @@ private final class AtomDelegate: XMLSAXParserDelegate {
|
||||
// If no logo, fall back to icon for both.
|
||||
let iconURL = logoURLString ?? iconURLString
|
||||
let faviconURL = iconURLString
|
||||
let parsedItems = Set(items.map { $0.toParsedItem(feedURL: feedURLString) })
|
||||
let parsedItems = Set(items.map { $0.toParsedItem(feedURL: feedURLString, feedImageURL: iconURL) })
|
||||
|
||||
return ParsedFeed(
|
||||
type: .atom,
|
||||
|
||||
@@ -69,7 +69,8 @@ final class RSSItem {
|
||||
return s.md5String
|
||||
}
|
||||
|
||||
func toParsedItem(feedURL: String) -> ParsedItem {
|
||||
/// Attachments at `feedImageURL` are skipped. Substack lists the feed’s image as an enclosure on posts that have no image of their own.
|
||||
func toParsedItem(feedURL: String, feedImageURL: String?) -> ParsedItem {
|
||||
// If body is empty and summary has content,
|
||||
// promote summary to body and drop summary.
|
||||
var contentHTML = body
|
||||
@@ -110,7 +111,7 @@ final class RSSItem {
|
||||
dateModified: dateModified,
|
||||
authors: authorsWithAvatars.isEmpty ? nil : authorsWithAvatars,
|
||||
tags: nil,
|
||||
attachments: mergedAttachments(attachments.filter { !avatarURLs.contains($0.url) })
|
||||
attachments: mergedAttachments(attachments.filter { !avatarURLs.contains($0.url) && $0.url != feedImageURL })
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ private final class RSSDelegate: XMLSAXParserDelegate {
|
||||
// MARK: Building output
|
||||
|
||||
func buildParsedFeed() -> ParsedFeed {
|
||||
let parsedItems = Set(items.map { $0.toParsedItem(feedURL: feedURLString) })
|
||||
let parsedItems = Set(items.map { $0.toParsedItem(feedURL: feedURLString, feedImageURL: channelImageURLString) })
|
||||
return ParsedFeed(
|
||||
type: .rss,
|
||||
title: title,
|
||||
|
||||
@@ -115,6 +115,20 @@ import RSParser
|
||||
])
|
||||
}
|
||||
|
||||
@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"
|
||||
let rss = """
|
||||
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Letters from an American</title><link>https://heathercoxrichardson.substack.com</link><image><url>\(feedImageURL)</url><title>Letters from an American</title><link>https://heathercoxrichardson.substack.com</link></image><item><title>November 8, 2026</title><link>https://heathercoxrichardson.substack.com/p/november-8-2026</link><guid isPermaLink="false">https://heathercoxrichardson.substack.com/p/november-8-2026</guid><enclosure url="\(feedImageURL)" length="0" type="image/jpeg"/><description>Since the dramatic victories of the Democrats on Tuesday, Trump has been posting increasingly provocative material on social media.</description></item></channel></rss>
|
||||
"""
|
||||
let d = ParserData(url: "https://heathercoxrichardson.substack.com/feed", data: Data(rss.utf8))
|
||||
let parsedFeed = try #require(try FeedParser.parse(d))
|
||||
let item = try #require(parsedFeed.items.first)
|
||||
|
||||
#expect(parsedFeed.iconURL == feedImageURL)
|
||||
#expect(item.attachments == nil)
|
||||
}
|
||||
|
||||
@Test func theOmniShowUniqueIDs() throws {
|
||||
let d = parserData("theomnishow", "rss", "https://theomnishow.omnigroup.com/")
|
||||
let parsedFeed = try #require(try FeedParser.parse(d))
|
||||
|
||||
@@ -42,11 +42,14 @@ extension ArticleRenderer {
|
||||
Set(HTMLImageParser.significantImageURLStrings(html: body, baseURL: baseURL, maximumCount: Int.max).compactMap(imageIdentity))
|
||||
}
|
||||
|
||||
/// True when the body has `url`, or a resized copy of the image at `url`.
|
||||
/// True when the body has `url`, a resized copy of the image at `url`, or the YouTube video the image at `url` is a still of.
|
||||
static func bodyContains(_ url: String, body: String, bodyImageIdentities: Set<String>) -> Bool {
|
||||
if bodyContainsURL(body, url) {
|
||||
return true
|
||||
}
|
||||
if ArticleRenderingSpecialCases.bodyEmbedsYouTubeVideo(stillImageURL: url, body: body) {
|
||||
return true
|
||||
}
|
||||
guard let identity = imageIdentity(url) else {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -209,6 +209,27 @@ struct ArticleRenderingSpecialCases {
|
||||
return youTubeEmbedURLPrefix + videoID
|
||||
}
|
||||
|
||||
private static let youTubeVideoIDLength = 11
|
||||
private static let youTubeEmbedPaths = ["youtube.com/embed/", "youtube-nocookie.com/embed/"]
|
||||
|
||||
/// True when `body` embeds the YouTube video that the image at `stillImageURL` is a still of.
|
||||
/// Substack lists the still as an enclosure, as in `substackcdn.com/image/youtube/w_728,c_limit/VIDEO_ID`.
|
||||
/// YouTube’s own stills are like `i.ytimg.com/vi/VIDEO_ID/hqdefault.jpg`.
|
||||
static func bodyEmbedsYouTubeVideo(stillImageURL: String, body: String) -> Bool {
|
||||
guard let url = URL(string: stillImageURL) else {
|
||||
return false
|
||||
}
|
||||
let possibleVideoIDs = url.pathComponents.filter { pathComponent in
|
||||
pathComponent.count == youTubeVideoIDLength && pathComponent.unicodeScalars.allSatisfy { youTubeVideoIDCharacters.contains($0) }
|
||||
}
|
||||
for videoID in possibleVideoIDs {
|
||||
if youTubeEmbedPaths.contains(where: { body.contains($0 + videoID) }) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private static func anyURLStringMatchesAnyDomain(_ urlStrings: [String?], _ domains: [String]) -> Bool {
|
||||
for urlString in urlStrings {
|
||||
if let urlString, SpecialCase.urlStringMatchesDomain(urlString, domains) {
|
||||
|
||||
@@ -97,6 +97,15 @@ import Articles
|
||||
#expect(ArticleRenderer.primaryImage(imageLink: nil, thumbnailLink: nil, attachments: [attachment], body: body, bodyImageIdentities: ArticleRenderer.bodyImageIdentities(body, baseURL: nil))?.url == imageURL)
|
||||
}
|
||||
|
||||
// Letters from an American’s “Amanda Litman | American Conversations”: the enclosure is Substack’s still of the YouTube video the body embeds.
|
||||
@Test func primaryImageSkippedWhenBodyEmbedsItsYouTubeVideo() throws {
|
||||
let imageURL = "https://substackcdn.com/image/youtube/w_728,c_limit/V1DtMzaVvfA"
|
||||
let attachment = try #require(Attachment(url: imageURL, mimeType: "image/jpeg", title: nil, sizeInBytes: 0, durationInSeconds: nil, width: nil, height: nil, medium: nil))
|
||||
let body = "<div id=\"youtube2-V1DtMzaVvfA\" class=\"youtube-wrap\"><div class=\"youtube-inner\"><iframe src=\"https://www.youtube-nocookie.com/embed/V1DtMzaVvfA?rel=0&autoplay=0&showinfo=0&enablejsapi=0\" frameborder=\"0\" loading=\"lazy\" width=\"728\" height=\"409\"></iframe></div></div>"
|
||||
|
||||
#expect(ArticleRenderer.primaryImage(imageLink: nil, thumbnailLink: nil, attachments: [attachment], body: body, bodyImageIdentities: ArticleRenderer.bodyImageIdentities(body, baseURL: nil)) == nil)
|
||||
}
|
||||
|
||||
// Wired: no attachments, and the full-size lead image is in media:thumbnail.
|
||||
@Test func primaryImageFallsBackToThumbnail() {
|
||||
let thumbnailURL = "https://media.wired.com/photos/6aee77f4ece1c19963602401/master/pass/gear-nyt-cooking-ai-assistant.jpg"
|
||||
|
||||
Reference in new issue
Block a user