diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 53bc91dd8..a79b0e272 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -982,7 +982,7 @@ private extension FeedbinAccountDelegate { let parsedItems: [ParsedItem] = entries.map { entry in let authors = Set([ParsedAuthor(name: entry.authorName, url: entry.jsonFeed?.jsonFeedAuthor?.url, avatarURL: entry.jsonFeed?.jsonFeedAuthor?.avatarURL, emailAddress: nil)]) - return ParsedItem(syncServiceID: String(entry.articleID), uniqueID: String(entry.articleID), feedURL: String(entry.feedID), url: entry.url, externalURL: entry.jsonFeed?.jsonFeedExternalURL, title: entry.title, language: nil, contentHTML: entry.contentHTML, contentText: nil, markdown: nil, summary: entry.summary, imageURL: nil, thumbnailURL: nil, bannerImageURL: nil, datePublished: entry.parsedDatePublished, dateModified: nil, authors: authors, tags: nil, attachments: nil) + return ParsedItem(syncServiceID: String(entry.articleID), uniqueID: String(entry.articleID), feedURL: String(entry.feedID), url: entry.url, externalURL: entry.jsonFeed?.jsonFeedExternalURL, title: entry.title, language: nil, contentHTML: entry.contentHTML, contentText: nil, markdown: nil, summary: entry.summary, imageURL: entry.imageURL, thumbnailURL: entry.thumbnailURL, bannerImageURL: nil, datePublished: entry.parsedDatePublished, dateModified: nil, authors: authors, tags: nil, attachments: entry.attachments) } return Set(parsedItems) diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinEntry.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinEntry.swift index 1232ed768..c6fae9011 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinEntry.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinEntry.swift @@ -21,6 +21,8 @@ final class FeedbinEntry: Decodable, Sendable { let datePublished: String? let dateArrived: String? let jsonFeed: FeedbinEntryJSONFeed? + let images: FeedbinEntryImages? + let enclosure: FeedbinEntryEnclosure? // Feedbin dates can't be decoded by the JSONDecoding 8601 decoding strategy. Feedbin // requires a very specific date formatter to work and even then it fails occasionally. @@ -33,6 +35,21 @@ final class FeedbinEntry: Decodable, Sendable { return nil } + var imageURL: String? { + images?.originalURL ?? enclosure?.itunesImageURL + } + + var thumbnailURL: String? { + images?.thumbnail?.cdnURL + } + + var attachments: Set? { + guard let attachment = enclosure?.parsedAttachment else { + return nil + } + return [attachment] + } + enum CodingKeys: String, CodingKey { case articleID = "id" case feedID = "feed_id" @@ -44,6 +61,8 @@ final class FeedbinEntry: Decodable, Sendable { case datePublished = "published" case dateArrived = "created_at" case jsonFeed = "json_feed" + case images = "images" + case enclosure = "enclosure" } } @@ -80,3 +99,68 @@ struct FeedbinEntryJSONFeedAuthor: Decodable, Sendable { case avatarURL = "avatar" } } + +struct FeedbinEntryImages: Decodable, Sendable { + let originalURL: String? + let thumbnail: FeedbinEntryThumbnail? + + enum CodingKeys: String, CodingKey { + case originalURL = "original_url" + case thumbnail = "size_1" + } + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + originalURL = try? container.decodeIfPresent(String.self, forKey: .originalURL) + thumbnail = try? container.decodeIfPresent(FeedbinEntryThumbnail.self, forKey: .thumbnail) + } +} + +/// Feedbin’s resized copy of the image, on its CDN. +struct FeedbinEntryThumbnail: Decodable, Sendable { + let cdnURL: String? + + enum CodingKeys: String, CodingKey { + case cdnURL = "cdn_url" + } +} + +struct FeedbinEntryEnclosure: Decodable, Sendable { + let url: String? + let mimeType: String? + let length: String? + let itunesDuration: String? + let itunesImageURL: String? + + var parsedAttachment: ParsedAttachment? { + guard let url else { + return nil + } + let durationInSeconds = ParsedAttachment.durationInSeconds(itunesDuration) + return ParsedAttachment(url: url, mimeType: mimeType, title: nil, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds, width: nil, height: nil, medium: nil) + } + + private var sizeInBytes: Int? { + guard let length, let sizeInBytes = Int(length), sizeInBytes > 0 else { + return nil + } + return sizeInBytes + } + + enum CodingKeys: String, CodingKey { + case url = "enclosure_url" + case mimeType = "enclosure_type" + case length = "enclosure_length" + case itunesDuration = "itunes_duration" + case itunesImageURL = "itunes_image" + } + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + url = try? container.decodeIfPresent(String.self, forKey: .url) + mimeType = try? container.decodeIfPresent(String.self, forKey: .mimeType) + length = try? container.decodeIfPresent(String.self, forKey: .length) + itunesDuration = try? container.decodeIfPresent(String.self, forKey: .itunesDuration) + itunesImageURL = try? container.decodeIfPresent(String.self, forKey: .itunesImageURL) + } +} diff --git a/Modules/Account/Tests/AccountTests/Feedbin/FeedbinEntryTests.swift b/Modules/Account/Tests/AccountTests/Feedbin/FeedbinEntryTests.swift new file mode 100644 index 000000000..9d65387b4 --- /dev/null +++ b/Modules/Account/Tests/AccountTests/Feedbin/FeedbinEntryTests.swift @@ -0,0 +1,55 @@ +// +// FeedbinEntryTests.swift +// AccountTests +// +// Created by Brent Simmons on 10/23/26. +// + +import Foundation +import Testing +@testable import Account + +struct FeedbinEntryTests { + + @Test func imagesAndEnclosure() throws { + // Fields from the Feedbin API docs for mode=extended. + let json = """ + { + "id": 2077, + "feed_id": 135, + "title": "Objective-C Runtime Releases", + "url": "http://mjtsai.com/blog/2013/02/02/objective-c-runtime-releases/", + "author": "Michael Tsai", + "content": "

Bavarious created a GitHub repository.

", + "summary": "Bavarious created a GitHub repository.", + "published": "2013-02-03T01:00:19.000000Z", + "created_at": "2013-02-04T01:00:19.127893Z", + "images": { + "original_url": "http://example.com/image.jpg", + "size_1": { + "cdn_url": "https://images.feedbinusercontent.com/image.jpg", + "width": 542, + "height": 304 + } + }, + "enclosure": { + "enclosure_url": "http://example.com/episode.mp3", + "enclosure_type": "audio/mpeg", + "enclosure_length": "54947132", + "itunes_duration": "01:54:09", + "itunes_image": "http://example.com/artwork.jpg" + } + } + """ + let entry = try JSONDecoder().decode(FeedbinEntry.self, from: Data(json.utf8)) + + #expect(entry.imageURL == "http://example.com/image.jpg") + #expect(entry.thumbnailURL == "https://images.feedbinusercontent.com/image.jpg") + + let attachment = try #require(entry.attachments?.first) + #expect(attachment.url == "http://example.com/episode.mp3") + #expect(attachment.mimeType == "audio/mpeg") + #expect(attachment.sizeInBytes == 54947132) + #expect(attachment.durationInSeconds == 6849) + } +} diff --git a/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift b/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift index 92555b34e..54bf1f5d7 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/ParsedAttachment.swift @@ -50,6 +50,37 @@ public struct ParsedAttachment: Hashable, Sendable { } } +// MARK: - Duration + +extension ParsedAttachment { + + private static let maximumDurationComponentCount = 3 // 1:00:00 + + /// Parses plain seconds (`3600`, `3600.5`) and clock forms (`1:00:00`, `59:30`). + public static func durationInSeconds(_ s: String?) -> Int? { + guard let s else { + return nil + } + let components = s.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: ":", omittingEmptySubsequences: false) + guard components.count <= maximumDurationComponentCount else { + return nil + } + + var seconds = 0.0 + for component in components { + guard let value = Double(component), value.isFinite, value >= 0 else { + return nil + } + seconds = seconds * 60 + value + } + + guard let wholeSeconds = Int(exactly: seconds.rounded(.down)), wholeSeconds > 0 else { + return nil + } + return wholeSeconds + } +} + // MARK: - Merging extension ParsedAttachment { diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift index 73abb70a7..2aaae70b6 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/MediaRSS.swift @@ -16,7 +16,7 @@ enum MediaRSS { let mimeType = attributes.value(forNameCaseInsensitive: "type") // The spec says fileSize. Some feeds (atp.fm) use length, as in RSS enclosures. let sizeInBytes = positiveInt(attributes.value(forNameCaseInsensitive: "fileSize") ?? attributes.value(forNameCaseInsensitive: "length")) - let durationInSeconds = durationInSeconds(attributes.value(forNameCaseInsensitive: "duration")) + let durationInSeconds = ParsedAttachment.durationInSeconds(attributes.value(forNameCaseInsensitive: "duration")) let width = positiveInt(attributes.value(forNameCaseInsensitive: "width")) let height = positiveInt(attributes.value(forNameCaseInsensitive: "height")) let mediumValue = attributes.value(forNameCaseInsensitive: "medium") @@ -29,32 +29,6 @@ enum MediaRSS { attributes.value(forNameCaseInsensitive: "isDefault")?.lowercased() == "true" } - private static let maximumDurationComponentCount = 3 // 1:00:00 - - /// Parses plain seconds (`3600`, `3600.5`) and clock forms (`1:00:00`, `59:30`). - static func durationInSeconds(_ s: String?) -> Int? { - guard let s else { - return nil - } - let components = s.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: ":", omittingEmptySubsequences: false) - guard components.count <= maximumDurationComponentCount else { - return nil - } - - var seconds = 0.0 - for component in components { - guard let value = Double(component), value.isFinite, value >= 0 else { - return nil - } - seconds = seconds * 60 + value - } - - guard let wholeSeconds = Int(exactly: seconds.rounded(.down)), wholeSeconds > 0 else { - return nil - } - return wholeSeconds - } - private static func positiveInt(_ s: String?) -> Int? { guard let s, let value = Int(s.trimmingCharacters(in: .whitespacesAndNewlines)), value > 0 else { return nil diff --git a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift index 786df6c2c..12ce2daa4 100644 --- a/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift +++ b/Modules/RSParser/Sources/RSParser/Feeds/XML/RSSParser.swift @@ -269,7 +269,7 @@ private final class RSSDelegate: XMLSAXParserDelegate { return } if namespace.isITunes && localName.equals("duration") { - article.itunesDurationInSeconds = MediaRSS.durationInSeconds(parser.currentStringWithTrimmedWhitespace) + article.itunesDurationInSeconds = ParsedAttachment.durationInSeconds(parser.currentStringWithTrimmedWhitespace) return } // `source:markdown` — scripting.com's namespace, used by WordLand-generated feeds.