RSS を Python から作成したくて色々探してみたのですが、ElementTree や RSS.py を触ってみたものの使用感が個人的にイマイチ。
更に探してみたら PyRSS2Gen というものがあることを発見。使ってみた感覚としては、前述した ElementTree や RSS.py より利用しやすいと思いました。
PyRSS2Gen, ダウンロード
使う前にインストール。coreserver 上で使いたいので、Prefix は変更。
user@host:~> cd src/
user@host:~/src>
user@host:~/src> wget http://www.dalkescientific.com/Python/PyRSS2Gen-1.0.0.tar.gz
user@host:~/src> tar zxvf PyRSS2Gen-1.0.0.tar.gz
user@host:~/src> cd PyRSS2Gen-1.0.0/
user@host:~/src/PyRSS2Gen-1.0.0> python setup.py build
user@host:~/src/PyRSS2Gen-1.0.0> python setup.py install –Prefix=$HOME/local
これでインストール完了。動作確認。
user@host:~> python
Python 2.4.3 (#1, Sep 9 2006, 16:25:40)
[GCC 3.4.6] on linux2
>>> import PyRSS2Gen
>>>
問題なくインポートできたもよう。
実際の動作確認として PyRSS2Gen にあるサンプルソースを実行。
import datetime
import PyRSS2Gen
rss = PyRSS2Gen.RSS2(
title = "Andrew’s PyRSS2Gen feed",
link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html",
description = "The latest news about PyRSS2Gen, a "
"Python library for generating RSS2 feeds",
lastBuildDate = datetime.datetime.now(),
items = [
PyRSS2Gen.RSSItem(
title = "PyRSS2Gen-0.0 released",
link = "http://www.dalkescientific.com/news/030906-PyRSS2Gen.html",
description = "Dalke Scientific today announced PyRSS2Gen-0.0, "
"a library for generating RSS feeds for Python. ",
guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/news/"
"030906-PyRSS2Gen.html"),
pubDate = datetime.datetime(2003, 9, 6, 21, 31)),
PyRSS2Gen.RSSItem(
title = "Thoughts on RSS feeds for bioinformatics",
link = "http://www.dalkescientific.com/writings/diary/"
"archive/2003/09/06/RSS.html",
description = "One of the reasons I wrote PyRSS2Gen was to "
"experiment with RSS for data collection in "
"bioinformatics. Last year I came across...",
guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/writings/"
"diary/archive/2003/09/06/RSS.html"),
pubDate = datetime.datetime(2003, 9, 6, 21, 49)),
])
rss.write_xml(open("pyrss2gen.xml", "w"))
ここでトラップカードオープン!
保存された pyrss2gen.xml を確認してみると……。
<?xml version="1.0" encoding="Shift_JIS"?>
…… Shift_JIS とな!
それじゃあ意味がない。
にしても PyRSS2Gen 公式にまともなドキュメントが見当たらない。自分が見つけられないだけなのか?
ということで、色々試した結果 UTF-8 で保存する方法を発見。
rss.write_xml(open("pyrss2gen.xml", "w"), "utf-8")
こうすることにより、UTF-8 で保存することが出来ました。
official.PyRSS2Gen