#!/Perl5.8/bin/perl.exe
# Title = zed2twitter
# Para =< tmp.log
# Input = tmp.log
# Output = tmp.log
use strict;
use warnings;
use YAML;
use Encode;
use Net::Twitter;
use File::Spec;
use File::HomeDir;
use Term::Prompt;
use Getopt::Long;
use Term::Encoding;
$| = 1;# for Zed
our $config;
our $twitter;
our $encoding;
our $status;
our $input;
our $baseurl = "http://homepage1.nifty.com/kazuf/renewal.html";
main();
sub main {
setup_encoding();
setup_config();
setup_client();
my %methods = (
update => \&update,
);
my $method;
# only on Zed
while(<>){
if (index($_, "\x1A") > -1) {
last;
}
chomp;
$input = $_;
}
## twitter methods with ddl
if($input =~ /^(.+?): (.+)$/){
if($1 eq 'update'){
$method = 'update';
$status = decode($encoding, $2);
}
## read a title and a url in an article of renewal.html
}elsif($input =~ /^
(.+?)<\/A><\/div>$/){
$method = 'update';
$status = decode($encoding, $1 . $3 . " ${baseurl}#" . $2);
## read directly text data on Zed
}elsif($input =~ /^.+$/){
$method = 'update';
$status = decode($encoding, $input);
}else{
print "No data!!!\n";
exit;
}
$methods{$method}->();
}
sub setup_encoding {
$encoding = Term::Encoding::get_encoding();
$encoding ||= "utf8";
print "Encoding: ", $encoding, "\n";
}
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},
);
}
# STATUS METHODS
sub update {
eval { my $response = $twitter->update($status) };
if( $@ ) {
warn "update failed because: $@\n";
}
print_update_status();
}
# print
sub print_update_status {
if($ARGV[1]){
printf "Set status: %s\n", $ARGV[1];
}else{
printf "Set status: %s\n", encode($encoding, $status);
}
}
__END__