Net::Twitter楽だよね。例外処理が微妙にダサイと感じるのはPerlそのものの問題なので置いといて、と。ひっかかったのは マルチバイト文字の正規表現を書くときは、Perlの内部フォーマットである UTF-8フラグが立った状態にしないといけない、という点くらいだろうか。
俺別にPerlが嫌いなわけじゃないんだね、と改めて再認識した。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Net::Twitter; | |
use Encode; | |
binmode(STDOUT, ":utf8"); | |
my $ng_word_regex = decode("utf-8", "XXXXX"); | |
my $sleep_time = 60; | |
my $nt = Net::Twitter->new( | |
traits => [qw/API::REST/], | |
username => 'username_or_mail_address', | |
password => 'user_password' | |
); | |
foreach my $i (1 .. 255) { | |
eval { | |
my $result = $nt->user_timeline({ page => $i }); | |
print "digging my timeline page $i ....\n\n"; | |
sleep($sleep_time); | |
foreach my $tweet (@$result) { | |
if ($tweet->{text} =~ /$ng_word_regex/) { | |
print "\n"; | |
print "NG Word Detected! Do you want to delete this tweet? \n"; | |
print "detected tweet is ... -> " . $tweet->{text} . " (y/n):"; | |
if (<STDIN> =~ /y/i) { | |
eval { | |
my $status = $nt->destroy_status($tweet->{id}); | |
}; | |
if (my $err = $@) { | |
print "error occured deleting tweet ... " . $err->error . "\n"; | |
} else { | |
print "deleted statues " . $tweet->{id} . "\n\n"; | |
sleep($sleep_time); | |
} | |
} else { | |
print "inputted 'N' ... ignoring. \n\n"; | |
} | |
} | |
} | |
}; | |
if (my $err = $@) { | |
print "error occured when getting user timeline ... " . $err->error . "\n"; | |
} | |
} |
[ Update January 2nd 20:36:55 JST by m ]
例外処理がちょっとダサイ点については Try::Tiny がちょっとカッコいい!(`ー´)
tokuhirom さめ情報ありがちょーヽ(´ー`。)ノ
use Try::Tiny;
try {
die "error occured!";
} catch {
warn "caught error: $_";
}
2 件のコメント:
例外処理がダサいのは、
use Try::Tiny;
try {
...
} catch {
print "error ocurred: $_";
};
とかすると、ちょっとましになります :)
http://search.cpan.org/~nuffin/Try-Tiny/
いいですね。少なくともif文よりはマシす(`ー´)
tokuhiromさめにコメントしてもらえるとか感激すぐるっす。ありがとうですヽ(´ー`。)ノ
コメントを投稿