[48] | 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 | |
---|
[50] | 27 | eval { |
---|
| 28 | foreach my $id (@ARGV) { |
---|
| 29 | my $res = $bot->show_status($id); |
---|
| 30 | use Data::Dumper; |
---|
| 31 | print Dumper $res; |
---|
[48] | 32 | } |
---|
[50] | 33 | }; |
---|
| 34 | if ($@) { |
---|
| 35 | evalrescue($@); |
---|
[48] | 36 | } |
---|
[50] | 37 | print "done\n"; |
---|
[48] | 38 | |
---|
| 39 | |
---|
| 40 | sub loadconf { |
---|
| 41 | # load configration data from yaml formatted file |
---|
| 42 | # param => scalar string of filename |
---|
| 43 | # ret => hash object of yaml data |
---|
| 44 | |
---|
| 45 | my $file = shift @_; |
---|
| 46 | |
---|
| 47 | my $yaml = YAML::Tiny->read($file); |
---|
| 48 | |
---|
| 49 | if ($!) { |
---|
| 50 | warn "$0: '$file' $!\n"; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | return $yaml->[0]; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | sub login { |
---|
| 57 | # make Net::Twitter::Lite object and login |
---|
| 58 | # param => hash object of configration |
---|
| 59 | # ret => Net::Twitter::Lite object |
---|
| 60 | |
---|
| 61 | my $conf = shift @_; |
---|
| 62 | |
---|
| 63 | my $bot = Net::Twitter::Lite->new( |
---|
| 64 | consumer_key => $conf->{consumer_key}, |
---|
| 65 | consumer_secret => $conf->{consumer_secret}, |
---|
| 66 | ); |
---|
| 67 | |
---|
| 68 | $bot->access_token($conf->{access_token}); |
---|
| 69 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 70 | |
---|
| 71 | return $bot; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | sub evalrescue { |
---|
| 75 | # output error message at eval error |
---|
| 76 | |
---|
| 77 | use Scalar::Util qw(blessed); |
---|
| 78 | |
---|
| 79 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 80 | warn $@->error; |
---|
| 81 | if ($@->twitter_error) { |
---|
| 82 | my %twitter_error = %{$@->twitter_error}; |
---|
| 83 | map { |
---|
| 84 | $twitter_error{"$_ => "} = $twitter_error{$_} . "\n"; |
---|
| 85 | delete $twitter_error{$_} |
---|
| 86 | } keys %twitter_error; |
---|
| 87 | warn join("", %twitter_error); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | else { |
---|
| 91 | warn $@; |
---|
| 92 | } |
---|
| 93 | } |
---|