mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-29 00:31:14 +00:00
Parse enclosures and articles images from Reader API accounts.
This commit is contained in:
1 parent
c9d52b859c
commit
d3d52a72ef
4 files changed
+102
-3
No files matched your search
@@ -1153,14 +1153,14 @@ private extension ReaderAPIAccountDelegate {
|
||||
contentText: nil,
|
||||
markdown: nil,
|
||||
summary: entry.summary.content,
|
||||
imageURL: nil,
|
||||
imageURL: entry.imageURL,
|
||||
thumbnailURL: nil,
|
||||
bannerImageURL: nil,
|
||||
datePublished: entry.parseDatePublished(),
|
||||
dateModified: nil,
|
||||
authors: authors,
|
||||
tags: nil,
|
||||
attachments: nil)
|
||||
attachments: entry.attachments)
|
||||
}
|
||||
|
||||
return Set(parsedItems)
|
||||
|
||||
@@ -59,6 +59,16 @@ struct ReaderAPIEntry: Codable, Sendable {
|
||||
let alternates: [ReaderAPIAlternateLocation]?
|
||||
let categories: [String]
|
||||
let origin: ReaderAPIEntryOrigin
|
||||
let enclosures: [ReaderAPIEnclosure]?
|
||||
|
||||
var attachments: Set<ParsedAttachment>? {
|
||||
let attachments = enclosures?.compactMap { $0.parsedAttachment } ?? []
|
||||
return attachments.isEmpty ? nil : Set(attachments)
|
||||
}
|
||||
|
||||
var imageURL: String? {
|
||||
enclosures?.first { $0.isImage }?.url
|
||||
}
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case articleID = "id"
|
||||
@@ -71,6 +81,7 @@ struct ReaderAPIEntry: Codable, Sendable {
|
||||
case crawledTimestamp = "crawlTimeMsec"
|
||||
case origin = "origin"
|
||||
case timestampUsec = "timestampUsec"
|
||||
case enclosures = "enclosure"
|
||||
}
|
||||
|
||||
func parseDatePublished() -> Date? {
|
||||
@@ -120,6 +131,67 @@ struct ReaderAPIAlternateLocation: Codable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
/// `type` is a MIME type, or a bare `image`, `audio`, or `video` (FreshRSS).
|
||||
struct ReaderAPIEnclosure: Codable, Sendable {
|
||||
let url: String?
|
||||
let type: String?
|
||||
let length: Int?
|
||||
|
||||
var parsedAttachment: ParsedAttachment? {
|
||||
guard let url else {
|
||||
return nil
|
||||
}
|
||||
return ParsedAttachment(url: url, mimeType: mimeType, title: nil, sizeInBytes: sizeInBytes, durationInSeconds: nil, width: nil, height: nil, medium: medium)
|
||||
}
|
||||
|
||||
var isImage: Bool {
|
||||
medium == .image || mimeType?.lowercased().hasPrefix("image/") == true
|
||||
}
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case url = "href"
|
||||
case type
|
||||
case length
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
url = try? container.decodeIfPresent(String.self, forKey: .url)
|
||||
type = try? container.decodeIfPresent(String.self, forKey: .type)
|
||||
if let length = try? container.decodeIfPresent(Int.self, forKey: .length) {
|
||||
self.length = length
|
||||
} else if let lengthString = try? container.decodeIfPresent(String.self, forKey: .length) {
|
||||
self.length = Int(lengthString)
|
||||
} else {
|
||||
self.length = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension ReaderAPIEnclosure {
|
||||
|
||||
var sizeInBytes: Int? {
|
||||
guard let length, length > 0 else {
|
||||
return nil
|
||||
}
|
||||
return length
|
||||
}
|
||||
|
||||
var mimeType: String? {
|
||||
guard let type, type.contains("/") else {
|
||||
return nil
|
||||
}
|
||||
return type
|
||||
}
|
||||
|
||||
var medium: ParsedAttachment.Medium? {
|
||||
guard mimeType == nil else {
|
||||
return nil
|
||||
}
|
||||
return ParsedAttachment.Medium(mediaRSSValue: type)
|
||||
}
|
||||
}
|
||||
|
||||
struct ReaderAPIEntryOrigin: Codable, Sendable {
|
||||
let streamId: String?
|
||||
let title: String?
|
||||
|
||||
@@ -50,6 +50,33 @@ struct ReaderAPIEntryTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test func enclosures() throws {
|
||||
// From FreshRSS’s toGReader: a MIME type, or a bare medium.
|
||||
let json = """
|
||||
{
|
||||
"id": "tag:google.com,2005:reader/item/00058b10ce338909",
|
||||
"summary": {},
|
||||
"categories": [],
|
||||
"origin": {},
|
||||
"enclosure": [
|
||||
{"href": "https://example.com/episode.mp3", "type": "audio/mpeg", "length": 54947132},
|
||||
{"href": "https://example.com/cover.jpg", "type": "image"}
|
||||
]
|
||||
}
|
||||
"""
|
||||
let entry = try JSONDecoder().decode(ReaderAPIEntry.self, from: Data(json.utf8))
|
||||
|
||||
#expect(entry.imageURL == "https://example.com/cover.jpg")
|
||||
|
||||
let attachments = try #require(entry.attachments)
|
||||
let audio = try #require(attachments.first { $0.url == "https://example.com/episode.mp3" })
|
||||
#expect(audio.mimeType == "audio/mpeg")
|
||||
#expect(audio.sizeInBytes == 54947132)
|
||||
let image = try #require(attachments.first { $0.url == "https://example.com/cover.jpg" })
|
||||
#expect(image.mimeType == nil)
|
||||
#expect(image.medium == .image)
|
||||
}
|
||||
|
||||
private func makeEntry(articleID: String) throws -> ReaderAPIEntry {
|
||||
let json = "{\"id\":\"\(articleID)\",\"summary\":{},\"categories\":[],\"origin\":{}}"
|
||||
return try JSONDecoder().decode(ReaderAPIEntry.self, from: Data(json.utf8))
|
||||
|
||||
@@ -130,7 +130,7 @@ private struct MediaGroup {
|
||||
extension ParsedAttachment.Medium {
|
||||
|
||||
/// Case-insensitive. Nil for values the Media RSS spec doesn’t define.
|
||||
init?(mediaRSSValue: String?) {
|
||||
public init?(mediaRSSValue: String?) {
|
||||
guard let mediaRSSValue else {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user