pybloglinesを使う
サイトは、ここ。
http://www.josephson.org/projects/pybloglines/
以下の作業は、Python 2.4.1 for Cygwinで行った。(最近、Cygwinばっかりだ)
$ tar xzvf pybloglines-0.02.tar.gz pybloglines/pybloglines.py $ cd pybloglines $ ls pybloglines.py $ ipython Python 2.4.1 (#1, May 27 2005, 18:02:40) Type "copyright", "credits" or "license" for more information. IPython 0.6.15 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import pybloglines --------------------------------------------------------------------------- exceptions.ImportError Traceback (most rece nt call last) /cygdrive/f/Wacky/Test/bloglines/pybloglines/<console> /cygdrive/f/Wacky/Test/bloglines/pybloglines/pybloglines.py 11 import base64 12 import xml.parsers.expat ---> 13 import feedparser # http://sourceforge.net/projects/feedparser/ 14 15 class BloglinesException(Exception): pass ImportError: No module named feedparser
ふ〜む、FeedParserが要るらしい。
- SourceForge.net: Project Info - Universal Feed Parser
http://sourceforge.net/projects/feedparser/
$ python setup.py install running install running build running build_py creating build creating build/lib copying feedparser.py -> build/lib running install_lib copying build/lib/feedparser.py -> /usr/lib/python2.4/site-packages byte-compiling /usr/lib/python2.4/site-packages/feedparser.py to feedparser.pyc
よし、入った。
って訳で、ipythonで、対話的に操作してみる。
$ ipython Python 2.4.1 (#1, May 27 2005, 18:02:40) Type "copyright", "credits" or "license" for more information. IPython 0.6.15 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import pybloglines In [2]: dir(pybloglines) Out[2]: ['BloglinesException', 'BloglinesWebServices', 'OpmlParser', 'Subscription', '__builtins__', '__doc__', '__file__', '__name__', 'base64', 'feedparser', 're', 'string', 'urllib', 'urllib2', 'xml'] In [9]: srv = pybloglines.BloglinesWebServices("ユーザ名", "パスワード") In [10]: srv.system.listMethods() --------------------------------------------------------------------------- exceptions.AttributeError Traceback (most recent call last) /cygdrive/f/Wacky/Test/bloglines/pybloglines/<console> AttributeError: BloglinesWebServices instance has no attribute 'system' In [11]: dir(srv) Out[11]: ['BWS_HOSTNAME', 'BWS_REALM', '__doc__', '__init__', '__module__', 'checkPasswordSpecified', 'getContent', 'getitems', 'listsubs', 'password', 'update', 'user'] In [23]: srv.listsubs() --------------------------------------------------------------------------- xml.parsers.expat.ExpatError Traceback (most rece nt call last) /cygdrive/f/Wacky/Test/bloglines/pybloglines/<console> /cygdrive/f/Wacky/Test/bloglines/pybloglines/pybloglines.py in listsubs(self) 71 content = self.getContent(url, True) 72 opmlParser = OpmlParser() ---> 73 feedlist = opmlParser.parse(content) 74 return feedlist 75 /cygdrive/f/Wacky/Test/bloglines/pybloglines/pybloglines.py in parse(self, opml) 103 def parse(self, opml): 104 self.feedlist = [] --> 105 self.parser.Parse(opml) 106 return self.feedlist 107 ExpatError: not well-formed (invalid token): line 51, column 46 In [24]: help(pybloglines.BloglinesWebServices)
listsubs メソッドの呼び出しでエラーが起きる。
何で?と思って、IEから直接叩いてやると、原因がわかった。
要するに、吐き出すXMLが異常だっちゅ〜事やね。
という訳で、IEでパースできない登録アイテムを さっさと削除しる。
In [31]: f = srv.listsubs() In [35]: for i in f: ....: p i.title.encode("euc_jp") ....: --------------------------------------------------------------------------- exceptions.UnicodeEncodeError Traceback (most rece nt call last) /cygdrive/f/Wacky/Test/bloglines/pybloglines/<console> /usr/lib/python2.4/site-packages/IPython/iplib.py in ipmagic(arg_s) 140 else: 141 magic_args = __IPYTHON__.var_expand(magic_args) --> 142 return fn(magic_args) 143 144 def ipalias(arg_s): /usr/lib/python2.4/site-packages/IPython/Magic.py in magic_p(self, parameter_s) 576 def magic_p(self, parameter_s=''): 577 """Just a short alias for Python's 'print'.""" --> 578 exec 'print ' + parameter_s in self.shell.user_ns 579 580 def magic_r(self, parameter_s=''): /cygdrive/f/Wacky/Test/bloglines/pybloglines/<string> UnicodeEncodeError: 'euc_jp' codec can't encode character u'\uff5e' in position 3: illegal multibyte sequence 100SHIKI CodeZine Devlog FedoraNEWS日本語版 GDNJ 掲示板 MFPM:最新のお気に入りコメント ...
途中までだけど、うまく取り込めた!
で、今までの結果をコードに落とした。
import pybloglines srv = pybloglines.BloglinesWebServices("ユーザ名", "パスワード") cnt = srv.update() print "count=", cnt f = srv.listsubs() for i in f: name = i.title.encode("euc_jp") print "title=", name print "id=", i.bloglinesSubId if name == "新着リスト / すべて": o = srv.getitems(i.bloglinesSubId, 2) print o break
実は、このpybloglinesを使ってみたのは、「まだ読んでないことにする」対象の奴を、全て既読にしてやろうと思っていたんだけど、この後の情報を見る限り、そういう事は出来ないっぽいな。
結局の所、一番手っ取り早いのは、一旦登録解除して、再登録しなおすという方法しか無いって事がわかった一日だった。はふぅ。
補足
XMLで正常にパースできなくなる件なんだけど、件名の所を短くすると回避できる気がしなくないでも無い。(一回試してうまくいった。その後試してないので、保障はできないが)