jetpackでtwitter client

何度目かのjavascript

普段xircdを使ってtwitterをチェックしていたのですが,いじっている内に動きがおかしくなってきたこともあって,クライアントを変えることにしました.firefox用のEcofonをしばらく使ってみたのですが,自分が欲しいのはlistなんて機能とは関係なく,リアルタイムにtwitをポップアップしてくれるだけのツールだと気づいたので,作ってみる事にしました.

ポーリングにチャレンジ

最終的にはstreaming APIを使ってリアルタイムにポップアップするようにしたいのですが,まずは肩ならしとしてapi.twitter.comをポーリングするスクリプトを書いてみました.久しぶりに書いたので結構色々なことを忘れていました...jetpackを使うにはjetpack用のjavascriptファイルとそれを呼び出すhtmlファイルを書く必要があります.今回はdropboxのpublicフォルダにjsとhtmlファイルをおいて,それを呼び出すようにしてみました.それぞれの名前をtwitter-notifier.html, twitter-notifier.jsとすると,twitter-notifier.htmlはこんな感じ.

<title>Twitter notifier</title>
<link rel="jetpack" href="twitter-notifier.js">
<h1>Twitter notifier</h1>
<p>Displays a notification bubble whenever you have an unread tweet in
Twitter.</p>
<p>Version 0.2 - 12 Feb 2010</p>
<p>Code is 
<a href="http://dl.dropbox.com/u/USERID/jetpack/TwitterNotifier/twitter-notifier.js">here</a>
.</p>

実際に動くjavascriptは以下のようになります。

jetpack.future.import("storage.simple");

twitter = {
 update: function () {
  $.ajax({
   type: "GET",
   url: "http://twitter.com/statuses/friends_timeline.json?since_id="+store.lastId,
   dataType: "json",
   success: function (tweets) {
    if (tweets.length == 0) return;
     tweets.sort(function (a, b) {
     return a.id - b.id;
    });
    $.each(tweets, function () {
     if (this.id > store.lastId) {
      store.lastId = this.id;
      queue.push({
       title: this.user.name,
       body: this.text,
       icon: this.user.profile_image_url
      });
     }
    });
    twitter.showNotifications();
   },
   error: function (req, status, error) {
    console.log(status + ' ' + error);
   }    
  });
 },

 showNotifications: function () {
  if (queue.length > 0) {
   jetpack.notifications.show(queue.shift());
   notifier = setTimeout(twitter.showNotifications, 6000);
  } else 
   clearTimeout(notifier);
 },
}

store = jetpack.storage.simple;
if (!store.lastId)
 store.lastId = 1;
queue = [];

twitter.update();
setInterval(twitter.update, 60*1000);

このAPIはベーシック認証を使っているので,FireFoxを起動したときにアカウントとパスワードを聞いてきます.後は1分毎にサイトをチェックして,新規twitがあればそれをNotificationとして表示します.私の環境はmacGrowlを使っているので,Growlがポップアップしてきます.

さて,

streamAPIを使うにはどのライブラリを使うと楽かなー