pythonで動作するWebService::Simple「webSimple」を使ってネタバイザーのRSSから最新ネタを取得し、LingrのチャットルームにこれまたwebSimpleで発言するアプリです。
まずテンプレート
lingr.html
<html>そしてハンドラ
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ネタバイザーのネタをlingrに転送</title>
<style tyle="text/css"><!--
body {
font-family: 'メイリオ', 'Osaka'
}
#content {
margin-left: 50px;
}
--></style>
</head>
<body>
<h1>ネタバイザーのネタをlingrに転送</h1>
<div id="content">
<p><a href="http://netaviser.woresukebe.com/" class="external" target="_blank">ネタバイザー</a>の最新発言を受信し、<a href="http://www.lingr.com/" class="external" target="_blank">Lingr</a>のチャットルーム「<a href="http://www.lingr.com/room/hO4SmQWTdJ4">LingrAPI Test</a>」に転送します。</p>
<p>「ネタ転送」ボタンを押下して下さい。</p>
<form method="post">
<input type="submit" value="ネタ転送" />
</form>
<div>
{% if neta %}
ネタ「{{ neta|escape }}」を転送しました。
{% endif %}
</div>
</div>
<hr />
<p style="text-align: center">provided by <a href="http://mattn.kaoriya.net">mattn</a>, hosted on google app server.</p>
</body>
</html>
lingr.py
#!-*- coding:utf-8 -*-で、動いている物がこちら
import os
import cgi
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch
from webSimple import Simple
import elementtree.SimpleXMLTreeBuilder as xmlbuilder
import xmllib
class MainPage(webapp.RequestHandler):
def get_neta(self):
neta = urlfetch.fetch('http://netaviser.woresukebe.com/index.xml').content
parser = xmlbuilder.TreeBuilder()
xmllib.XMLParser.__init__(parser, accept_utf8=1)
parser.feed(neta)
xml = parser.close()
return xml.find('channel/item/title').text
def post(self):
api = Simple({
'base_url' : 'http://www.lingr.com',
'param' : {
'api_key' : 'your-api-key',
'format' : 'xml'
},
})
neta = self.get_neta()
session = api.get({}, {
'path' : '/api/session/create',
}).parse_xml().find('session').text;
ticket = api.get({
'session' : session,
'id' : 'hO4SmQWTdJ4',
'nickname' : 'ネタバイザー転送サーバ',
}, {
'path' : '/api/room/enter',
}).parse_xml().find('ticket').text;
status = api.get({
'session' : session,
'ticket' : ticket,
'message' : neta,
}, {
'path' : '/api/room/say',
}).parse_xml().find('status').text;
api.get({
'session' : session,
}, {
'path' : '/api/session/destroy',
})
path = os.path.join(os.path.dirname(__file__), 'lingr.html')
template_values = {
'session' : session,
'ticket' : ticket,
'neta' : neta,
'status' : status,
}
self.response.out.write(template.render(path, template_values))
def get(self):
path = os.path.join(os.path.dirname(__file__), 'lingr.html')
self.response.out.write(template.render(path, {}))
def main():
application = webapp.WSGIApplication([('/lingr/', MainPage)], debug=True)
wsgiref.handlers.CGIHandler().run(application)
ネタバイザーのネタをlingrに転送ネタバイザーに負荷が掛かりますので、あまりにリクエストが多い場合には停止させて頂く所存です。またネタバイザーの方が苦情があれば、これまた停止させて頂く所存です。
なお、先ほど修正したのですがPyWrapperのTreeBuilderはlibxml.XMLParserを初期化する際にutf-8を許可するかどうかのフラグ、「accept_utf8」を0のまま渡してしまっています。よってutf-8なXMLが通りませんでした。
初期化が冗長ですが、作ったTreeBuilderをlibxml.XMLParser.__init__で再初期化する様修正しています。



![Validate my RSS feed [Valid RSS]](http://mattn.kaoriya.net/images/valid-rss.png)

