Source File: twitter.pl 000 #!/usr/bin/perl 001 # twitter.pl 002 # Copyright (c) 2010, Naomasa Matsubayashi 003 # All rights reserved. 004 # 005 # Redistribution and use in source and binary forms, 006 # with or without modification, are permitted provided 007 # that the following conditions are met: 008 # 009 # * Redistributions of source code must retain the 010 # above copyright notice, this list of conditions 011 # and the following disclaimer. 012 # * Redistributions in binary form must reproduce the 013 # above copyright notice, this list of conditions 014 # and the following disclaimer in the documentation 015 # and/or other materials provided with the 016 # distribution. 017 # 018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 019 # AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 020 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 021 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 022 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 023 # SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 024 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 025 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 026 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 027 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 028 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 029 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 030 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 032 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 033 034 use utf8; 035 use strict; 036 use integer; 037 use Net::Twitter; 038 use Text::Iconv; 039 040 sub loginTwitter() { 041 print "Username : "; 042 chomp( my $username = <STDIN> ); 043 print "Password : "; 044 system "stty -echo"; 045 chomp( my $password = <STDIN> ); 046 system "stty echo"; 047 print "\n"; 048 my $twitter = Net::Twitter->new( 049 traits => [qw/API::REST/], 050 username => $username, 051 password => $password 052 ); 053 return $twitter; 054 } 055 056 sub getTimeline( $$ ) { 057 my $twitter = $_[ 0 ]; 058 my $since = $_[ 1 ]; 059 my $status; 060 if( $since != 0 ) { 061 $status = $twitter->friends_timeline({ since_id => $since, count => 100 }); 062 } 063 else { 064 $status = $twitter->friends_timeline({ count => 20 }); 065 } 066 return $status; 067 } 068 069 sub getSystemCharCode() { 070 my $code = "utf8"; 071 if( exists( $ENV{"LANG"} ) ) { 072 $code = $ENV{"LANG"}; 073 } 074 if( exists( $ENV{"LC_MESSAGE"} ) ) { 075 $code = $ENV{"LC_MESSAGE"}; 076 } 077 if( exists( $ENV{"LC_ALL"} ) ) { 078 $code = $ENV{"LC_ALL"}; 079 } 080 $code =~ s/\A.*\.//; 081 return $code; 082 } 083 084 sub displayTimeline( $$ ) { 085 my $timeline = $_[ 0 ]; 086 my $current_head = $_[ 1 ]; 087 if( exists( ${$timeline}[ 0 ] ) ) { 088 my @reversed_timeline; 089 my $converter = Text::Iconv->new( "utf8", getSystemCharCode() ); 090 foreach my $status ( @$timeline ) { 091 push( @reversed_timeline, $converter->convert( ${status}->{user}->{screen_name}. " : ". ${status}->{text} . "\n" ) ); 092 } 093 foreach my $status ( @$timeline ) { 094 print( pop( @reversed_timeline ) ); 095 } 096 $current_head = ${$timeline}[ 0 ]->{id}; 097 } 098 return $current_head; 099 } 100 101 102 binmode STDIN, ":" . getSystemCharCode(); 103 binmode STDOUT, ":" . getSystemCharCode(); 104 105 my $twitter = loginTwitter(); 106 my $current_head = 0; 107 while( 1 ) { 108 my $timeline = getTimeline( $twitter, $current_head ); 109 $current_head = displayTimeline( $timeline, $current_head ); 110 sleep( 120 ); 111 }