gdata-python-clientのatomモジュールを使ってみる

gdata-python-clientにはatomモジュールが含まれているのだけど、結構よく出来てる。
Python2.5, gdata-2.0.14で確認。

インストール

PyPIに登録されているので、easy_installやpipでインストールできる。

$ pip install gdata

test_atom.py

# coding: utf-8
import pprint
from atom.data import Feed, Title, Entry
from atom.core import XmlElement

class Foo(XmlElement):
    _qname = '{http://example.com/atom/extended}foo'


class MyFeed(Feed):
    foo = Foo


def main():
    feed = Feed()
    feed.title = Title('title1')
    feed.entry = [
        Entry(title=Title('entry1')),
        Entry(title=Title('entry2')),
    ]

    # Feedクラスのメンバーを出力
    pprint.pprint(Feed._list_xml_members())

    # feedを文字列出力(Atomになる)
    print feed

    # 拡張したAtomとか
    myfeed = MyFeed()
    myfeed.foo = Foo('mydata')
    print myfeed

if __name__ == '__main__':
    main()

実行結果

$ python test_atom.py
[('author', [<class 'atom.data.Author'>]),
 ('category', [<class 'atom.data.Category'>]),
 ('contributor', [<class 'atom.data.Contributor'>]),
 ('entry', [<class 'atom.data.Entry'>]),
 ('generator', <class 'atom.data.Generator'>),
 ('icon', <class 'atom.data.Icon'>),
 ('id', <class 'atom.data.Id'>),
 ('link', [<class 'atom.data.Link'>]),
 ('logo', <class 'atom.data.Logo'>),
 ('rights', <class 'atom.data.Rights'>),
 ('subtitle', <class 'atom.data.Subtitle'>),
 ('title', <class 'atom.data.Title'>),
 ('updated', <class 'atom.data.Updated'>)]
<ns0:feed xmlns:ns0="http://www.w3.org/2005/Atom"><ns0:entry><ns0:title>entry1</ns0:title></ns0:entry><ns0:entry><ns0:title>entry2</ns0:title></ns0:entry><ns0:title>title1</ns0:title></ns0:feed>
<ns0:feed xmlns:ns0="http://www.w3.org/2005/Atom"><ns1:foo xmlns:ns1="http://example.com/atom/extended">mydata</ns1:foo></ns0:feed>

拡張も可能だしメタ情報も取れるので、いろいろできる。