Find file
8fd2ecc Jun 22, 2009
82 lines (62 sloc) 3.06 KB
Using the Blogger API from Python
Blogger exposes a Google Data API. Using this API, you can access all parts of
your blog using simple HTTP requests. The REST-style API is based on the
Atom Publishing Protocol.
It is very easy to access the blogger API (or any Google Data API) from
Python using the `Google Data APIs Python Client Library (gdata-python-client) <>`_.
This library is easy to use, has great documentation and great sample code
Using `reStructuredText <>`_ and gdata-python-client I created a simple
application (````) that takes a rst file, converts it to HTML and publishes the HTML on my blog.
Using this approach, I can edit blog posts in `a my editor of choice
<>`_, using an easy-to-read plaintext markup syntax.
Example use:
.. sourcecode:: bash
# first, get the blog id
$ python --username --listblogs
nnnnnnnnnnnnnnnnnnn: Code Monkey Do
# then create a new blog post
$ python --username --blog nnnnnnnnnnnnnnnnnnn post1.rst
$ python --help
```` source code (`on github <>`_):
.. sourcecode:: python
import rstdirective
def login(username, password):
import gdata.service
service = gdata.service.GDataService(username, password)
service.service = 'blogger'
service.server = ''
return service
def create_entry(title, content, draft=False):
import atom
import gdata
entry = gdata.GDataEntry()
entry.title = atom.Title(title_type='text', text=title)
entry.content = atom.Content(content_type='html', text=content.encode('utf8'))
if draft:
control = atom.Control()
control.draft = atom.Draft(text='yes')
entry.control = control
return entry
def listblogs(service):
feed = service.Get('/feeds/default/blogs')
for blog in feed.entry:
print "%s: %s" % (blog.GetSelfLink().href.split('/')[-1],
def listposts(service, blogid):
feed = service.Get('/feeds/' + blogid + '/posts/default')
for post in feed.entry:
print post.GetEditLink().href.split('/')[-1], post.title.text, "[DRAFT]" if is_draft(post) else ""
# ... see full source code on github:
If you are not familiar with the reStructuredText format, have a look at
`this example <>`_,
the rst source for this post.
You can download the entire source code from `github <>`_.