* skip unimportant elements from the Dump
[lab.git] / 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;
13 use FindBin qw($Bin);
14 use YAML::Tiny;
15
16 my $conf = loadconf("$Bin/config.yml");
17 if (! defined $conf) {
18     die "$0: cannot parse config file.\n";
19 }
20
21 my $bot = login($conf);
22 if (! $bot->authorized) {
23     die "$0: this client is not yet authorized.\n";
24 }
25
26
27 eval {
28     foreach my $id (@ARGV) {
29         my $res = $bot->show_status($id);
30         use Data::Dumper;
31         foreach my $line (split /\n/, Dumper $res) {
32             if ($line =~ /undef/) { next; }
33             unless ($line =~ / => {/
34                 ||  $line =~ / = /
35                 ||  $line =~ /status/
36                 ||  $line =~ /'text'/
37                 ||  $line =~ /created/
38                 ||  $line =~ /'id'/
39                 ||  $line =~ /name/
40                 ||  $line =~ / },/
41                 ||  $line =~ / };/
42             ) { next; }
43             print $line, "\n";
44         }
45     }
46 };
47 if ($@) {
48     evalrescue($@);
49 }
50 print "done\n";
51
52
53 sub loadconf {
54     # load configration data from yaml formatted file
55     #   param   => scalar string of filename
56     #   ret     => hash object of yaml data
57     
58     my $file = shift @_;
59     
60     my $yaml = YAML::Tiny->read($file);
61     
62     if ($!) {
63         warn "$0: '$file' $!\n";
64     }
65     
66     return $yaml->[0];
67 }
68
69 sub login {
70     # make Net::Twitter::Lite object and login
71     #   param   => hash object of configration
72     #   ret     => Net::Twitter::Lite object
73     
74     my $conf = shift @_;
75     
76     my $bot = Net::Twitter::Lite->new(
77         consumer_key    => $conf->{consumer_key},
78         consumer_secret => $conf->{consumer_secret},
79     );
80     
81     $bot->access_token($conf->{access_token});
82     $bot->access_token_secret($conf->{access_token_secret});
83     
84     return $bot;
85 }
86
87 sub evalrescue {
88     # output error message at eval error
89     
90     use Scalar::Util qw(blessed);
91     
92     if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
93         warn $@->error;
94         if ($@->twitter_error) {
95             my %twitter_error = %{$@->twitter_error};
96             map {
97                 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
98                 delete $twitter_error{$_}
99             } keys %twitter_error;
100             warn join("", %twitter_error);
101         }
102     }
103     else {
104         warn $@;
105     }
106 }