[ab38dc9] | 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 |
---|
[e960958] | 12 | use Net::Twitter::Lite::WithAPIv1_1; |
---|
[ab38dc9] | 13 | use FindBin qw($Bin); |
---|
| 14 | use YAML::Tiny; |
---|
| 15 | use Data::Dumper; |
---|
| 16 | use Encode; |
---|
| 17 | |
---|
| 18 | my $help = sub { |
---|
| 19 | die <<EOM; |
---|
| 20 | usage: $0 [text_file_to_tweet] |
---|
| 21 | EOM |
---|
| 22 | }; |
---|
| 23 | if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) { |
---|
| 24 | &{$help}; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | my $file = $ARGV[0] || 'update.log'; |
---|
| 28 | my $conf = loadconf("$Bin/config.yml"); |
---|
| 29 | if (! defined $conf) { |
---|
| 30 | die "$0: cannot parse config file."; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | my $bot = login($conf); |
---|
| 34 | if (! $bot->authorized) { |
---|
| 35 | die "$0: this client is not yet authorized."; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | open TWEET, $file or die "$0: cannot open file: $file"; |
---|
| 39 | eval { |
---|
| 40 | my $update = ""; |
---|
| 41 | my $res; |
---|
| 42 | foreach my $line (<TWEET>) { |
---|
| 43 | chomp $line; |
---|
| 44 | $update .= $line; |
---|
| 45 | } |
---|
| 46 | if (length $update) { |
---|
| 47 | $update = decode('utf8', $update); |
---|
| 48 | $res = $bot->update($update); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | foreach my $line (split /\n/, Dumper $res) { |
---|
| 52 | if ($line =~ /undef/) { next; } |
---|
| 53 | print STDERR $line, "\n"; |
---|
| 54 | } |
---|
| 55 | if (ref $res) { |
---|
| 56 | my $status = $res; |
---|
| 57 | my $text = ""; |
---|
| 58 | $text .= "(". $status->{id} . ") "; |
---|
| 59 | $text .= ($status->{user}{name}) ? $status->{user}{name} : $status->{from_user}; |
---|
| 60 | $text .= " [" . $status->{created_at} . "]"; |
---|
| 61 | $text .= " ". $status->{text}; |
---|
| 62 | $text =~ s/\n//; |
---|
| 63 | print encode('sjis', $text), "\n"; |
---|
| 64 | } |
---|
| 65 | }; |
---|
| 66 | if ($@) { |
---|
| 67 | evalrescue($@); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | sub loadconf { |
---|
| 72 | # load configration data from yaml formatted file |
---|
| 73 | # param => scalar string of filename |
---|
| 74 | # ret => hash object of yaml data |
---|
| 75 | |
---|
| 76 | my $file = shift @_; |
---|
| 77 | |
---|
| 78 | my $yaml = YAML::Tiny->read($file); |
---|
| 79 | |
---|
| 80 | if ($!) { |
---|
| 81 | warn "$0: '$file' $!"; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | return $yaml->[0]; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | sub login { |
---|
[e960958] | 88 | # make Net::Twitter::Lite::WithAPIv1_1 object and login |
---|
[ab38dc9] | 89 | # param => hash object of configration |
---|
[e960958] | 90 | # ret => Net::Twitter::Lite::WithAPIv1_1 object |
---|
[ab38dc9] | 91 | |
---|
| 92 | my $conf = shift @_; |
---|
| 93 | |
---|
[e960958] | 94 | my $bot = Net::Twitter::Lite::WithAPIv1_1->new( |
---|
[ab38dc9] | 95 | consumer_key => $conf->{consumer_key}, |
---|
| 96 | consumer_secret => $conf->{consumer_secret}, |
---|
[43d739e] | 97 | legacy_lists_api => 0, |
---|
[ab38dc9] | 98 | ); |
---|
| 99 | |
---|
| 100 | $bot->access_token($conf->{access_token}); |
---|
| 101 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 102 | |
---|
| 103 | return $bot; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | sub evalrescue { |
---|
| 107 | # output error message at eval error |
---|
| 108 | |
---|
| 109 | use Scalar::Util qw(blessed); |
---|
| 110 | |
---|
| 111 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 112 | warn $@->error; |
---|
| 113 | if ($@->twitter_error) { |
---|
[a5b6bf1] | 114 | my $twitter_error = $@->twitter_error; |
---|
| 115 | if (defined $twitter_error->{errors}) { |
---|
| 116 | foreach my $error (@{$twitter_error->{errors}}) { |
---|
| 117 | warn "code => " , $error->{code}, "\n"; |
---|
| 118 | warn "message => ", $error->{message}, "\n"; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | else { |
---|
| 122 | # unknown HASH structure |
---|
| 123 | use Data::Dumper; |
---|
| 124 | warn Dumper $twitter_error; |
---|
| 125 | } |
---|
[ab38dc9] | 126 | } |
---|
| 127 | } |
---|
| 128 | else { |
---|
| 129 | warn $@; |
---|
| 130 | } |
---|
| 131 | } |
---|