FeedKit
FeedKit is a Swift library for Reading and Generating RSS, Atom, and JSON feeds.
git clone https://github.com/nmdias/FeedKit.gitnmdias/FeedKit
FeedKit is a Swift library for Reading and Generating RSS, Atom, and JSON feeds.
Features
- Atom
- RSS
- JSON
- Namespaces
- Examples
- Documentation
- Unit Tests
Feed Reader
Feed reading can be made with a dedicated type, such as RSSFeed, AtomFeed and JSONFeed, or a universal type Feed.
Dedicated
When you know the type of feed to be read, use a dedicated type.
try await RSSFeed(urlString: "https://developer.apple.com/news/rss/news.rss")
Universal
When you don't know the type of feed, use the universal Feed enum type.
The Feed enum type handles RSS, Atom and JSON feeds and will determine the type of feed before reading, parsing and decoding occurs.
// Read any type of feed
let feed = try await Feed(urlString: "https://surprise.me/feed")
// Use a switch to get the resulting feed model
switch feed {
case let .atom(feed): // An AtomFeed instance
case let .rss(feed): // An RSSFeed instance
case let .json(feed): // A JSONFeed instance
}
Manual Feed Type Detection
The universal Feed enum above automatically determines the feed type, so you generally don't need to do it manually. However, using a FeedType enum can be helpful when you want to identify the type of feed without
the overhead of parsing and decoding.
Show
let feedType = try FeedType(data: data)
// Detect feed type
switch feedType {
case .rss: // RSS feed detected
case .atom: // Atom feed detected
case .json: // JSON feed detected
}
// Or feed content type
if feedType.isXML {
// XML feed detected
} else if feedType.isJson {
// JSON feed detected
}
Initializers
All feed types conform to FeedInitializable, sharing multiple common initializers. They provide a flexible way to read feeds from the most common sources.
Show
From a URL String:
init(urlString: String) async throws
From a URL, handling both local file URLs and remote URLs:
init(url: URL) async throws
From a local file URL:
init(fileURL url: URL) throws
From a remote URL:
init(remoteURL url: URL) async throws
From an XML or JSON String:
init(string: String) throws
From raw Data:
init(data: Data) throws
Feed Generator
XML
To generate an XML string for any given XML feed, create an instance of an RSSFeed or AtomFeed and populate it with the necessary data.
let feed = RSSFeed(
channel: .init(
title: "Breaking News",
link: "http://www.breakingnews.com/",
description: "Get the latest updates as they happen.",
// ...
items: [
.init(
title: "Breaking News: All Hearts are Joyful",
link: "http://breakingnews.com/2025/01/09/joyful-hearts",
description: "A heartwarming story of unity and celebration."
// ...
),
]
)
)
Then call toXMLString(formatted:) to generate the XML string.
try feed.toXMLString(formatted: true)
Output
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Breaking News</title>
<link>http://www.breakingnews.com/</link>
<description>Get the latest updates as they happen.</description>
<item>
<title>Breaking News: All Hearts are Joyful</title>
<link>http://breakingnews.com/2025/01/09/joyful-hearts</link>
<description>A heartwarming story of unity and celebration.</description>
</item>
</channel>
</rss>
JSON
In the same way, you can create an instance of a JSONFeed, populate it, and then call toJSONString(formatted:) to generate a JSON string.
try feed.toJSONString(formatted: true)
Feed Models
The RSS, Atom, and JSON feed models are highly comprehensive, especially when combined with all the supported namespaces. Below is a small preview of what’s available.
Preview
RSS
channel.title channel.link channel.description channel.language channel.copyright channel.managingEditor channel.webMaster channel.pubDate channel.lastBuildDate channel.categories channel.generator channel.docs channel.cloud channel.rating channel.ttl channel.image channel.textInput channel.skipHours channel.skipDays // ... channel.dublinCore channel.syndication channel.iTunes channel.atom // ... let item = channel.items?.first item?.title item?.link item?.description item?.author item?.categories item?.comments item?.enclosure item?.guid item?.pubDate item?.source //... item?.dublinCore item?.content item?.iTunes item?.media // ...
Atom
feed.title feed.subtitle feed.links feed.updated feed.authors feed.contributors feed.id feed.generator feed.icon feed.logo feed.rights // ... let entry = feed.entries?.first entry?.title entry?.summary entry?.authors entry?.contributors entry?.links entry?.updated entry?.categories entry?.id entry?.content entry?.published entry?.source entry?.rights // ... entry?.media entry?.youTube
JSON
feed.version feed.title feed.homePageURL feed.feedUrl feed.description feed.userComment feed.nextUrl feed.icon feed.favicon feed.author feed.expired feed.hubs // ... let item = feed.items?.first item?.id item?.url item?.externalUrl item?.title item?.contentText item?.contentHtml item?.summary item?.image item?.bannerImage item?.datePublished item?.dateModified item?.author item?.url item?.tags item?.attachments // ...
Installation
To add FeedKit to your Xcode project, follow these steps:
- Open your project in Xcode and go to the "File" menu.
- Select "Add Package Dependencies…"
- Enter the "Package URL": https://github.com/nmdias/FeedKit
- Select "Add Package"
License
FeedKit is released under the MIT license. See LICENSE for details.
more like this
veille-techno
Skill Claude Code de veille tech francophone. Agrège les flux RSS (Journal du Hacker, Human Coders News…) et produit un…