* add search command
[lab.git] / Dev / twitter / dump_timeline.pl
index 36fd046..0564d5d 100755 (executable)
@@ -15,24 +15,38 @@ use YAML::Tiny;
 use Data::Dumper;
 use Encode;
 
-if (@ARGV < 1) {
-    die "usage: $0 screen_name [number_of_pages|all [dump]]\n";
+my $help = sub {
+    die <<EOM;
+usage: $0
+    [{u}ser_timeline(default)|{r}etweeted_by_me|{m}sentions|{s}earch
+        [screen_name
+            [number_of_pages|all
+                [dump]
+            ]
+        ]
+    ]
+EOM
+};
+if ($ARGV[0] && ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') ) {
+    &{$help};
 }
-my $screen_name = $ARGV[0];
-my $pages = $ARGV[1] || 1;
+
+my $method = $ARGV[0] || 'user_timeline';
+my $screen_name = $ARGV[1] || '';
+my $pages = $ARGV[2] || 1;
 if ($pages eq 'all') {
     $pages = -1;
 }
-my $dump = $ARGV[2] || 0;
+my $dump = $ARGV[3] || 0;
 
 my $conf = loadconf("$Bin/config.yml");
 if (! defined $conf) {
-    die "$0: cannot parse config file.\n";
+    die "$0: cannot parse config file.";
 }
 
 my $bot = login($conf);
 if (! $bot->authorized) {
-    die "$0: this client is not yet authorized.\n";
+    die "$0: this client is not yet authorized.";
 }
 
 
@@ -40,12 +54,39 @@ eval {
     my $page = 0;
     while ($pages - $page && $page <= 160) {
         $page++;
-        my $res = $bot->user_timeline(
-            {
-                screen_name => $screen_name,
-                page        => $page,
+        
+        my $param = ($screen_name)
+            ? { page => $page, screen_name => $screen_name, }
+            : { page => $page, }
+        ;
+    
+        my $res;
+        if ($method eq 'user_timeline' || $method eq 'u') {
+            $res = $bot->user_timeline($param);
+        }
+        elsif ($method eq 'retweeted_by_me' || $method eq 'r') {
+            $res = $bot->retweeted_by_me($param);
+        }
+        elsif ($method eq 'mentions' || $method eq 'm') {
+            $res = $bot->mentions($param);
+        }
+        elsif ($method eq 'search' || $method eq 's') {
+            my $key;
+            foreach my $word (@{ $conf->{hashtag} }) {
+                if ($key) {
+                    $key .= " OR $word";
+                }
+                else {
+                    $key = $word;
+                }
             }
-        );
+            $param->{q} = $key;
+            $res = $bot->search($param)->{results};
+        }
+        else {
+            warn "$0: unknown method '$method'";
+            &{$help};
+        }
         
         if ($dump) {
             foreach my $line (split /\n/, Dumper $res) {
@@ -56,12 +97,13 @@ eval {
         else {
             foreach my $status (@{$res}) {
                 my $text = "";
+                $text .= "(". $status->{id} . ") ";
+                $text .= $status->{user}{screen_name} . "|";
                 $text .= $status->{user}{name};
                 $text .= " [" . $status->{created_at} . "]";
-                $text .= " (". $status->{id} . ")";
-                $text .= " ". encode('utf8', $status->{text});
+                $text .= " ".  $status->{text};
                 $text =~ s/\n//;
-                print $text, "\n";
+                print encode('utf8', $text), "\n";
             }
         }
     }
@@ -69,7 +111,6 @@ eval {
 if ($@) {
     evalrescue($@);
 }
-print "done\n";
 
 
 sub loadconf {
@@ -82,7 +123,7 @@ sub loadconf {
     my $yaml = YAML::Tiny->read($file);
     
     if ($!) {
-        warn "$0: '$file' $!\n";
+        warn "$0: '$file' $!";
     }
     
     return $yaml->[0];