source: lab.git/Dev/twitter/update_timeline.pl @ e960958

Last change on this file since e960958 was e960958, checked in by Ken-ichi Mito <mitty@…>, 11 years ago

use Net::Twitter::Lite::WithAPIv1_1 instead of Net::Twitter::Lite

  • s/Net::Twitter::Lite/Net::Twitter::Lite::WithAPIv1_1/g;
  • Property mode set to 100755
File size: 3.2 KB
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use 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
12use Net::Twitter::Lite::WithAPIv1_1;
13use FindBin qw($Bin);
14use YAML::Tiny;
15use Data::Dumper;
16use Encode;
17
18my $help = sub {
19    die <<EOM;
20usage: $0 [text_file_to_tweet]
21EOM
22};
23if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
24    &{$help};
25}
26
27my $file = $ARGV[0] || 'update.log';
28my $conf = loadconf("$Bin/config.yml");
29if (! defined $conf) {
30    die "$0: cannot parse config file.";
31}
32
33my $bot = login($conf);
34if (! $bot->authorized) {
35    die "$0: this client is not yet authorized.";
36}
37
38open TWEET, $file or die "$0: cannot open file: $file";
39eval {
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};
66if ($@) {
67    evalrescue($@);
68}
69
70
71sub 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
87sub 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    );
99   
100    $bot->access_token($conf->{access_token});
101    $bot->access_token_secret($conf->{access_token_secret});
102   
103    return $bot;
104}
105
106sub 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) {
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            }
126        }
127    }
128    else {
129        warn $@;
130    }
131}
Note: See TracBrowser for help on using the repository browser.