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;
20 usage: $0 [text_file_to_tweet]
23 if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
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.";
33 my $bot = login($conf);
34 if (! $bot->authorized) {
35 die "$0: this client is not yet authorized.";
38 open TWEET, $file or die "$0: cannot open file: $file";
42 foreach my $line (<TWEET>) {
47 $update = decode('utf8', $update);
48 $res = $bot->update($update);
51 foreach my $line (split /\n/, Dumper $res) {
52 if ($line =~ /undef/) { next; }
53 print STDERR $line, "\n";
58 $text .= "(". $status->{id} . ") ";
59 $text .= ($status->{user}{name}) ? $status->{user}{name} : $status->{from_user};
60 $text .= " [" . $status->{created_at} . "]";
61 $text .= " ". $status->{text};
63 print encode('sjis', $text), "\n";
72 # load configration data from yaml formatted file
73 # param => scalar string of filename
74 # ret => hash object of yaml data
78 my $yaml = YAML::Tiny->read($file);
81 warn "$0: '$file' $!";
88 # make Net::Twitter::Lite::WithAPIv1_1 object and login
89 # param => hash object of configration
90 # ret => Net::Twitter::Lite::WithAPIv1_1 object
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,
101 $bot->access_token($conf->{access_token});
102 $bot->access_token_secret($conf->{access_token_secret});
108 # output error message at eval error
110 use Scalar::Util qw(blessed);
112 if (blessed $@ && $@->isa('Net::Twitter::Lite::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";
123 # unknown HASH structure
125 warn Dumper $twitter_error;