Twitterのコマンドラインクライアント

#!/usr/bin/perl

use strict;
use warnings;

use YAML;
use Encode;
use Pod::Usage;
use Net::Twitter;
use File::Spec;
use File::HomeDir;
use Term::Prompt;
use Getopt::Long;
use Term::Encoding;

our $config;
our $twitter;
our %args   = ();

main();

sub main {
    setup_encoding();
    setup_config();
    setup_client();

    my %commands = (
        update => \&update,
        friends  => \&friends,
        friends_timeline  => \&friends_timeline,
        public_timeline   => \&public_timeline,
        followers  => \&followers,
    );

    my $command = $ARGV[0] || "friends";
    $command eq 'update' && !$ARGV[1] && pod2usage(1);
    $commands{$command} or pod2usage(-message => "Unknown command: $command", -exitval => 2);
    $commands{$command}->();

}

sub setup_encoding {
    my $encoding;
    eval {
        require Term::Encoding;
        $encoding = Term::Encoding::get_encoding();
    };

    $encoding ||= "utf-8";

    binmode STDOUT, ":encoding($encoding)";
    binmode STDIN, ":encoding($encoding)";
}

sub setup_config {
    my $path   = File::Spec->catfile(File::HomeDir->my_home, ".twitter");
    $config = eval { YAML::LoadFile($path) } || {};

    my $changed;
    while (!$config->{username} || !$config->{password}) {
        $config->{username} = prompt('x', 'username: ', '', '');
        $config->{password} = prompt('p', 'password: ', '', '');
        $changed++;
    }

    save_config($path, $config) if $changed;
}

sub save_config {
    my ($path, $config ) =@_;
    YAML::DumpFile($path, $config);
    chmod 0600, $path;
}

sub setup_client {
    $twitter   =  Net::Twitter->new(
        username  => $config->{username},
        password  => $config->{password},
    );
}

# commands
sub update {
    my $response = $twitter->update($ARGV[1]);
    print_update_status();
}

sub friends {
    my $response = $twitter->friends;
    print_status($response);

}

sub public_timeline {
    my $response = $twitter->public_timeline;
    print_timeline($response);
}

sub followers {
    my $response = $twitter->followers;
    print_status($response);
}

sub friends_timeline {
    my $response = $twitter->friends_timeline;
    print_timeline($response);
}

# print
sub print_status {
    my ($response) = @_;
    for (@{$response}) {
        printf "%s: %s\n", $_->{name}, $_->{status}{text};
    }

}

sub print_timeline {
    my ($response) = @_;
    for (@{$response}) {
        printf "%s: %s\n", $_->{user}{name}, $_->{text};
    }
}

sub print_update_status {
    printf "Set status: %s\n", $ARGV[1];
}


__END__


=head1 SYNOPSIS

  twitter.pl update <status>
      Set your current status. 

  twitter.pl friends
      Show most recent status of those you have marked as friends in twitter.

  twitter.pl public_timeline
      Show the public timeline of all twitter users

  twitter.pl friends_timeline
      Show the timeline of those you have marked as friends in twitter.

  twitter.pl followers
      Show the timeline of those who follow your status in twitter.

=cut

途中まで作って http://blog.kentarok.org/2007/04/05/232531.html を発見 orz. メッセージ表示部分をパクらせてもらいました。次はRuby版つくろうかな。