Stephan Lensky, software developer, amateur writer

Syndication

— 04 January, 2017

In last week's episode of Dragon Ball Z, we went over the basics of how the static site generator for this blog is set up. Since then, I've updated it to generate an Atom feed for the site using the wonderful Python library feedgen. Took about 10 new lines of code and 15 minutes altogether including setup.

Atom Feed?

Atom is a syndication format spec which expands greatly on previously implemented standards (such as RSS). For those who don't want to read that monster of a Wikipedia article, a syndication file in the context of a blog is simply a file containing basic metadata about the website and the posts on the site. It provides a quick way for dedicated blog readers to aggregate post information about a number of different sites, provided these sites all have a syndication file. There are a number of different standards for what this syndication file should look like, most notable RSS and Atom. Atom's RFC was first published in 2005 and 12 years later has finally reached a point where it's well supported enough to be usable by most people, so it was the format chosen for this site.

Code

There isn't much to using feedgen, since most of the code is simply declarative. There are of course many properties that I haven't bothered to set (such as a recommended time to check for updates, yikes...), but I'm pretty sure I covered all of the important ones.

# blog properties
fg = FeedGenerator()
fg.title("Stephan's Blog")
fg.id('http://blog.slensky.com')
fg.subtitle('Sharing obscure workarounds and interesting programming stories')
fg.author({'name': 'Stephan Lensky', 'email': 'stephanl.public@gmail.com'})
fg.link({'href': 'http://blog.slensky.com'})
fg.language('en')
fg.updated(datetime.now(pytz.timezone('America/New_York')).isoformat())

# properties for each post
for p in posts:
    url = 'http://blog.slensky.com/{}/{}/{}.html'
        .format(p['date'].year, p['date'].month, sanitized_name)
    fe = fg.add_entry()
    fe.id(url)
    fe.title(p['name'])
    fe.updated(pytz.timezone('America/New_York')
        .localize(p['modified']).isoformat())
    fe.link({'href': url})

# create files
fg.atom_file(str(BUILD_DIR) + '/feed.xml')
fg.rss_file(str(BUILD_DIR) + '/rss.xml')

As you can see, feedgen makes it rather easy to generate both Atom and RSS feeds. Atom is honestly just a better format, but for your convenience RSS is also available here.

Cheers,
Stephan