X-Git-Url: http://lab.mitty.jp/git/?a=blobdiff_plain;f=Dev%2Ftwitter%2Ftwitterbot.pl;h=f9dea40084f8a0d6617f48e2d8efd1f9dd75db84;hb=45a20c35b7f63730b2bd6d7253d7b9aa07df68c2;hp=406ee128f4ca5a883417a86dc12db8896903dafe;hpb=8333ea00a9fe608c90c20af12ea0c51548f66f4e;p=lab.git diff --git a/Dev/twitter/twitterbot.pl b/Dev/twitter/twitterbot.pl index 406ee12..f9dea40 100755 --- a/Dev/twitter/twitterbot.pl +++ b/Dev/twitter/twitterbot.pl @@ -13,6 +13,8 @@ use Net::Twitter::Lite; use FindBin qw($Bin); use YAML::Tiny; use Date::Parse qw(str2time); +use Mail::Sendmail; +use Encode; my $_execmode = $ARGV[0] || 0; sub VERBOSE () { $_execmode eq 'verbose' }; @@ -48,9 +50,65 @@ my $tweets = {}; foreach my $id (sort keys %$tweets) { # $tweets->{$id}{type} eq 'search' => found by search API # eq 'mention' => found by mention API + my $skip; if ($tweets->{$id}{type} eq 'retweet') { + DEBUG and warn "skipping $id that was already retweeted\n"; + $skip = 'retweet'; + } + if (defined $conf->{allow}) { + $skip = 'allow'; + foreach my $screen_name ( @{ $conf->{allow}{screen_name} } ) { + if ($tweets->{$id}{screen_name} eq $screen_name) { + DEBUG and warn "$id was allowed by screen_name\n"; + undef $skip; + last; + } + } + foreach my $user_id ( @{ $conf->{allow}{user_id} } ) { + if ($tweets->{$id}{user_id} eq $user_id) { + DEBUG and warn "$id was allowed by user_id\n"; + undef $skip; + last; + } + } + } + + if (defined $conf->{mail}) { + my $send; + foreach my $pickup ( @{ $conf->{mail}{pickup} } ) { + if ($tweets->{$id}{type} eq $pickup) { + $send = 1; + last; + } + } + if ($conf->{mail}{ignore_allowed}) { + if (defined $conf->{allow} and ! defined $skip) { + # this tweet was allowed to retweet, so that be ignored on mail + undef $send; + } + } + + if ($send) { + if (! exists($conf->{mail}{body}) ) { + $conf->{mail}{body} = + "[localtime(date)] http://twitter.com//status/ text\n" . + "----------------------------------------\n" + ; + } + $conf->{mail}{body} .= + "[" . localtime($tweets->{$id}{date}) . "] " . + "http://twitter.com/" . + $tweets->{$id}{screen_name} . "/status/" . + $tweets->{$id}{status_id} . " " . + $tweets->{$id}{text} . "\n" + ; + } + } + + if ($skip) { next; } + DEBUG or sleep($conf->{sleep}); # do retweet found tweets @@ -68,6 +126,22 @@ foreach my $id (sort keys %$tweets) { $stat->{$tweets->{$id}{type}} = $id; } +if ($conf->{mail}{body}) { + my $body = encode("iso-2022-jp", $conf->{mail}{body}); + + my %mail = ( + Smtp => $conf->{mail}{server}, + From => $conf->{mail}{from}, + To => join(", ", @{ $conf->{mail}{to} }), + Subject => $conf->{mail}{subject}, + "Content-Type" => $conf->{mail}{contenttype}, + Body => $body, + ); + DEBUG and warn "sending mail => ", Dumper(\%mail); + + DEBUG or sendmail(%mail) or warn "Error sending mail: $Mail::Sendmail::error\n"; +} + if ($tweets) { # save last status to yaml file DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat); @@ -88,6 +162,8 @@ sub loadconf { warn "$0: '$file' $!\n"; } + DEBUG and warn "'$file' => ", Dumper($yaml); + return $yaml->[0]; }