[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 |
---|
| 12 | use Net::Twitter::Lite; |
---|
| 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 { |
---|
| 88 | # make Net::Twitter::Lite object and login |
---|
| 89 | # param => hash object of configration |
---|
| 90 | # ret => Net::Twitter::Lite object |
---|
| 91 | |
---|
| 92 | my $conf = shift @_; |
---|
| 93 | |
---|
| 94 | my $bot = Net::Twitter::Lite->new( |
---|
| 95 | consumer_key => $conf->{consumer_key}, |
---|
| 96 | consumer_secret => $conf->{consumer_secret}, |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | $bot->access_token($conf->{access_token}); |
---|
| 100 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 101 | |
---|
| 102 | return $bot; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | sub evalrescue { |
---|
| 106 | # output error message at eval error |
---|
| 107 | |
---|
| 108 | use Scalar::Util qw(blessed); |
---|
| 109 | |
---|
| 110 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 111 | warn $@->error; |
---|
| 112 | if ($@->twitter_error) { |
---|
| 113 | my %twitter_error = %{$@->twitter_error}; |
---|
| 114 | map { |
---|
| 115 | $twitter_error{"$_ => "} = $twitter_error{$_} . "\n"; |
---|
| 116 | delete $twitter_error{$_} |
---|
| 117 | } keys %twitter_error; |
---|
| 118 | warn join("", %twitter_error); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | else { |
---|
| 122 | warn $@; |
---|
| 123 | } |
---|
| 124 | } |
---|