public
Description: Publish reStructuredText (rst) formatted blog posts to blogger.
Homepage: http://codemonkeydo.blogspot.com/2009/06/using-blogger-api-from-python_344.html
Clone URL: git://github.com/codeape2/python-blogger.git
python-blogger / posts / codemonkeydo / using-the-blogger-api-from-python
100644 82 lines (62 sloc) 3.131 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
=================================
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) <http://code.google.com/p/gdata-python-client/>`_.
This library is easy to use, has great documentation and great sample code
included.
 
Using `reStructuredText <http://docutils.sourceforge.net/rst.html>`_ and gdata-python-client I created a simple
application (``blogger.py``) 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
<http://www.vim.org>`_, using an easy-to-read plaintext markup syntax.
 
Example use:
 
.. sourcecode:: bash
 
    # first, get the blog id
    (vanilla)[~/misc/blogger]
    $ python blogger.py --username my.email@gmail.com --listblogs
    Password:
    nnnnnnnnnnnnnnnnnnn: Code Monkey Do
 
    # then create a new blog post
    (vanilla)[~/misc/blogger]
    $ python blogger.py --username my.email@gmail.com --blog nnnnnnnnnnnnnnnnnnn post1.rst
    Password:
 
    $ python blogger.py --help
    ...
 
``blogger.py`` source code (`on github <http://github.com/codeape2/python-blogger/tree/master/blogger.py>`_):
 
.. sourcecode:: python
 
    import rstdirective
 
    def login(username, password):
        import gdata.service
        service = gdata.service.GDataService(username, password)
        service.service = 'blogger'
        service.server = 'www.blogger.com'
        service.ProgrammaticLogin()
        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],
                blog.title.text)
 
    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: http://github.com/codeape2/python-blogger/tree/master/blogger.py
 
If you are not familiar with the reStructuredText format, have a look at
`this example <http://github.com/codeape2/python-blogger/tree/master/posts/codemonkeydo/using-the-blogger-api-from-python>`_,
the rst source for this post.
 
You can download the entire source code from `github <http://github.com/codeape2/python-blogger>`_.