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;
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 object and login
89 # param => hash object of configration
90 # ret => Net::Twitter::Lite object
94 my $bot = Net::Twitter::Lite->new(
95 consumer_key => $conf->{consumer_key},
96 consumer_secret => $conf->{consumer_secret},
99 $bot->access_token($conf->{access_token});
100 $bot->access_token_secret($conf->{access_token_secret});
106 # output error message at eval error
108 use Scalar::Util qw(blessed);
110 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
112 if ($@->twitter_error) {
113 my %twitter_error = %{$@->twitter_error};
115 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
116 delete $twitter_error{$_}
117 } keys %twitter_error;
118 warn join("", %twitter_error);