From: mitty Date: Mon, 10 Jan 2011 09:57:45 +0000 (+0000) Subject: * NEW send a mail X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=45a20c35b7f63730b2bd6d7253d7b9aa07df68c2;p=lab.git * NEW send a mail * tweets to me and search results git-svn-id: https://lab.mitty.jp/svn/lab/trunk@104 7d2118f6-f56c-43e7-95a2-4bb3031d96e7 --- diff --git a/Dev/twitter/twitterbot.pl b/Dev/twitter/twitterbot.pl index 9b9e2d8..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' }; @@ -51,10 +53,10 @@ foreach my $id (sort keys %$tweets) { my $skip; if ($tweets->{$id}{type} eq 'retweet') { DEBUG and warn "skipping $id that was already retweeted\n"; - $skip = $id; + $skip = 'retweet'; } if (defined $conf->{allow}) { - $skip = $id; + $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"; @@ -71,6 +73,38 @@ foreach my $id (sort keys %$tweets) { } } + 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; } @@ -92,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);