source: lab.git/Dev/twitter/dump_timeline.pl @ 7848b0b

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

enable SSL option for Net::Twitter::Lite::WithAPIv1_1

  • SSL is required
  • Property mode set to 100755
File size: 4.6 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
21    [{u}ser_timeline(default)|{m}sentions|{s}earch
22        [screen_name
23            [number_of_pages|all
24                [dump]
25            ]
26        ]
27    ]
28EOM
29};
30if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
31    &{$help};
32}
33
34my $method = $ARGV[0] || 'user_timeline';
35my $screen_name = $ARGV[1] || '';
36my $pages = $ARGV[2] || 1;
37if ($pages eq 'all') {
38    $pages = -1;
39}
40my $dump = $ARGV[3] || 0;
41
42my $conf = loadconf("$Bin/config.yml");
43if (! defined $conf) {
44    die "$0: cannot parse config file.";
45}
46
47my $bot = login($conf);
48if (! $bot->authorized) {
49    die "$0: this client is not yet authorized.";
50}
51
52
53eval {
54    my $page = 0;
55    while ($pages - $page && $page <= 20) {
56        $page++;
57       
58        my $param = ($screen_name)
59            ? { page => $page, screen_name => $screen_name, count => 200, }
60            : { page => $page, count => 200, }
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 'mentions' || $method eq 'm') {
68            $res = $bot->mentions($param);
69        }
70        elsif ($method eq 'search' || $method eq 's') {
71            my $key;
72            foreach my $word (@{ $conf->{hashtag} }) {
73                if ($key) {
74                    $key .= " OR $word";
75                }
76                else {
77                    $key = $word;
78                }
79            }
80            $param->{q} = $key;
81            $res = $bot->search($param)->{results};
82        }
83        else {
84            warn "$0: unknown method '$method'";
85            &{$help};
86        }
87       
88        if ($dump) {
89            foreach my $line (split /\n/, Dumper $res) {
90                if ($line =~ /undef/) { next; }
91                print $line, "\n";
92            }
93        }
94        else {
95            foreach my $status (@{$res}) {
96                my $text = "";
97                $text .= "(". $status->{id} . ") ";
98                $text .= ($status->{user}{screen_name}) ?
99                    $status->{user}{screen_name} : $status->{from_user};
100                $text .= "|";
101                $text .= ($status->{user}{name}) ?
102                    $status->{user}{name} : $status->{from_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};
111if ($@) {
112    evalrescue($@);
113}
114
115
116sub 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
132sub login {
133    # make Net::Twitter::Lite::WithAPIv1_1 object and login
134    #   param   => hash object of configration
135    #   ret     => Net::Twitter::Lite::WithAPIv1_1 object
136   
137    my $conf = shift @_;
138   
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,
143        ssl             => 1,
144    );
145   
146    $bot->access_token($conf->{access_token});
147    $bot->access_token_secret($conf->{access_token_secret});
148   
149    return $bot;
150}
151
152sub evalrescue {
153    # output error message at eval error
154   
155    use Scalar::Util qw(blessed);
156   
157    if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
158        warn $@->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";
165                }
166            }
167            else {
168                # unknown HASH structure
169                use Data::Dumper;
170                warn Dumper $twitter_error;
171            }
172        }
173    }
174    else {
175        warn $@;
176    }
177}
Note: See TracBrowser for help on using the repository browser.