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