*目次 [#m579fb68] #contents *参考情報 [#y3bb4c2c] -[[Streaming API Documentation | dev.twitter.com:http://dev.twitter.com/pages/streaming_api]]。公式ドキュメント。 -[[Twitter Streaming APIをRubyで試してみる - しばそんノート:http://d.hatena.ne.jp/shibason/20090816/1250405491]] -[[RubyからTwitterStreamingAPIを使うgem作ったよ! - yayuguのにっき:http://d.hatena.ne.jp/yayugu/20091230/1262183478]] *基本 [#b89d8901] -BASIC認証で使える。一つのアカウントで一つのみ。 -OAuthでも使えるらしい([[OAuth で Streaming API - 記録用:http://d.hatena.ne.jp/laughing/20100613/1276434386]])。 -全てのデータがとれるわけではない。firehose, gardenhose, sample(spritzer)のアクセスレベルがアカウントごとに設定されており、許可無しで使えるのはsampleレベルのみ。 -[[Twitter Streaming API を gardenhose レベルで利用する - nobu-qの日記:http://d.hatena.ne.jp/nobu-q/20091202]]によるとgardenhoseレベルは申請すれば割と簡単に許可されるのだろうか?許可を申請するhttp://twitter.com/help/request_streamingのURL自体どこからたどればいいのか分からない。 *パラメータ [#ma7dfd37] ** track [#zb88381a] [[Streaming API: Methods | dev.twitter.com:http://dev.twitter.com/pages/streaming_api_methods#track]]より #pre{{ Specifies keywords to track. Phrases of keywords are specified by a comma-separated list. Queries are subject to Track Limitations, described in Track Limiting and subject to access roles, described in the statuses/filter method. Comma separated keywords and phrases are logical ORs, phrases are logical ANDs. Words within phrases are delimited by spaces. A tweet matches if any phrase matches. A phrase matches if all of the words are present in the tweet. (e.g. 'the twitter' is the AND twitter, and 'the,twitter' is the OR twitter.). Terms are exact-matched, and also exact-matched ignoring punctuation. Exact matching on phrases, that is, keywords with spaces, is not supported. Keywords containing punctuation will only exact match tokens and, other than keywords prefixed by # and @, will tend to never match. Non-space separated languages, such as CJK and Arabic, are currently unsupported as tokenization occurs on whitespace. Other UTF-8 phrases should exact match correctly, but will not substitute similar characters to their least-common-denominator. For all these cases, consider falling back to the Search REST API. }} -トラックするキーワードを指定する。キーワードのフレーズはカンマ区切りのリスト形式。 -カンマ区切りのキーワードは論理OR("the, twitter")。 -フレーズは論理AND。フレーズ内の単語はスペースで区切られる("the twitter")。 -フレーズのどれかに一致すればtweetは一致したとみなされる。 -フレーズはすべての語がtweetの中に出現すれば一致したとみなされる。 -スペースが含まれるキーワードの性格なマッチはサポートされない。 -句読点が含まれるキーワードは(句読点以外の)項目だけがマッチする。 -#と@はマッチしない。 -CJKやArabicは現在はサポートされない。 -UTF-8はマッチする(?) *Rubyのサンプルコード [#zeca8442] #pre{{ #!/usr/bin/env ruby # coding: utf-8 require 'net/http' require 'uri' require 'rubygems' require 'json' USERNAME = '_USERNAME_' # ここを書き換える PASSWORD = '_PASSWORD_' # ここを書き換える # URLが変更になってるみたい(2010/07/22) #uri = URI.parse('http://stream.twitter.com/spritzer.json') uri = URI.parse('http://stream.twitter.com/1/statuses/sample.json') Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP::Get.new(uri.request_uri) # Streaming APIはBasic認証のみ request.basic_auth(USERNAME, PASSWORD) http.request(request) do |response| raise 'Response is not chuncked' unless response.chunked? response.read_body do |chunk| # 空行は無視する = JSON形式でのパースに失敗したら次へ status = JSON.parse(chunk) rescue next # 削除通知など、'text'パラメータを含まないものは無視して次へ next unless status['text'] user = status['user'] puts "#{user['screen_name']}: #{status['text']}" end end end }} *Tweepy(Python)のサンプルコード [#r2370c91] #pre{{ #!/usr/bin/python # -*- coding: utf-8 -*- import sys import tweepy import simplejson #from pit import Pit class StreamListener(tweepy.StreamListener): def on_data(self, data): data = simplejson.loads(data) if data.has_key('text'): print data['text'] def main(): user = 'xxxxx' passwd = 'yyyyy' stream = tweepy.Stream(user, passwd, StreamListener()) stream.filter(track=('#nhk',)) if __name__ == "__main__": main() }} streaming.pyで処理が行われている。 #pre{{ def filter(self, follow=None, track=None, async=False): params = {} self.headers['Content-type'] = "application/x-www-form-urlencoded" if self.running: raise TweepError('Stream object already connected!') self.url = '/%i/statuses/filter.json?delimited=length' % STREAM_VERSION if follow: params['follow'] = ','.join(map(str, follow)) if track: params['track'] = ','.join(map(str, track)) self.body = urllib.urlencode(params) self._start(async) }}