そういえばあまり「GAE(Google App Engine)」を触っていなかったので、久々にやってみた。
# というよりも、アプリなんか作ったことないんだけどねw
とりあえず、Twitterのタイムラインでも取得するか!って事で、以前使った「python-twitter」でやってみる。
なんか調べると多少ファイルが必要になったり、
一部変更が必要なみたいなので、メモメモ。
- python-twitterのファイルをダウンロードし設置
まずはファイルが必要みたいなので、GoogleCodeにある「python-twitter」からファイルをダウンロードしてきた。
そこに入っている、
- twitter.py(ファイル)
- simplejson(ディレクトリ)
が必要となるようだ。
なのでまずはこいつをGAEのアプリのドキュメントルートへ設置する。
./ ../ app.yaml index.yaml main.py simplejson/ twitter.py |
- APIクラスでのファイルキャッシュ処理のせいで、正常に実行されないために上書き
普通にドキュメントに書かれているとおりやると、
- AttributeError: 'module' object has no attribute 'getlogin' - ImportError: No module named _multiprocessing |
などのエラーが出るので、main.pyに下記を記述する。
import twitter # twitter.Api.__init__ method for override. def twitter_api_init_gae(self, username=None, password=None, input_encoding=None, request_headers=None): import urllib2 from twitter import Api self._cache = None self._urllib = urllib2 self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT self._InitializeRequestHeaders(request_headers) self._InitializeUserAgent() self._InitializeDefaultParameters() self._input_encoding = input_encoding self.SetCredentials(username, password) # overriding API __init__ twitter.Api.__init__ = twitter_api_init_gae |
こいつについては、
python-twitterのIssue 59と
pengineの Issue 1504 に記載されておりました。
参考URL :
Lazycozy’s Blog – Twitter Bot を作ってみる –
Reinvention of the Wheel – Google App Engineでpython-twitterが動かない件について –
- pythonのバージョンは「2.5」
ローカルのPythonがPython2.6の場合、GoogleAppEngineLauncherでPython Pathを記述出来るので、そちらへPython2.5を設定する。
#GAE上のPythonは2.5なので併せた。
- ImportError: No module named _ctypes
調べたところ、デプロイしたものであれば、とりあえず動作するようだが、原因がわかっていないので調査する。
# 2010.03.26追記
GAEのPythonPathが2.5では無く2.6を見ていたらしい。。。
実際に行った対応は、GAELauncherを起動して
gae_python25 posted by (C)kishir
GAELauncherの環境設定のPythonPathを設定する項目へ、
Python2.5のパスを入力する。
この後に「Enter(エンター)」を押さないと、設定が反映されないようだ。。。
その後にLauncherを再起動する。
で再度、環境設定のPythonPathを確認して設定されている事を確認。設定されていればオッケー。
これで正常に動作しました。
- main.pyへロジックを記述
下記のロジックでタイムラインを取得する事が可能。
#!/usr/bin/env python #!-*- coding:utf-8 -*- # # Copyright 2007 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import os import twitter from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp import util from google.appengine.ext.webapp import template # twitter.Api.__init__ method for override. def twitter_api_init_gae(self, username=None, password=None, input_encoding=None, request_headers=None): import urllib2 from twitter import Api self._cache = None self._urllib = urllib2 self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT self._InitializeRequestHeaders(request_headers) self._InitializeUserAgent() self._InitializeDefaultParameters() self._input_encoding = input_encoding self.SetCredentials(username, password) # overriding API __init__ twitter.Api.__init__ = twitter_api_init_gae class MainHandler(webapp.RequestHandler): """ get. """ def get(self): user = users.get_current_user() if user: u = 'ユーザー名' p = 'パスワード' api = twitter.Api(username=u, password=p) statuses = api.GetUserTimeline() l = [] for s in statuses: l.append(s.text.encode('utf-8')) template_values = { 'list': l, } path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri)) def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main() |
今回はテンプレートモジュールを使用しているので、main.pyと同じディレクトリへ、
「index.html」を設置して対応した。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8" /> <title>GAEでTwitter</title> <meta name="description" content="GAE Twitter" /> <body> <div id="name"> {% for l in list %} {{ l }} {% endfor %} </div> </body> </html> |
これでタイムラインがとりあえず取得可能となるので、後は色々といじくることが可能となりそう。
ちなみに取得したタイムラインはHTML上に下記のように表示された。
@komagata 埼玉の人にもらっちゃえば良いよ @komagata 高いっすよねー。まいっちんぐですよ。 そっかpython2.5なのか・・・。2.6じゃだめかw 今日はGAEと戯れてみる バナナマンのpodcastを全部聞いてしまったせいか、寂しいな・・・。 雨が降ってる。。。やだよー。。。 ファックな事に雨が降ってる。。。 次は「3DS」なのかー RT @fladdict: あ、本物だった → Nintendo 3DS 発表。 http://j.mp/9Y7del RT @fladdict: Googleが中国での稼働状況公開ページを作っとるw http://www.google.com/prc/report.html#hl=en RT @whosaysni: RT @kernel_thread: RT @niryuu: 日本語訳来た Google Japan Blog: 中国における事業展開について http://bit.ly/cA1A44 いいなーこういうの。RT @skmt09: おもろい。http://bit.ly/6ipn6F RT @umelns: おっと! - 米Amazon、早くもiPad用Kindleアプリの情報提供開始 - http://goo.gl/kKPc - グーグル、中国の検索サイト停止 - http://bit.ly/dCXnE2 @yoshuki おつでしたー 子供をお風呂に入れたー あ、今日はカバチ最終回か。嫁さまが楽しみにしているドラマなのに・・・。 タスクが6つほどあるな・・・。どうさばくかなー。 おっと、OAuthのバグのせいなのかw 光が丘に「おはしCafeガスト」が出来てたので、来てみた。 メニューの内容が体に良いものがあるので、いまの自分には丁度良いなー デパートみたいにガヤガヤしているところだと、 携帯に掛けても気づかれないんだよなー。。。 |