File Coverage

blib/lib/App/Twpin.pm
Criterion Covered Total %
statement 33 132 25.0
branch 0 64 0.0
condition 0 18 0.0
subroutine 11 22 50.0
pod 0 8 0.0
total 44 244 18.0


line stmt bran cond sub pod time code
1             package App::Twpin;
2 1     1   689 use strict;
  1         2  
  1         33  
3 1     1   6 use warnings;
  1         1  
  1         29  
4 1     1   12500 use FindBin;
  1         1124  
  1         41  
5 1     1   3818 use Tie::File;
  1         24451  
  1         31  
6 1     1   3040 use Env qw/HOME/;
  1         3177  
  1         8  
7 1     1   139 use Carp qw/carp croak/;
  1         1  
  1         41  
8 1     1   4 use Scalar::Util qw/blessed/;
  1         2  
  1         64  
9 1     1   817 use File::Spec::Functions;
  1         673  
  1         87  
10 1     1   2994 use Encode qw/encode decode is_utf8 find_encoding/;
  1         16073  
  1         103  
11 1     1   8 use base qw/Exporter/;
  1         2  
  1         146  
12              
13             our $VERSION = 0.007;
14              
15             our @EXPORT = qw//;
16             our @EXPORT_OK =
17             qw/tw_config tw_update tw_load_config tw_list tw_get_follower tw_get_following tw_follow tw_unfollow/;
18             our %EXPORT_TAGS = (
19             all => [
20             qw/tw_config tw_update tw_load_config tw_list tw_get_follower tw_get_following tw_follow tw_unfollow/
21             ]
22             );
23              
24             my $config_file = catfile($HOME, '.twpinrc');
25             if ($^O eq 'MSWin32') {
26 1     1   889 use autouse FindBin => qw($Bin);
  1         636  
  1         5  
27             $config_file = catfile($Bin, '.twpinrc');
28             }
29              
30             sub tw_config {
31 0     0 0   my %args = @_;
32 0           my @array;
33 0           my %has = (user => 0, pass => 0, apiurl => 0, encode => 0);
34 0 0         tie @array, 'Tie::File', $config_file or croak $!;
35 0           foreach (@array) {
36 0 0         if (/^user\s*=/) {
    0          
    0          
    0          
    0          
37 0 0         defined $args{user} and $_ = "user = $args{user}";
38 0           $has{user}++;
39             } elsif (/^pass\s*=/) {
40 0 0         defined $args{pass} and $_ = "pass = $args{pass}";
41 0           $has{pass}++;
42             } elsif (/^apiurl\s*=/) {
43 0 0         defined $args{apiurl} and $_ = "apiurl = $args{apiurl}";
44 0           $has{apiurl}++;
45             } elsif (/^encode\s*=/) {
46 0 0         defined $args{encode} and $_ = "encode = $args{encode}";
47 0           $has{encode}++;
48             } elsif (/^\s*$/) {
49 0           next;
50             } else {
51 0           croak "bad configuration: '$_' ";
52             }
53             }
54 0           foreach (keys %args) {
55 0 0         !$has{$_} && push @array, "$_ = $args{$_}";
56             }
57 0           print "configuration updated\n";
58 0           untie @array;
59             }
60              
61             sub tw_load_config {
62              
63 0     0 0   my (%conf, @array);
64 0 0         tie @array, 'Tie::File', $config_file or croak $!;
65              
66 0           foreach (@array) {
67 0 0 0       /^user\s*=\s*(.*?)\s*$/ and $conf{username} = $1 and next;
68 0 0 0       /^pass\s*=\s*(.*?)\s*$/ and $conf{password} = $1 and next;
69 0 0 0       /^apiurl\s*=\s*(.*?)\s*$/ and $conf{apiurl} = $1 and next;
70             }
71              
72 0           untie @array;
73 0 0 0       if (!defined $conf{username} || !defined $conf{password}) {
74 0           croak "Error: username or password missing\n";
75             }
76 0           return %conf;
77             }
78              
79             sub tw_update {
80 0     0 0   my $twc = shift;
81 0           my $encode = _term_encoding();
82 0           foreach my $tweet (@_) {
83 0           eval { $twc->update(decode($encode, $tweet)) };
  0            
84 0 0         if ($@) {
85 0           _error_handle($@);
86             } else {
87 0           print("status updated\n");
88             }
89             }
90             }
91              
92             sub tw_list {
93 0     0 0   my ($twc, $user) = @_;
94 0           my $statuses;
95 0 0         if (defined $user) {
96 0           eval { $statuses = $twc->user_timeline({id => $user}) };
  0            
97             } else {
98 0           eval { $statuses = $twc->friends_timeline() };
  0            
99             }
100 0 0         _error_handle($@) if ($@);
101              
102 0           foreach my $status (@$statuses) {
103 0           my $create = (split '\+', $status->{created_at}, 2)[0];
104 0           print _($status->{user}{screen_name}), "\t", _($create), "\n",
105             _($status->{text}), "\n\n";
106             }
107             }
108              
109             sub tw_get_follower {
110 0     0 0   my $twc = shift;
111 0           my $followers;
112 0           eval { $followers = $twc->followers() };
  0            
113 0 0         _error_handle($@) if $@;
114 0           print scalar @$followers, " followers received from twitter\n\n";
115 0           foreach my $follower (@$followers) {
116 0           printf("%-15s\t%-15s",
117             _($follower->{name}),
118             _($follower->{screen_name}));
119 0 0         printf("\t%s", _($follower->{location}))
120             if defined $follower->{location};
121 0           print "\n";
122             }
123             }
124              
125             sub tw_get_following {
126 0     0 0   my $twc = shift;
127 0           my $following;
128 0           eval { $following = $twc->friends() };
  0            
129 0 0         _error_handle($@) if $@;
130 0           print scalar @$following, " followings received from twitter\n\n";
131 0           foreach my $friend (@$following) {
132 0           printf("%-15s\t%-15s", _($friend->{name}), _($friend->{screen_name}));
133 0 0         printf("\t%s", _($friend->{location})) if defined $friend->{location};
134 0           print "\n";
135             }
136             }
137              
138             sub _error_handle($) {
139 0     0     my $error = shift;
140 0 0 0       if (blessed $error && $error->isa('Net::Twitter::Error')) {
141 0           print STDERR $error->error, "\n";
142 0           exit;
143             } else {
144 0           croak "Unknow Exception: $error";
145             }
146             }
147              
148             sub tw_follow {
149 0     0 0   my ($twc, $screen_name) = @_;
150 0           my $friend;
151 0           eval { $friend = $twc->create_friend($screen_name) };
  0            
152 0 0         _error_handle($@) if $@;
153 0           print "OK, you are now following:\n";
154 0           printf("%-15s\t%-15s", _($friend->{name}), _($friend->{screen_name}));
155 0 0         printf("\t%s", _($friend->{location})) if defined $friend->{location};
156 0           print "\n";
157             }
158              
159             sub tw_unfollow {
160 0     0 0   my ($twc, $screen_name) = @_;
161 0           eval { $twc->destroy_friend($screen_name) };
  0            
162 0 0         _error_handle($@) if $@;
163 0           print("unfollowed\n");
164             }
165              
166             sub _term_encoding {
167 0     0     my ($encode, @array);
168 0 0         tie @array, 'Tie::File', $config_file or croak $!;
169              
170 0           foreach (@array) {
171 0 0 0       /^encode\s*=\s*(.*?)\s*$/ and $encode = $1 and last;
172             }
173 0 0         return 'utf8' unless defined $encode;
174 0 0         croak "Unknow encoding $encode" unless ref(find_encoding($encode));
175 0           return $encode;
176             }
177              
178             sub _ {
179 0     0     my $string = shift;
180 0 0         if (is_utf8($string)) {
181 0           $string = encode _term_encoding(), $string;
182             }
183 0           return $string;
184             }
185              
186             1;
187              
188             __END__
189              
190             =head1 NAME
191              
192             twpin - Just Another Command Line Twitter Client
193              
194             =head1 VERSION
195              
196             version 0.007
197              
198             =head1 SYNOPSIS
199            
200             twpin config -u username -p password
201             twpin config -a 'http://url/twip'
202             twpin update "hello twitter"
203             twpin follow perl_api
204             twpin status
205             twpin help
206              
207             =head1 DESCRIPTION
208              
209             C<twpin> is a script for you to access twitter from command line
210              
211             twip L<http://code.google.com/p/twip/> is a twitter API proxy in PHP. This script is created mainly because I can not find a good twitter client that supports this proxy
212              
213             Note on the recent OAuth-Supported twip, you can not use your email to access twitter, so always use Screename as your username to login
214              
215             Configration file is located at $HOME(or $Bin on MSWin32)/.twpinrc, you can just edit this file and add your username/password there.
216              
217             By default, the term encoding is set to utf8. If you are using other encodings, set 'encode = STH' in the configuration file(or use 'twpin config -e').
218              
219              
220             =head1 AUTHOR
221              
222             woosley.xu<redicaps@gmail.com>
223              
224             =head1 COPYRIGHT & LICENSE
225              
226             This software is copyright (c) 2010 by woosley.xu.
227              
228             This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.