[b4d59be] | 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 | |
---|
[f1b126a] | 18 | if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) { |
---|
| 19 | die <<EOM; |
---|
| 20 | usage: $0 |
---|
| 21 | [user_timeline(default)|retweeted_by_me|mentions |
---|
| 22 | [screen_name |
---|
| 23 | [number_of_pages|all |
---|
| 24 | [dump] |
---|
| 25 | ] |
---|
| 26 | ] |
---|
| 27 | ] |
---|
| 28 | EOM |
---|
[b4d59be] | 29 | } |
---|
[f1b126a] | 30 | my $method = $ARGV[0] || 'user_timeline'; |
---|
| 31 | my $screen_name = $ARGV[1] || ''; |
---|
| 32 | my $pages = $ARGV[2] || 1; |
---|
[b4d59be] | 33 | if ($pages eq 'all') { |
---|
| 34 | $pages = -1; |
---|
| 35 | } |
---|
[f1b126a] | 36 | my $dump = $ARGV[3] || 0; |
---|
[b4d59be] | 37 | |
---|
| 38 | my $conf = loadconf("$Bin/config.yml"); |
---|
| 39 | if (! defined $conf) { |
---|
| 40 | die "$0: cannot parse config file.\n"; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | my $bot = login($conf); |
---|
| 44 | if (! $bot->authorized) { |
---|
| 45 | die "$0: this client is not yet authorized.\n"; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | eval { |
---|
| 50 | my $page = 0; |
---|
| 51 | while ($pages - $page && $page <= 160) { |
---|
| 52 | $page++; |
---|
[f1b126a] | 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 | } |
---|
[b4d59be] | 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 | }; |
---|
| 92 | if ($@) { |
---|
| 93 | evalrescue($@); |
---|
| 94 | } |
---|
| 95 | print "done\n"; |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | sub 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 | |
---|
| 114 | sub 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 | |
---|
| 132 | sub 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 | } |
---|