2008年11月17日月曜日

TwootとGrowlの連携



元々TwitterPodを使ってた身としては、Growl通知はどうしても欲しい。


ということで、TwootをGrowl対応させることにした。


で、ググってみた結果以下の事がわかった。



  • Growlには各種プログラミング言語用のバインディングが存在している

  • Growlはネットワーク経由での連携が可能

  • JavaScript -> CGI -> Growl とすることでJavaScriptからでもGrowlを呼び出せる


というわけで、TwootをGrowl対応させることは可能。


まずは以下の記事を参考にしてGrowlを呼び出すCGIを書いてみた。


Big Sky :: Growl for Windowsを試してみた


Growl用WebAPI提供サーバ - Okiraku Programming



#!/opt/local/bin/perl
use lib '/opt/local/lib/perl5/5.8.8';
use lib '/opt/local/lib/perl5/vendor_perl/5.8.8';
use strict;
use warnings;
use Net::Growl;
use utf8;
use CGI;
use Encode;
my $q = new CGI;

my $name = "Message via Web API";
my $title = $q->param("title");
my $msg = $q->param("msg");

register(host => 'localhost',
application =>"Twoot",
password=>"sm05ofgd", );

notify(
application => "Twoot",
title => decode_utf8($title),
description => decode_utf8($msg),
priority => 2,
sticky => 'True',
password => "growl's password",
);

print "Content-type: text/javascript\n\n\"OK\";";


これを/Users/username/Sites 辺りに保存しておく。


もちろんapacheの設定等も必要だけどそこら辺は割愛。


で、次にTwootからこのCGIを呼び出す部分。


twoot.jsの20行目あたりに以下の記述を追加



if($("#msg-" + item.id).length == 0) { // <- fix for twitter caching which sometimes have problems with the "since" parameter
$.get("http://localhost/~username/growl.cgi", { title: item.user.name, msg: item.text}); // <- これを追加
list.prepend('<li id="msg-' + item.id + '"><img class="profile_image" src="' + item.user.profile_image_url + '" alt="' + item.user.name + '" /><span class="time" title="' + item.created_at + '">' + relative_time(item.created_at) + '</span> <a class="user" href="javascript:addAddress(\'' + item.user.screen_name + '\')">' + item.user.screen_name + '</a><div class="tweet_text">' + item.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/g, '<a href="$1">$1</a>').replace(/[\@]+([A-Za-z0-9-_]+)/g, '<a href="http://twitter.com/$1">@$1</a>').replace(/[&#60;]+[3]/g, "<tt class='heart'>&#x2665;</tt>") + '</div></li>');


最後にGrowl自体の設定。


システム環境設定 -> Growl -> ネットワークと進んで、「受信される通知を聞く」と「リモートアプリケーション登録を許可」にチェックを入れてパスワードを入力。さっきのPerlのソースコード中の「growl's password」はこれと対応している。


これでTwootとGrowlの連携は完了。


























で、ここまで書き終わってTwootの最新版をチェックしてみたところ、なんとGrowl対応がされていた…。まあ、いいや。





0 件のコメント: