* add search command
[lab.git] / Dev / twitter / dump_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;
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
21     [{u}ser_timeline(default)|{r}etweeted_by_me|{m}sentions|{s}earch
22         [screen_name
23             [number_of_pages|all
24                 [dump]
25             ]
26         ]
27     ]
28 EOM
29 };
30 if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
31     &{$help};
32 }
33
34 my $method = $ARGV[0] || 'user_timeline';
35 my $screen_name = $ARGV[1] || '';
36 my $pages = $ARGV[2] || 1;
37 if ($pages eq 'all') {
38     $pages = -1;
39 }
40 my $dump = $ARGV[3] || 0;
41
42 my $conf = loadconf("$Bin/config.yml");
43 if (! defined $conf) {
44     die "$0: cannot parse config file.";
45 }
46
47 my $bot = login($conf);
48 if (! $bot->authorized) {
49     die "$0: this client is not yet authorized.";
50 }
51
52
53 eval {
54     my $page = 0;
55     while ($pages - $page && $page <= 160) {
56         $page++;
57         
58         my $param = ($screen_name)
59             ? { page => $page, screen_name => $screen_name, }
60             : { page => $page, }
61         ;
62     
63         my $res;
64         if ($method eq 'user_timeline' || $method eq 'u') {
65             $res = $bot->user_timeline($param);
66         }
67         elsif ($method eq 'retweeted_by_me' || $method eq 'r') {
68             $res = $bot->retweeted_by_me($param);
69         }
70         elsif ($method eq 'mentions' || $method eq 'm') {
71             $res = $bot->mentions($param);
72         }
73         elsif ($method eq 'search' || $method eq 's') {
74             my $key;
75             foreach my $word (@{ $conf->{hashtag} }) {
76                 if ($key) {
77                     $key .= " OR $word";
78                 }
79                 else {
80                     $key = $word;
81                 }
82             }
83             $param->{q} = $key;
84             $res = $bot->search($param)->{results};
85         }
86         else {
87             warn "$0: unknown method '$method'";
88             &{$help};
89         }
90         
91         if ($dump) {
92             foreach my $line (split /\n/, Dumper $res) {
93                 if ($line =~ /undef/) { next; }
94                 print $line, "\n";
95             }
96         }
97         else {
98             foreach my $status (@{$res}) {
99                 my $text = "";
100                 $text .= "(". $status->{id} . ") ";
101                 $text .= $status->{user}{screen_name} . "|";
102                 $text .= $status->{user}{name};
103                 $text .= " [" . $status->{created_at} . "]";
104                 $text .= " ".  $status->{text};
105                 $text =~ s/\n//;
106                 print encode('utf8', $text), "\n";
107             }
108         }
109     }
110 };
111 if ($@) {
112     evalrescue($@);
113 }
114
115
116 sub loadconf {
117     # load configration data from yaml formatted file
118     #   param   => scalar string of filename
119     #   ret     => hash object of yaml data
120     
121     my $file = shift @_;
122     
123     my $yaml = YAML::Tiny->read($file);
124     
125     if ($!) {
126         warn "$0: '$file' $!";
127     }
128     
129     return $yaml->[0];
130 }
131
132 sub login {
133     # make Net::Twitter::Lite object and login
134     #   param   => hash object of configration
135     #   ret     => Net::Twitter::Lite object
136     
137     my $conf = shift @_;
138     
139     my $bot = Net::Twitter::Lite->new(
140         consumer_key    => $conf->{consumer_key},
141         consumer_secret => $conf->{consumer_secret},
142     );
143     
144     $bot->access_token($conf->{access_token});
145     $bot->access_token_secret($conf->{access_token_secret});
146     
147     return $bot;
148 }
149
150 sub evalrescue {
151     # output error message at eval error
152     
153     use Scalar::Util qw(blessed);
154     
155     if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
156         warn $@->error;
157         if ($@->twitter_error) {
158             my %twitter_error = %{$@->twitter_error};
159             map {
160                 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
161                 delete $twitter_error{$_}
162             } keys %twitter_error;
163             warn join("", %twitter_error);
164         }
165     }
166     else {
167         warn $@;
168     }
169 }