python-box:appengine-twitter
差分
このページの2つのバージョン間の差分を表示します。
次のリビジョン | 前のリビジョン | ||
python-box:appengine-twitter [2009/07/09 23:51] – 作成 dgbadmin | python-box:appengine-twitter [2017/05/13 05:36] (現在) – [AppEngine-Twitterのソースコード] dgbadmin | ||
---|---|---|---|
行 1: | 行 1: | ||
====== Google App Engineで手軽にTwitterアプリを作成!(OAuthにも対応!) - AppEngine-Twitter ====== | ====== Google App Engineで手軽にTwitterアプリを作成!(OAuthにも対応!) - AppEngine-Twitter ====== | ||
- | Coming Soon! | + | 更新履歴とコメントは[[http:// |
+ | |||
+ | {{: | ||
+ | |||
+ | Google App EngineでTwitter APIを操作するライブラリを作ってみた。\\ | ||
+ | 車輪の再発明ではなく、趣味の車輪作り。 | ||
+ | |||
+ | バージョン0.3.0からはTwitter Search APIの廃止に対応して、検索もOAuthを使ってTwitter REST API 1.1を使うように変更した。 | ||
+ | |||
+ | |||
+ | ===== ライセンス ===== | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | |||
+ | ===== デモ(動作サンプル) ===== | ||
+ | |||
+ | ==== その1. RT(ReTweet)の多いつぶやきを報告するBot ==== | ||
+ | [[http:// | ||
+ | |||
+ | |||
+ | ==== その2. 地名を告げるとその近くにいるTwitterユーザーの名前を教えてくれるBot ==== | ||
+ | [[http:// | ||
+ | |||
+ | ※検索がまだTwitter REST API 1.1に対応させてないので今は動かない(はず) | ||
+ | |||
+ | === 使用例 === | ||
+ | < | ||
+ | 試しにつぶやくには、Twitterにログイン後に[[http:// | ||
+ | |||
+ | |||
+ | ==== その3. 出発地点と目的地を告げると、乗換(ルート)案内のGoogleマップを表示してくれるBot ==== | ||
+ | [[http:// | ||
+ | |||
+ | === 使用例 1. 移動方法を指定しないと、電車でのルート検索・乗り換え案内になる === | ||
+ | < | ||
+ | 試しにつぶやくには、Twitterにログイン後に[[http:// | ||
+ | |||
+ | === 使用例 2. 車でのルートの案内 === | ||
+ | < | ||
+ | 試しにつぶやくには、Twitterにログイン後に[[http:// | ||
+ | |||
+ | === 使用例 3. 歩きでのルートの案内 === | ||
+ | < | ||
+ | 試しにつぶやくには、Twitterにログイン後に[[http:// | ||
+ | |||
+ | |||
+ | ==== その4. Twitter APIでOAuth認証してつぶやいたりフォローしたりするデモ ==== | ||
+ | |||
+ | [[python-box/ | ||
+ | |||
+ | |||
+ | ===== AppEngine-Twitterのソースコード ===== | ||
+ | <code python> | ||
+ | #!/ | ||
+ | # -*- coding: utf-8 -*- | ||
+ | ''' | ||
+ | AppEngine-Twitter | ||
+ | |||
+ | Twitter API wrapper for applications on Google App Engine | ||
+ | |||
+ | See: http:// | ||
+ | License: http:// | ||
+ | |||
+ | See also: | ||
+ | https:// | ||
+ | https:// | ||
+ | ''' | ||
+ | |||
+ | __author__ = ' | ||
+ | __version__ = ' | ||
+ | |||
+ | |||
+ | import json | ||
+ | import urllib | ||
+ | from appengine_oauth import AppEngineOAuth | ||
+ | from google.appengine.api import urlfetch | ||
+ | |||
+ | |||
+ | class AppEngineTwitter(object): | ||
+ | |||
+ | def __init__(self): | ||
+ | self._api_url = ' | ||
+ | self._oauth_url = ' | ||
+ | self._oauth = None | ||
+ | self._tw_name = '' | ||
+ | |||
+ | |||
+ | def update(self, | ||
+ | ''' | ||
+ | Post a tweet | ||
+ | Sucess => Retrun 200 / Fialed => Return other HTTP status | ||
+ | ''' | ||
+ | return self._post(' | ||
+ | |||
+ | |||
+ | def retweet(self, | ||
+ | ''' | ||
+ | ReTweet a tweet | ||
+ | Sucess => Retrun 200 / Fialed => Return other HTTP status | ||
+ | ''' | ||
+ | return self._post(' | ||
+ | |||
+ | |||
+ | def follow(self, | ||
+ | ''' | ||
+ | Sucess => Return 200 / Already following => Return 403 / | ||
+ | Fialed => Return other HTTP status | ||
+ | ''' | ||
+ | return self._post(' | ||
+ | |||
+ | |||
+ | def is_following(self, | ||
+ | ''' | ||
+ | Yes => Return True / No => Return False / | ||
+ | Fialed => Return HTTP status except 200 | ||
+ | ''' | ||
+ | if self._tw_name == '': | ||
+ | self.verify() | ||
+ | user_info = json.loads(self.last_response.content) | ||
+ | self._tw_name = user_info[' | ||
+ | |||
+ | status = self._get(' | ||
+ | | ||
+ | if status == 200: | ||
+ | return (self.last_response.content == ' | ||
+ | else: | ||
+ | return status | ||
+ | |||
+ | |||
+ | def verify(self): | ||
+ | ''' | ||
+ | Verify user_name and password, and get user info | ||
+ | Sucess => Return 200 / Fialed => Return other HTTP status | ||
+ | ''' | ||
+ | return self._get(' | ||
+ | |||
+ | |||
+ | def search(self, | ||
+ | ''' | ||
+ | Sucess => Return Array of dict / Fialed => raise an Exception | ||
+ | ''' | ||
+ | params[' | ||
+ | status = self._get(' | ||
+ | |||
+ | if status == 200: | ||
+ | return json.loads(self.last_response.content)[' | ||
+ | else: | ||
+ | raise Exception(' | ||
+ | |||
+ | |||
+ | # OAuth methods | ||
+ | # (See http:// | ||
+ | |||
+ | def set_oauth(self, | ||
+ | ''' | ||
+ | Set OAuth parameters | ||
+ | ''' | ||
+ | self._oauth = AppEngineOAuth(key, | ||
+ | |||
+ | |||
+ | def prepare_oauth_login(self): | ||
+ | ''' | ||
+ | Get request token, request token secret and login URL | ||
+ | ''' | ||
+ | dic = self._oauth.prepare_login(self._oauth_url + ' | ||
+ | dic[' | ||
+ | return dic | ||
+ | |||
+ | |||
+ | def exchange_oauth_tokens(self, | ||
+ | ''' | ||
+ | Exchange request token for access token | ||
+ | ''' | ||
+ | return self._oauth.exchange_tokens(self._oauth_url + ' | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | # Private methods | ||
+ | |||
+ | def _post(self, path, params): | ||
+ | url = self._api_url + path | ||
+ | params = self._oauth.get_oauth_params(url, | ||
+ | res = urlfetch.fetch(url=url, | ||
+ | self.last_response = res | ||
+ | return res.status_code | ||
+ | |||
+ | |||
+ | def _get(self, path, params): | ||
+ | url = self._api_url + path | ||
+ | if self._oauth != None: | ||
+ | params = self._oauth.get_oauth_params(url, | ||
+ | url += '?' | ||
+ | res = urlfetch.fetch(url=url, | ||
+ | self.last_response = res | ||
+ | return res.status_code | ||
+ | </ | ||
+ |
python-box/appengine-twitter.1247151100.txt.gz · 最終更新: 2009/07/09 23:51 by dgbadmin