use Net::Twitter::Lite::WithAPIv1_1 instead of Net::Twitter::Lite
[lab.git] / Dev / twitter / show_status.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 ## IMPORTANT ##
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::WithAPIv1_1;
13 use Data::Dumper;
14
15 my $bot = Net::Twitter::Lite::WithAPIv1_1->new(
16     legacy_lists_api => 0,
17 );
18
19 my $dump;
20 if (defined $ARGV[0] and $ARGV[0] eq "-d") {
21     $dump = shift @ARGV;
22 }
23
24 eval {
25     foreach my $id (@ARGV) {
26         $id =~ /\/?(\d+)$/;
27         $id = $1;
28         my $res = $bot->show_status($id);
29         foreach my $line (split /\n/, Dumper $res) {
30             if ($line =~ /undef/) { next; }
31             if (! $dump) {
32                 unless ($line =~ / => {/
33                     ||  $line =~ / = /
34                     ||  $line =~ /status/
35                     ||  $line =~ /'text'/
36                     ||  $line =~ /created/
37                     ||  $line =~ /'id'/
38                     ||  $line =~ /name/
39                     ||  $line =~ / },/
40                     ||  $line =~ / };/
41                 ) { next; }
42             }
43             print $line, "\n";
44         }
45     }
46 };
47 if ($@) {
48     evalrescue($@);
49 }
50
51
52 sub evalrescue {
53     # output error message at eval error
54     
55     use Scalar::Util qw(blessed);
56     
57     if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
58         warn $@->error;
59         if ($@->twitter_error) {
60             my $twitter_error = $@->twitter_error;
61             if (defined $twitter_error->{errors}) {
62                 foreach my $error (@{$twitter_error->{errors}}) {
63                     warn "code => "   , $error->{code}, "\n";
64                     warn "message => ", $error->{message}, "\n";
65                 }
66             }
67             else {
68                 # unknown HASH structure
69                 use Data::Dumper;
70                 warn Dumper $twitter_error;
71             }
72         }
73     }
74     else {
75         warn $@;
76     }
77 }