feed - RSS 2.0 and Atom 1.0 syndication
Import with import "feed.j" as feed;. Builds and parses web syndication feeds, RSS 2.0 and Atom 1.0, through one module (there are no separate rss / atom modules). The format is chosen when you build and detected from the root element when you parse; both use the same value-semantic Feed shape.
Parsing rides the xml library (entities, CDATA, namespaces); building emits escaped XML through it. Elements are matched by namespace, not by prefix: Atom in http://www.w3.org/2005/Atom (as a default namespace or under any prefix; a <feed> outside it is not an Atom feed), and Dublin Core dc:creator under whatever prefix the feed binds http://purl.org/dc/elements/1.1/ to. Dates go through time.
import "feed.j" as feed;
def f as feed.Feed init feed.add(
feed.feed("My Blog", "https://example.org"),
feed.entry("Hello", "https://example.org/1"));
def rss as string init feed.build("rss", $f); # or "atom"
def back as feed.Feed init feed.parse($rss); # format auto-detectedRunnable: examples/modules/feed_demo.j.
The Feed, Entry and Enclosure structs
All three are value-semantic, so the builders return a fresh copy.
| Struct | Fields |
|---|---|
feed.Feed | title as string, link as string, updated as time.Time, entries as list of Entry, author as string, categories as list of string |
feed.Entry | title as string, link as string, id as string, published as time.Time, updated as time.Time, summary as string, content as string, author as string, categories as list of string, enclosure as Enclosure |
feed.Enclosure | url as string, length as int, type as string |
A field that maps to different names per format: id is RSS guid / Atom id; summary is RSS description / Atom summary; content is Atom-only (RSS carries the one description, kept in summary). author is RSS <author> / <dc:creator> (channel <managingEditor>) and Atom <author><name>; categories are RSS <category> / Atom <category term>; enclosure is the attached media file (RSS <enclosure> / Atom <link rel="enclosure">), the podcast episode's audio. An unset date is the Unix epoch and is omitted when building; an empty enclosure url means no enclosure and is likewise omitted.
Building
| Call | Returns | Notes |
|---|---|---|
feed.feed(title, link) | Feed | A new empty feed (no entries, unset date). |
feed.feedUpdated(f, t) | Feed | A copy with the feed's updated instant set. |
feed.add(f, e) | Feed | A copy with e appended to the entries. |
feed.addAll(f, entries) | Feed | A copy with every entry in the list appended, in order. |
feed.entry(title, link) | Entry | A new entry (empty id / summary / content, unset dates). |
feed.entryId(e, id) | Entry | A copy with the stable id set. |
feed.entryPublished(e, t) | Entry | A copy with the published instant set. |
feed.entryUpdated(e, t) | Entry | A copy with the updated instant set. |
feed.entrySummary(e, s) | Entry | A copy with the summary set. |
feed.entryContent(e, c) | Entry | A copy with the full content set (Atom). |
feed.entryAuthor(e, author) | Entry | A copy with the item author set. |
feed.entryCategory(e, category) | Entry | A copy with category appended to the item's tags. |
feed.entryEnclosure(e, url, length, type) | Entry | A copy with a media enclosure (podcast file) set. |
feed.feedAuthor(f, author) | Feed | A copy with the feed author set. |
feed.feedCategory(f, category) | Feed | A copy with category appended to the feed's tags. |
feed.hasEnclosure(e) | bool | Whether the entry has a media enclosure (non-empty url). |
feed.build(format, f) | string | Render to "rss" (RSS 2.0) or "atom" (Atom 1.0). An unknown format is kind "value". |
RSS uses RFC 822 dates (pubDate / lastBuildDate); Atom uses RFC 3339 (published / updated). All text is XML-escaped, so &, <, and > in titles or summaries round-trip.
Each builder returns the updated value. A call into the module rebuilds the Feed it is given and the one it returns (every value holding the module's structs is rebuilt at the module boundary), so add in a loop is O(entries^2): a few thousand adds take many seconds. Collect the entries in a list and call addAll once.
import "feed.j" as feed;
def rows as list of map of string to string init [
{"title": "One", "link": "https://example.org/1"},
{"title": "Two", "link": "https://example.org/2"}
];
def entries as list of feed.Entry init [];
for (def row in $rows) {
$entries[] = feed.entry($row["title"], $row["link"]);
}
def out as feed.Feed init feed.addAll(feed.feed("My Blog", "https://example.org"), $entries);An Atom feed and each of its entries should carry an id and an updated date (RFC 4287); build omits whichever is unset, and Feed has no id. Set link and dates if the output must validate.
Podcasts and metadata
entryEnclosure attaches the media file that makes a feed a podcast: RSS emits <enclosure url length type/>, Atom <link rel="enclosure" href length type/>, and parse reads either back into the Enclosure struct. The item's own page link (Atom's alternate <link>) stays separate from the enclosure, so entry.link is the episode page and entry.enclosure.url the audio. author and categories round-trip on both the feed and each entry; on parse an RSS author falls back to <dc:creator> when the native <author> is absent. An enclosure without a length reads as 0 (unknown); a length that is not a non-negative integer (blank, negative, non-numeric) fails the parse.
import "feed.j" as feed;
def ep as feed.Entry init feed.entryEnclosure(
feed.entryAuthor(feed.entry("Episode 1", "https://show.example/1"), "Jane Host"),
"https://show.example/1.mp3", 12345678, "audio/mpeg");
def show as feed.Feed init feed.feedCategory(
feed.feedAuthor(feed.feed("My Show", "https://show.example"), "Jane Host"),
"Technology");
def rss as string init feed.build("rss", feed.add($show, $ep));Parsing
| Call | Returns | Notes |
|---|---|---|
feed.parse(text) | Feed | Parse feed XML, detecting RSS vs Atom from the root element. |
feed.kind(text) | string | "rss" or "atom", the detected format (kind "value" if neither). |
Parsing is strict: a document that is not a well-formed feed throws Error{kind: "value"} (malformed XML throws the xml library's error), so a broken feed is never read as a plausible one with empty fields or 1970 dates. That covers:
- a root that is neither an RSS
<rss>(in no namespace, with exactly one<channel>) nor an Atom<feed>in the Atom namespace; - a date that is there but does not parse: an unparseable date on any item fails the whole parse. RSS dates are RFC 822: an optional day name, a 1- or 2-digit day, a 2- or 4-digit year (
00-49is 20xx,50-9919xx, as RFC 5322 reads them), optional seconds, and a zone that is a numeric offset,UT/GMT/Z, orEST/EDT/CST/CDT/MST/MDT/PST/PDT; names match in any case, and the day name is not checked against the date. Atom dates are RFC 3339. An absent date element is unset (the epoch sentinel), not an error; - an enclosure
lengththat is not a non-negative integer; - an Atom
type="xhtml"text construct (title,summary,content) that does not hold exactly one XHTML<div>.
An Atom type="xhtml" construct reads as the serialised markup inside its <div> (RFC 4287 3.1.1.3; the div itself is left out, prefixes as written); type="html" reads as the HTML text, unescaped once by XML as usual (<p> is <p>); type="text" or none as the text.
Reading a feed off the web
The module does no I/O. Fetch the document with the http module (default binary) and parse the body:
import "http.j" as http;
import "feed.j" as feed;
def resp as http.Response init http.get("https://example.org/feed.xml", {});
if ($resp.status < 200 or $resp.status >= 300) {
throw Error{kind: "http", message: "feed returned status {$resp.status}"};
}
def f as feed.Feed init feed.parse($resp.body);Safety for untrusted feeds
Feeds come from URLs you do not control, so the untrusted-input paths are hardened:
- Deeply nested XML is capped by the
xmllibrary's shared nesting limit (a catchable error, not a stack overflow); see xml. - Entity-expansion ("billion laughs") is impossible:
xmldecodes only the five predefined entities and numeric character references, never a DTD's custom entities. - Oversized bodies are bounded when you fetch through
http, which caps a response body (64 MiB by default,Options.maxBytesofhttp.sendfor another cap) so a hostile server cannot drive the interpreter to OOM; the fetch fails with a catchable error instead. - Parsing is linear in the document size; a large feed is slow, not quadratic.
For a hostile source, prefer feed.parse on a body you have already size-bounded, and wrap the call in try / catch: a malformed feed throws (kind "value", or xml's error for malformed XML).
Platforms
Pure Jennifer over xml + time, with no network dependency: the module runs on both binaries. Fetching a feed is the caller's http call, which needs the default jennifer.
Academy talks
| Talk | What it shows |
|---|---|
| Your own small blog with Jennifer | an RSS and Atom feed of the posts |
API reference
Every exported function, struct and constant of feed, with its signature, generated from the module's doc comments: the feed API reference.