python-box:appengine-twitter
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
python-box:appengine-twitter [2011/03/28 21:43] – [その5. Twitter APIでOAuth認証な操作(検索やフォローしてるかどうかの確認)をするデモ] dgbadmin | python-box:appengine-twitter [2017/05/13 05:36] (現在) – [AppEngine-Twitterのソースコード] dgbadmin | ||
---|---|---|---|
行 7: | 行 7: | ||
Google App EngineでTwitter APIを操作するライブラリを作ってみた。\\ | Google App EngineでTwitter APIを操作するライブラリを作ってみた。\\ | ||
車輪の再発明ではなく、趣味の車輪作り。 | 車輪の再発明ではなく、趣味の車輪作り。 | ||
+ | |||
+ | バージョン0.3.0からはTwitter Search APIの廃止に対応して、検索もOAuthを使ってTwitter REST API 1.1を使うように変更した。 | ||
行 15: | 行 17: | ||
===== デモ(動作サンプル) ===== | ===== デモ(動作サンプル) ===== | ||
- | |||
- | ※OAuthを使っているデモについては[[python-box/ | ||
- | |||
- | |||
- | ちなみに、下記のTwitter BotはTwitter Search APIの制限を回避するために、Google App Engine以外のサーバも組み合わせて使っている。 | ||
- | |||
==== その1. RT(ReTweet)の多いつぶやきを報告するBot ==== | ==== その1. RT(ReTweet)の多いつぶやきを報告するBot ==== | ||
行 28: | 行 24: | ||
==== その2. 地名を告げるとその近くにいるTwitterユーザーの名前を教えてくれるBot ==== | ==== その2. 地名を告げるとその近くにいるTwitterユーザーの名前を教えてくれるBot ==== | ||
[[http:// | [[http:// | ||
+ | |||
+ | ※検索がまだTwitter REST API 1.1に対応させてないので今は動かない(はず) | ||
=== 使用例 === | === 使用例 === | ||
行 46: | 行 44: | ||
=== 使用例 3. 歩きでのルートの案内 === | === 使用例 3. 歩きでのルートの案内 === | ||
- | < | + | < |
- | 試しにつぶやくには、Twitterにログイン後に[[http:// | + | 試しにつぶやくには、Twitterにログイン後に[[http:// |
==== その4. Twitter APIでOAuth認証してつぶやいたりフォローしたりするデモ ==== | ==== その4. Twitter APIでOAuth認証してつぶやいたりフォローしたりするデモ ==== | ||
- | OAuth認証が必要なAPI操作のデモについては、[[python-box/ | ||
- | |||
- | |||
- | ==== その5. Twitter APIでOAuth認証が不要な操作(検索、フォローしてるかどうかの確認)をするデモ ==== | ||
- | [[http:// | ||
- | |||
- | ソースコード\\ | ||
- | (※ページ表示で[[python-box/ | ||
- | <code python> | ||
- | # | ||
- | # -*- coding: UTF-8 -*- | ||
- | ''' | ||
- | Sample for AppEngine-Twitter on Google App Engine | ||
- | |||
- | See: http:// | ||
- | ''' | ||
- | |||
- | import logging | ||
- | import wsgiref.handlers | ||
- | from appengine_twitter import AppEngineTwitter | ||
- | from basehandler import BaseHandler, | ||
- | from google.appengine.ext import webapp | ||
- | |||
- | |||
- | class MainHandler(BaseHandler): | ||
- | |||
- | def get(self): | ||
- | self.show() | ||
- | |||
- | |||
- | def post(self): | ||
- | self.show(self.request.get(' | ||
- | |||
- | |||
- | def show(self, name='' | ||
- | self.simple_header(u' | ||
- | |||
- | self.p(u'< | ||
- | self.p(u'< | ||
- | self.p('< | ||
- | self.p(u' | ||
- | self.p('< | ||
- | self.p(' | ||
- | self.p('</ | ||
- | self.p('< | ||
- | |||
- | if name: | ||
- | self.test(name) | ||
- | |||
- | self.p('< | ||
- | self.p(u' | ||
- | self.p('< | ||
- | self.p(' | ||
- | self.p(u'</ | ||
- | | ||
- | self.simple_footer() | ||
- | |||
- | |||
- | def test(self, name): | ||
- | twitter = AppEngineTwitter() | ||
- | |||
- | self.p(u'< | ||
- | |||
- | self.p(u'< | ||
- | results = twitter.search(u' | ||
- | for result in results: | ||
- | self.p(result[' | ||
- | self.p('< | ||
- | |||
- | self.p(u'< | ||
- | twitter._tw_name = name | ||
- | if twitter.is_following(' | ||
- | self.p(' | ||
- | else: | ||
- | self.p(' | ||
- | self.p('< | ||
- | |||
- | |||
- | |||
- | logging.getLogger() | ||
- | routing = [('/ | ||
- | application = webapp.WSGIApplication(routing, | ||
- | wsgiref.handlers.CGIHandler().run(application) | ||
- | </ | ||
+ | [[python-box/ | ||
行 151: | 行 66: | ||
See also: | See also: | ||
- | | + | |
- | | + | |
''' | ''' | ||
__author__ = ' | __author__ = ' | ||
- | __version__ = '0.2.1' | + | __version__ = '0.4.0' |
+ | import json | ||
import urllib | import urllib | ||
from appengine_oauth import AppEngineOAuth | from appengine_oauth import AppEngineOAuth | ||
- | from django.utils import simplejson | ||
from google.appengine.api import urlfetch | from google.appengine.api import urlfetch | ||
行 168: | 行 83: | ||
def __init__(self): | def __init__(self): | ||
- | self._api_url = ' | + | self._api_url = ' |
self._oauth_url = ' | self._oauth_url = ' | ||
- | self._search_url = ' | ||
self._oauth = None | self._oauth = None | ||
self._tw_name = '' | self._tw_name = '' | ||
行 206: | 行 120: | ||
if self._tw_name == '': | if self._tw_name == '': | ||
self.verify() | self.verify() | ||
- | user_info = simplejson.loads(self.last_response.content) | + | user_info = json.loads(self.last_response.content) |
self._tw_name = user_info[' | self._tw_name = user_info[' | ||
行 227: | 行 141: | ||
def search(self, | def search(self, | ||
''' | ''' | ||
- | Sucess => Return Array of dict / Fialed => Return HTTP status except 200 | + | Sucess => Return Array of dict / Fialed => raise an Exception |
- | FYI http:// | + | |
''' | ''' | ||
params[' | params[' | ||
- | | + | |
- | res = urlfetch.fetch(url=url, | + | |
- | self.last_response = res | + | |
- | if res.status_code | + | if status |
- | return | + | return |
- | elif res.status_code == 420: | + | |
- | err_msg = 'Rate Limiting: Retry After ' + res.headers[' | + | |
else: | else: | ||
- | | + | |
- | + | ||
- | raise Exception(' | + | |
python-box/appengine-twitter.1301316200.txt.gz · 最終更新: 2011/03/28 21:43 by dgbadmin