enable SSL option for Net::Twitter::Lite::WithAPIv1_1
[lab.git] / Dev / twitter / update_timeline.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 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::WithAPIv1_1 object and login
89     #   param   => hash object of configration
90     #   ret     => Net::Twitter::Lite::WithAPIv1_1 object
91     
92     my $conf = shift @_;
93     
94     my $bot = Net::Twitter::Lite::WithAPIv1_1->new(
95         consumer_key    => $conf->{consumer_key},
96         consumer_secret => $conf->{consumer_secret},
97         legacy_lists_api => 0,
98         ssl             => 1,
99     );
100     
101     $bot->access_token($conf->{access_token});
102     $bot->access_token_secret($conf->{access_token_secret});
103     
104     return $bot;
105 }
106
107 sub evalrescue {
108     # output error message at eval error
109     
110     use Scalar::Util qw(blessed);
111     
112     if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
113         warn $@->error;
114         if ($@->twitter_error) {
115             my $twitter_error = $@->twitter_error;
116             if (defined $twitter_error->{errors}) {
117                 foreach my $error (@{$twitter_error->{errors}}) {
118                     warn "code => "   , $error->{code}, "\n";
119                     warn "message => ", $error->{message}, "\n";
120                 }
121             }
122             else {
123                 # unknown HASH structure
124                 use Data::Dumper;
125                 warn Dumper $twitter_error;
126             }
127         }
128     }
129     else {
130         warn $@;
131     }
132 }