Twitter Streaming API
Twitter Streaming APIを使用する際のメモ。
以下の記事を参考にさせて頂きました。
node.js + socket.ioでTwitterのStreaming APIを使ってみる
TwitterのAPIはRESTとSearchに分かれてるみたい。
今回はSearchを使用する。
npmはやはりExpressでひな壇作成する度に必要っぽい。
socket.ioも前回同様に入れておく。
ntwitterの導入
npm install ntwitter
npm http GET https://registry.npmjs.org/ntwitter
npm http 200 https://registry.npmjs.org/ntwitter
npm http GET https://registry.npmjs.org/ntwitter/-/ntwitter-0.3.0.tgz
npm http 200 https://registry.npmjs.org/ntwitter/-/ntwitter-0.3.0.tgz
npm http GET https://registry.npmjs.org/oauth
npm http GET https://registry.npmjs.org/cookies
npm http GET https://registry.npmjs.org/keygrip
npm http 200 https://registry.npmjs.org/keygrip
npm http GET https://registry.npmjs.org/keygrip/-/keygrip-0.1.7.tgz
npm http 200 https://registry.npmjs.org/oauth
npm http GET https://registry.npmjs.org/oauth/-/oauth-0.9.6.tgz
npm http 200 https://registry.npmjs.org/cookies
npm http GET https://registry.npmjs.org/cookies/-/cookies-0.1.6.tgz
npm http 200 https://registry.npmjs.org/keygrip/-/keygrip-0.1.7.tgz
npm http 200 https://registry.npmjs.org/oauth/-/oauth-0.9.6.tgz
npm http 200 https://registry.npmjs.org/cookies/-/cookies-0.1.6.tgz
> keygrip@0.1.7 install /Users/ユーザ名/Desktop/node/NodeTwitter/node_modules/ntwitter/node_modules/keygrip
> node scripts/install.js
ntwitter@0.3.0 ./node_modules/ntwitter
├── cookies@0.1.6
├── keygrip@0.1.7
└── oauth@0.9.6
カスタマーキー等の取得
ここでソース中に登場するmodule.exportsに疑問。
module.exportsとexportsって何が違うんだろ?
と思っていたら以下サイトに詳しく載っていました。
Node.js : exports と module.exports の違い(解説編)
さくっと動作完了
app.js
>// Twitter Streaming APIを呼び出す
> twitter.stream('statuses/filter', {'track': keyword},function(stream) {
> stream.on('data', function (data) {
> io.sockets.emit('message',data.text);
> });
> stream.on('end', function (response) {
> // 切断された場合の処理
> });
> stream.on('destroy', function (response) {
> // 接続が破棄された場合の処理
> });
> });
でtwitter.streamのコールバックでdataが来たらio.socketsのイベントとしてmessageをemitして、
index.ejs
>socket.on('message', function(t){
> $('<div></div>')
> .html('<li>' + t + '</li>')
> .prependTo('#tweets');
>});
で、画面に表示をしている。
Twitter Streaming APIの精度とかとれる情報とか
http://q.hatena.ne.jp/1266577494
http://watcher.moe-nifty.com/memo/docs/twitterAPI.txt
次は検索結果取得系もやってみたいなぁー。