Source File: twitter_send.pl

 00 #!/usr/bin/perl
 01 # twitter_send.pl
 02 # Copyright (c) 2010, Naomasa Matsubayashi
 03 # All rights reserved.
 04 #
 05 # Redistribution and use in source and binary forms,
 06 # with or without modification, are permitted provided
 07 # that the following conditions are met:
 08 #
 09 #  * Redistributions of source code must retain the
 10 #    above copyright notice, this list of conditions
 11 #    and the following disclaimer.
 12 #  * Redistributions in binary form must reproduce the
 13 #    above copyright notice, this list of conditions
 14 #    and the following disclaimer in the documentation
 15 #    and/or other materials provided with the
 16 #    distribution.
 17 #
 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
 19 # AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 20 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 23 # SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 26 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 29 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 32 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 33 
 34 use utf8;
 35 use strict;
 36 use integer;
 37 use Text::Iconv;
 38 use Net::Twitter;
 39 
 40 sub loginTwitter() {
 41   print "Username : ";
 42   chomp( my $username = <STDIN> );
 43   print "Password : ";
 44   system "stty -echo";
 45   chomp( my $password = <STDIN> );
 46   system "stty echo";
 47   print "\n";
 48   my $twitter = Net::Twitter->new(
 49     username => $username,
 50     password => $password
 51   );
 52   return $twitter;
 53 }
 54 
 55 sub getSystemCharCode() {
 56   my $code = "utf8";
 57   if( exists( $ENV{"LANG"} ) ) {
 58     $code = $ENV{"LANG"};
 59   }
 60   if( exists( $ENV{"LC_MESSAGE"} ) ) {
 61     $code = $ENV{"LC_MESSAGE"};
 62   }
 63   if( exists( $ENV{"LC_ALL"} ) ) {
 64     $code = $ENV{"LC_ALL"};
 65   }
 66   $code =~ s/\A.*\.//;
 67   return $code;
 68 }
 69 
 70 sub updateTwitter( $$ ) {
 71   my $twitter = $_[ 0 ];
 72   my $text = $_[ 1 ];
 73   my $converter = Text::Iconv->new(  getSystemCharCode(), "utf8" );
 74   $twitter->update( $converter->convert( $text ) );
 75 }
 76 
 77 binmode STDIN,  ":" . getSystemCharCode();
 78 binmode STDOUT, ":" . getSystemCharCode();
 79 my $twitter = loginTwitter();
 80 updateTwitter( $twitter, $ARGV[ 0 ] );