source: lab.git/Dev/twitter/dump_timeline.pl @ f1b126a

trunk
Last change on this file since f1b126a was f1b126a, checked in by mitty <mitty@…>, 13 years ago
  • dump_timeline.pl now parses 4 parameters
    usage: dump_timeline.pl
        [user_timeline(default)|retweeted_by_me|
            [screen_name
                [number_of_pages|all
                    [dump]
                ]
            ]
        ]
    
    • supported method => user_timeline, retweeted_by_me, mentions
    • default method is user_timeline

git-svn-id: https://lab.mitty.jp/svn/lab/trunk@97 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

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