Python, Atom Generator, and Google gdata

14 Jun 2009

It's hidden, but google provides a python Atom module in the gdata API.

Installing is just the usual "easy_install URL" with the latest tarball from the downloads section. If having all the other gdata apis makes you uncomfortable, just checkout the atom bits with:

svn co

(consider usng svn:external)

The pedantic implementation of the first example from the Atom spec is:

from gdata.client import atom
# or "import atom" if you did the svn checkout method

feedauthor = atom.Author(name = atom.Name(text='John Doe'))
feedtitle = atom.Title(text = "Example Feed")
feedlink = atom.Link(href = "")
# anything as long as it is unique                                                                                                          
feedid = atom.Id(text="urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6")
feedupdated = atom.Updated("2003-12-13T18:30:02Z")

entries = []
e_title   = atom.Title(text="Atom-Powered Robots Run Amok")
e_link    = atom.Link(href= "")
e_id      = atom.Id(text="urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
e_updated = atom.Updated("2003-12-13T18:30:02Z")
e_summary = atom.Summary(text="Some text.")

entries.append( atom.Entry(title=e_title, link=e_link, atom_id=e_id, summary=e_summary))

feed = atom.Feed(entry=entries, title=feedtitle, link=feedlink, atom_id=feedid, updated=feedupdated)


As you can see it's kinda verbose, but hey, we are dealing with XML so whattdoya expect. pydoc is your friend.

The output will look something like the following. If you throw it under Apache with "junk.atom" you should be able to see it with your fav RSS reader or view it in your browser.

<?xml version='1.0' encoding='UTF-8'?>
<ns0:feed xmlns:ns0="">
<ns0:title>Example Feed</ns0:title>
<ns0:link href="" />
  <ns0:link href="" />
  <ns0:summary>Some text.</ns0:summary>
  <ns0:title>Atom-Powered Robots Run Amok</ns0:title>

I'm sure this library also does parsing, but I haven't tested that