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;
21 [{u}ser_timeline(default)|{m}sentions|{s}earch
30 if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
34 my $method = $ARGV[0] || 'user_timeline';
35 my $screen_name = $ARGV[1] || '';
36 my $pages = $ARGV[2] || 1;
37 if ($pages eq 'all') {
40 my $dump = $ARGV[3] || 0;
42 my $conf = loadconf("$Bin/config.yml");
43 if (! defined $conf) {
44 die "$0: cannot parse config file.";
47 my $bot = login($conf);
48 if (! $bot->authorized) {
49 die "$0: this client is not yet authorized.";
55 while ($pages - $page && $page <= 20) {
58 my $param = ($screen_name)
59 ? { page => $page, screen_name => $screen_name, count => 200, }
60 : { page => $page, count => 200, }
64 if ($method eq 'user_timeline' || $method eq 'u') {
65 $res = $bot->user_timeline($param);
67 elsif ($method eq 'mentions' || $method eq 'm') {
68 $res = $bot->mentions($param);
70 elsif ($method eq 'search' || $method eq 's') {
72 foreach my $word (@{ $conf->{hashtag} }) {
81 $res = $bot->search($param)->{results};
84 warn "$0: unknown method '$method'";
89 foreach my $line (split /\n/, Dumper $res) {
90 if ($line =~ /undef/) { next; }
95 foreach my $status (@{$res}) {
97 $text .= "(". $status->{id} . ") ";
98 $text .= ($status->{user}{screen_name}) ?
99 $status->{user}{screen_name} : $status->{from_user};
101 $text .= ($status->{user}{name}) ?
102 $status->{user}{name} : $status->{from_user_name};
103 $text .= " [" . $status->{created_at} . "]";
104 $text .= " ". $status->{text};
106 print encode('utf8', $text), "\n";
117 # load configration data from yaml formatted file
118 # param => scalar string of filename
119 # ret => hash object of yaml data
123 my $yaml = YAML::Tiny->read($file);
126 warn "$0: '$file' $!";
133 # make Net::Twitter::Lite::WithAPIv1_1 object and login
134 # param => hash object of configration
135 # ret => Net::Twitter::Lite::WithAPIv1_1 object
139 my $bot = Net::Twitter::Lite::WithAPIv1_1->new(
140 consumer_key => $conf->{consumer_key},
141 consumer_secret => $conf->{consumer_secret},
142 legacy_lists_api => 0,
146 $bot->access_token($conf->{access_token});
147 $bot->access_token_secret($conf->{access_token_secret});
153 # output error message at eval error
155 use Scalar::Util qw(blessed);
157 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
159 if ($@->twitter_error) {
160 my $twitter_error = $@->twitter_error;
161 if (defined $twitter_error->{errors}) {
162 foreach my $error (@{$twitter_error->{errors}}) {
163 warn "code => " , $error->{code}, "\n";
164 warn "message => ", $error->{message}, "\n";
168 # unknown HASH structure
170 warn Dumper $twitter_error;