Created
October 1, 2009 14:05
-
-
Save sugyan/198973 to your computer and use it in GitHub Desktop.
AnyEventを使った簡単ターミナルTwitterクライアント
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use AnyEvent; | |
use AnyEvent::Twitter; | |
use Encode qw/decode_utf8 encode_utf8/; | |
# ユーザー名、パスワードを引数から取得 | |
my ($username, $password) = @ARGV; | |
my $twitty = AnyEvent::Twitter->new( | |
username => $username, | |
password => $password, | |
); | |
# コールバックの登録 | |
$twitty->reg_cb( | |
statuses_friends => sub { | |
my ($twitty, @statuses) = @_; | |
print_statuses(@statuses); | |
}, | |
statuses_mentions => sub { | |
my ($twitty, @statuses) = @_; | |
print_statuses(@statuses); | |
}, | |
); | |
# 観測開始 | |
$twitty->receive_statuses_friends(3); | |
$twitty->receive_statuses_mentions(1); | |
$twitty->start; | |
# 入力待ちしてSTDINからの入力をPOSTする | |
my $cv = AnyEvent->condvar; | |
my $w; $w = AnyEvent->io( | |
fh => \*STDIN, | |
poll => 'r', | |
cb => sub { | |
chomp(my $input = <STDIN>); | |
$twitty->update_status(decode_utf8($input), sub { | |
my ($twitty, $status, $js, $error) = @_; | |
print "update!\n"; | |
}); | |
}, | |
); | |
$cv->recv; | |
# statusを整形して出力 | |
sub print_statuses { | |
my @statuses = @_; | |
for my $status (map { $$_[0] } reverse @statuses) { | |
printf "%s:[%s]%s\n", ( | |
scalar(localtime $status->{timestamp}), | |
$status->{screen_name}, | |
encode_utf8($status->{text}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment