[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 |
---|
[52c76fd] | 21 | [{u}ser_timeline(default)|{r}etweeted_by_me|{m}sentions|{s}earch |
---|
[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; |
---|
[c1b8ad4] | 55 | while ($pages - $page && $page <= 20) { |
---|
[b4d59be] | 56 | $page++; |
---|
[f1b126a] | 57 | |
---|
| 58 | my $param = ($screen_name) |
---|
[c1b8ad4] | 59 | ? { page => $page, screen_name => $screen_name, count => 200, } |
---|
| 60 | : { page => $page, count => 200, } |
---|
[f1b126a] | 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 | } |
---|
[52c76fd] | 73 | elsif ($method eq 'search' || $method eq 's') { |
---|
| 74 | my $key; |
---|
| 75 | foreach my $word (@{ $conf->{hashtag} }) { |
---|
| 76 | if ($key) { |
---|
| 77 | $key .= " OR $word"; |
---|
| 78 | } |
---|
| 79 | else { |
---|
| 80 | $key = $word; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | $param->{q} = $key; |
---|
| 84 | $res = $bot->search($param)->{results}; |
---|
| 85 | } |
---|
[f1b126a] | 86 | else { |
---|
[fe75883] | 87 | warn "$0: unknown method '$method'"; |
---|
| 88 | &{$help}; |
---|
[f1b126a] | 89 | } |
---|
[b4d59be] | 90 | |
---|
| 91 | if ($dump) { |
---|
| 92 | foreach my $line (split /\n/, Dumper $res) { |
---|
| 93 | if ($line =~ /undef/) { next; } |
---|
| 94 | print $line, "\n"; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | else { |
---|
| 98 | foreach my $status (@{$res}) { |
---|
| 99 | my $text = ""; |
---|
[ee655ec] | 100 | $text .= "(". $status->{id} . ") "; |
---|
[6852f37] | 101 | $text .= ($status->{user}{screen_name}) ? |
---|
| 102 | $status->{user}{screen_name} : $status->{from_user}; |
---|
| 103 | $text .= "|"; |
---|
| 104 | $text .= ($status->{user}{name}) ? |
---|
| 105 | $status->{user}{name} : $status->{from_user_name}; |
---|
[b4d59be] | 106 | $text .= " [" . $status->{created_at} . "]"; |
---|
[294a3aa] | 107 | $text .= " ". $status->{text}; |
---|
[b4d59be] | 108 | $text =~ s/\n//; |
---|
[294a3aa] | 109 | print encode('utf8', $text), "\n"; |
---|
[b4d59be] | 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | }; |
---|
| 114 | if ($@) { |
---|
| 115 | evalrescue($@); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | sub loadconf { |
---|
| 120 | # load configration data from yaml formatted file |
---|
| 121 | # param => scalar string of filename |
---|
| 122 | # ret => hash object of yaml data |
---|
| 123 | |
---|
| 124 | my $file = shift @_; |
---|
| 125 | |
---|
| 126 | my $yaml = YAML::Tiny->read($file); |
---|
| 127 | |
---|
| 128 | if ($!) { |
---|
[fe75883] | 129 | warn "$0: '$file' $!"; |
---|
[b4d59be] | 130 | } |
---|
| 131 | |
---|
| 132 | return $yaml->[0]; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | sub login { |
---|
| 136 | # make Net::Twitter::Lite object and login |
---|
| 137 | # param => hash object of configration |
---|
| 138 | # ret => Net::Twitter::Lite object |
---|
| 139 | |
---|
| 140 | my $conf = shift @_; |
---|
| 141 | |
---|
| 142 | my $bot = Net::Twitter::Lite->new( |
---|
| 143 | consumer_key => $conf->{consumer_key}, |
---|
| 144 | consumer_secret => $conf->{consumer_secret}, |
---|
[43d739e] | 145 | legacy_lists_api => 0, |
---|
[b4d59be] | 146 | ); |
---|
| 147 | |
---|
| 148 | $bot->access_token($conf->{access_token}); |
---|
| 149 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 150 | |
---|
| 151 | return $bot; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | sub evalrescue { |
---|
| 155 | # output error message at eval error |
---|
| 156 | |
---|
| 157 | use Scalar::Util qw(blessed); |
---|
| 158 | |
---|
| 159 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 160 | warn $@->error; |
---|
| 161 | if ($@->twitter_error) { |
---|
| 162 | my %twitter_error = %{$@->twitter_error}; |
---|
| 163 | map { |
---|
| 164 | $twitter_error{"$_ => "} = $twitter_error{$_} . "\n"; |
---|
| 165 | delete $twitter_error{$_} |
---|
| 166 | } keys %twitter_error; |
---|
| 167 | warn join("", %twitter_error); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else { |
---|
| 171 | warn $@; |
---|
| 172 | } |
---|
| 173 | } |
---|