Created
January 7, 2009 01:20
-
-
Save mattn/44118 to your computer and use it in GitHub Desktop.
post to im.kayac.com
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/bin/perl | |
use strict; | |
use warnings; | |
use Config::Pit; | |
use Digest::SHA1 qw(sha1_hex); | |
use Encode; | |
use Getopt::Long; | |
use JSON; | |
use LWP::UserAgent; | |
eval { | |
require Win32::API; | |
Win32::API->Import( 'kernel32', 'UINT GetACP()' ); | |
Encode::from_to( $ARGV[$_], 'cp' . GetACP(), 'utf-8' ) | |
for ( 0 .. @ARGV - 1 ); | |
} if $^O eq 'MSWin32'; | |
my $config = pit_get( | |
"im.kayac.com", | |
require => { | |
"username" => "your username on im.kayac.com", | |
"password" => "your password(or secretkey) on im.kayac.com", | |
"authtype" => "auth type on im.kayac.com: none/password/secretkey", | |
} | |
); | |
my $file = ''; | |
GetOptions( 'file=s' => \$file ) or die "Usage: $0 [-f file] [message]'"; | |
my %data = ( message => shift ); | |
if ($file) { | |
open my $fh, '<', $file or die "can't open file $file"; | |
$data{message} = do { local $/; <$fh> }; | |
close $fh; | |
} | |
die "should be specify message" unless $data{message}; | |
$data{password} = $config->{password} | |
if $config->{authtype} eq "password"; | |
$data{sig} = sha1_hex( $data{message} . $config->{password} ) | |
if $config->{authtype} eq "secretkey"; | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->post( "http://im.kayac.com/api/post/$config->{username}", | |
\%data, "Content-Type" => "application/x-www-form-urlencoded" ); | |
if ( $res->is_success ) { | |
$res = from_json( $res->decoded_content ); | |
print $res->{result} . $res->{error}, "\n"; | |
} | |
else { | |
die 'failed to post'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment