8 # When Net::Twitter::Lite encounters a Twitter API error or a network error,
9 # it throws a Net::Twitter::Lite::Error object.
10 # You can catch and process these exceptions by using eval blocks and testing $@
11 ## from http://search.cpan.org/perldoc?Net::Twitter::Lite#ERROR_HANDLING
12 use Net::Twitter::Lite;
21 [{u}ser_timeline(default)|{r}etweeted_by_me|{m}sentions
30 if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
34 my $method = $ARGV[0] || 'user_timeline';
35 my $screen_name = $ARGV[1] || '';
36 my $pages = $ARGV[2] || 1;
37 if ($pages eq 'all') {
40 my $dump = $ARGV[3] || 0;
42 my $conf = loadconf("$Bin/config.yml");
43 if (! defined $conf) {
44 die "$0: cannot parse config file.";
47 my $bot = login($conf);
48 if (! $bot->authorized) {
49 die "$0: this client is not yet authorized.";
55 while ($pages - $page && $page <= 160) {
58 my $param = ($screen_name)
59 ? { page => $page, screen_name => $screen_name, }
64 if ($method eq 'user_timeline' || $method eq 'u') {
65 $res = $bot->user_timeline($param);
67 elsif ($method eq 'retweeted_by_me' || $method eq 'r') {
68 $res = $bot->retweeted_by_me($param);
70 elsif ($method eq 'mentions' || $method eq 'm') {
71 $res = $bot->mentions($param);
74 warn "$0: unknown method '$method'";
79 foreach my $line (split /\n/, Dumper $res) {
80 if ($line =~ /undef/) { next; }
85 foreach my $status (@{$res}) {
87 $text .= "(". $status->{id} . ") ";
88 $text .= $status->{user}{name};
89 $text .= " [" . $status->{created_at} . "]";
90 $text .= " ". $status->{text};
92 print encode('utf8', $text), "\n";
103 # load configration data from yaml formatted file
104 # param => scalar string of filename
105 # ret => hash object of yaml data
109 my $yaml = YAML::Tiny->read($file);
112 warn "$0: '$file' $!";
119 # make Net::Twitter::Lite object and login
120 # param => hash object of configration
121 # ret => Net::Twitter::Lite object
125 my $bot = Net::Twitter::Lite->new(
126 consumer_key => $conf->{consumer_key},
127 consumer_secret => $conf->{consumer_secret},
130 $bot->access_token($conf->{access_token});
131 $bot->access_token_secret($conf->{access_token_secret});
137 # output error message at eval error
139 use Scalar::Util qw(blessed);
141 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
143 if ($@->twitter_error) {
144 my %twitter_error = %{$@->twitter_error};
146 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
147 delete $twitter_error{$_}
148 } keys %twitter_error;
149 warn join("", %twitter_error);