Home > Mac > | Perl > wassr2growl.pl

wassr2growl.pl

Wassrの、自分が購読中の人のヒトコト (friends_timeline) をGrowlに通知する

初めて実行した時に ~/.wassr2growl.yaml が生成されるので、そこに wassr の ID と PW を記入して、後は cron でよしなにまわすとよろし

2008-08-01 Net::Wassr で書き直し & friends_timeline だけでなく channel_timeline も取得するようにしました
2008-07-31 初回実行時に、id と pw の入力をうながすようにしました

#!/usr/bin/perl

use strict;
use warnings;

use Net::Wassr;

use YAML;
use File::HomeDir qw(home);
use File::Spec;
use IO::Prompt;
use Encode;
use HTTP::Date;

use Mac::Growl;

my $VERSION = '0.4';

my $app = (File::Spec->splitpath($0))[2];
my $conf_file = File::Spec->catfile(home, '.wassr2growl.yaml');

my $conf = { lastupdate => 0 };
unless ( -e $conf_file ) {
    $conf->{user} = prompt('user: ').'';
    $conf->{passwd} = prompt('passwd: ', -echo => '*').'';
    YAML::DumpFile($conf_file, $conf);
    chmod 0600, $conf_file;
    Mac::Growl::RegisterNotifications(
                                      $app,
                                      ['all-timeline'],
                                      ['all-timeline']
                                     );
}
$conf = YAML::LoadFile($conf_file);

my $wassr = Net::Wassr->new(
                            user => $conf->{user},
                            passwd => $conf->{passwd}
                           );

# Friend TimeLine
my $messages = $wassr->friends_timeline();

# Channel TimeLine
my $clist = $wassr->channel_user();
for my $c ( @{$clist->{channels}} ) {
    my $r = $wassr->channel_timeline($c);
    for ( @$r ) {
        push @$messages, {
                          name_en => $c->{name_en},
                          epoch => HTTP::Date::str2time( $_->{created_on} ),
                          user => {
                                   screen_name => $_->{user}->{nick}
                                  },
                          text => $_->{body},
                         };
    }
}

# sort by epoch
my @mes = sort { $b->{epoch} <=> $a->{epoch} } @$messages;

my @allmes = ();
for my $re ( @mes ) {
    last if $re->{epoch} <= $conf->{lastupdate};

    my $str .= sprintf('(%02d:%02d:%02d) ',reverse((gmtime($re->{epoch}+9*3600))[0..2]));
    if ( $re->{reply_message} ) {
        $str .= '> '.$re->{reply_message};
        $str .= ' by '. $re->{reply_user_nick} if  $re->{reply_user_nick};
        $str .= "\n";
    }
    elsif ( $re->{name_en} ) {
        $str .= '[#'.$re->{name_en}.'] ';
    }
    $str .= $re->{text} . ' by ' . $re->{user}->{screen_name};
    if ( utf8::is_utf8($str) ) {
        $str = encode('utf8', $str);
    }
    $str =~ s/[\x00-\x1f]//g;
    push @allmes, $str;
}

if ( scalar(@allmes) ) {
    $conf->{lastupdate} = $mes[0]->{epoch};
    YAML::DumpFile($conf_file, $conf);
    chmod 0600, $conf_file;

    Mac::Growl::PostNotification(
                                 $app,
                                 'all-timeline',
                                 $conf->{user}.'@Wassr',
                                 join("\n----\n",@allmes),
                                 1
                                );
}

exit;

Comments:0

Comment Form

Home > Mac > | Perl > wassr2growl.pl

Feeds

Return to page top