source: lab/trunk/Dev/twitter/dump_timeline.pl @ 100

Last change on this file since 100 was 100, checked in by mitty, 13 years ago
  • change order of column
    (status_id) screen_name [created_at] text
    
    • created_at => UTC
  • Property svn:executable set to *
File size: 3.7 KB
RevLine 
[48]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;
13use FindBin qw($Bin);
14use YAML::Tiny;
[58]15use Data::Dumper;
16use Encode;
[48]17
[98]18my $help = sub {
[97]19    die <<EOM;
20usage: $0
21    [user_timeline(default)|retweeted_by_me|mentions
22        [screen_name
23            [number_of_pages|all
24                [dump]
25            ]
26        ]
27    ]
28EOM
[98]29};
30if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
31    &{$help};
[58]32}
[98]33
[97]34my $method = $ARGV[0] || 'user_timeline';
35my $screen_name = $ARGV[1] || '';
36my $pages = $ARGV[2] || 1;
[58]37if ($pages eq 'all') {
38    $pages = -1;
39}
[97]40my $dump = $ARGV[3] || 0;
[58]41
[48]42my $conf = loadconf("$Bin/config.yml");
43if (! defined $conf) {
[98]44    die "$0: cannot parse config file.";
[48]45}
46
47my $bot = login($conf);
48if (! $bot->authorized) {
[98]49    die "$0: this client is not yet authorized.";
[48]50}
51
52
[50]53eval {
[58]54    my $page = 0;
55    while ($pages - $page && $page <= 160) {
56        $page++;
57       
[97]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') {
65            $res = $bot->user_timeline($param);
66        }
67        elsif ($method eq 'retweeted_by_me') {
68            $res = $bot->retweeted_by_me($param);
69        }
70        elsif ($method eq 'mentions') {
71            $res = $bot->mentions($param);
72        }
73        else {
[98]74            warn "$0: unknown method '$method'";
75            &{$help};
[97]76        }
77       
[58]78        if ($dump) {
79            foreach my $line (split /\n/, Dumper $res) {
80                if ($line =~ /undef/) { next; }
81                print $line, "\n";
82            }
[51]83        }
[58]84        else {
85            foreach my $status (@{$res}) {
86                my $text = "";
[100]87                $text .= "(". $status->{id} . ") ";
[58]88                $text .= $status->{user}{name};
89                $text .= " [" . $status->{created_at} . "]";
[99]90                $text .= " ".  $status->{text};
[58]91                $text =~ s/\n//;
[99]92                print encode('utf8', $text), "\n";
[58]93            }
94        }
[48]95    }
[50]96};
97if ($@) {
98    evalrescue($@);
[48]99}
100
101
102sub loadconf {
103    # load configration data from yaml formatted file
104    #   param   => scalar string of filename
105    #   ret     => hash object of yaml data
106   
107    my $file = shift @_;
108   
109    my $yaml = YAML::Tiny->read($file);
110   
111    if ($!) {
[98]112        warn "$0: '$file' $!";
[48]113    }
114   
115    return $yaml->[0];
116}
117
118sub login {
119    # make Net::Twitter::Lite object and login
120    #   param   => hash object of configration
121    #   ret     => Net::Twitter::Lite object
122   
123    my $conf = shift @_;
124   
125    my $bot = Net::Twitter::Lite->new(
126        consumer_key    => $conf->{consumer_key},
127        consumer_secret => $conf->{consumer_secret},
128    );
129   
130    $bot->access_token($conf->{access_token});
131    $bot->access_token_secret($conf->{access_token_secret});
132   
133    return $bot;
134}
135
136sub evalrescue {
137    # output error message at eval error
138   
139    use Scalar::Util qw(blessed);
140   
141    if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
142        warn $@->error;
143        if ($@->twitter_error) {
144            my %twitter_error = %{$@->twitter_error};
145            map {
146                $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
147                delete $twitter_error{$_}
148            } keys %twitter_error;
149            warn join("", %twitter_error);
150        }
151    }
152    else {
153        warn $@;
154    }
155}
Note: See TracBrowser for help on using the repository browser.