Created
July 26, 2010 06:47
-
-
Save yoshiki/490253 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/local/bin/perl | |
use strict; | |
use warnings; | |
use Config::Pit; | |
use HTTP::Headers; | |
use HTTP::Request::Common; | |
use LWP::UserAgent; | |
use AnyEvent::HTTP; | |
use MIME::Base64 qw( encode_base64 ); | |
use JSON::Syck; | |
my $twitter_config = pit_get("twitter.com"); | |
my $imkayac_config = pit_get("im.kayac.com"); | |
my $auth_info = join ':', @$twitter_config{qw( username password )}; | |
my $auth_header = HTTP::Headers->new( | |
Authorization => 'Basic ' . encode_base64( $auth_info ), | |
); | |
my $callback = sub { | |
my ( undef, $json ) = @_; | |
my $event = $json->{ event } or return; | |
my $source_screen_name = $json->{ source }{ screen_name }; | |
my $target_screen_name = $json->{ target }{ screen_name }; | |
if ( $event eq 'favorite' | |
&& $target_screen_name eq $twitter_config->{ username } ) { | |
my $text = $json->{ target_object }{ text }; | |
my $req = POST 'http://im.kayac.com/api/post/' . $imkayac_config->{ username }, [ | |
message => "$source_screen_name added to favorite $text", | |
password => $imkayac_config->{ password }, | |
handler => 'Twitter://', | |
]; | |
my %headers = map { $_ => $req->header($_), } $req->headers->header_field_names; | |
my $r; | |
$r = http_post $req->uri, $req->content, headers => \%headers, sub { undef $r }; | |
} | |
}; | |
my $cv = AE::cv; | |
http_request( | |
GET => 'http://chirpstream.twitter.com/2b/user.json', | |
headers => $auth_header, | |
want_body_handle => 1, | |
sub { | |
my $hdl = shift; | |
$hdl->on_read( sub { shift->push_read( json => $callback ) } ); | |
$hdl->on_error( sub { warn "Error occured $_[2]" } ); | |
} | |
); | |
$cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment