* add search command
[lab.git] / Dev / twitter / twitterbot.pl
index 870873c..ae37ea6 100755 (executable)
@@ -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,31 +50,66 @@ 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";
-        next;
+        $skip = 'retweet';
     }
     if (defined $conf->{allow}) {
-        my $flag;
+        $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";
-                $flag = $id;
+                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";
-                $flag = $id;
+                undef $skip;
                 last;
             }
         }
-        if (! $flag) {
-            next;
+    }
+    
+    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/<screen_name>/status/<status_id> 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) {
+        $stat->{$tweets->{$id}{type}} = $id;
+        next;
+    }
+    
     DEBUG or sleep($conf->{sleep});
     
     # do retweet found tweets
@@ -90,7 +127,23 @@ foreach my $id (sort keys %$tweets) {
     $stat->{$tweets->{$id}{type}} = $id;
 }
 
-if ($tweets) {
+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 (ref $tweets and keys %{$tweets}) {
     # save last status to yaml file
     DEBUG  or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
     DEBUG and warn "status.yml => ", Dumper($stat);