[Bps-public-commit] r17522 - in Net-Google-Code/trunk: lib/Net/Google/Code

sunnavy at bestpractical.com sunnavy at bestpractical.com
Fri Jan 2 02:34:11 EST 2009


Author: sunnavy
Date: Fri Jan  2 02:34:08 2009
New Revision: 17522

Modified:
   Net-Google-Code/trunk/   (props changed)
   Net-Google-Code/trunk/lib/Net/Google/Code/Search.pm

Log:
 r18480 at sunnavys-mb:  sunnavy | 2009-01-02 14:16:39 +0800
 parse list


Modified: Net-Google-Code/trunk/lib/Net/Google/Code/Search.pm
==============================================================================
--- Net-Google-Code/trunk/lib/Net/Google/Code/Search.pm	(original)
+++ Net-Google-Code/trunk/lib/Net/Google/Code/Search.pm	Fri Jan  2 02:34:08 2009
@@ -50,24 +50,64 @@
       . 'when search'
       unless $mech->response->is_success;
 
-    my $content = $mech->content;
+    my $content = $mech->response->content;
 
+            open my $fh, '>', '/tmp/t';
+            print $fh $content;
+            close $fh;
     if ( $mech->title =~ /Issue\s+(\d+)/ ) {
 # only get one ticket
         @{$self->ids} = $1;
     }
     elsif ( $mech->title =~ /Issues/ ) {
 # get a ticket list
-# XXX TODO parse the list
+        $self->ids([]); # clean previous ids
+
+        require HTML::TreeBuilder;
+        my $tree = HTML::TreeBuilder->new;
+        $tree->parse_content($content);
+        my $pagination = $tree->look_down( class => 'pagination' );
+        if ( my ( $start, $end, $total ) =
+            $pagination->content_array_ref->[0] =~
+            /(\d+)\s+-\s+(\d+)\s+of\s+(\d+)/ )
+        {
+
+            my @ids = $tree->look_down( class => 'vt id col_0' );
+            @ids =
+              map { $_->content_array_ref->[0]->content_array_ref->[0] } @ids;
+            push @{ $self->ids }, @ids;
+
+            while ( scalar @{$self->ids} < $total ) {
+                if ($mech->follow_link( text_regex => qr/Next\s+/ ) ) {
+                    if ( $mech->response->is_success ) {
+                        my $content = $mech->content;
+                        my $tree    = HTML::TreeBuilder->new;
+                        $tree->parse_content($content);
+                        my @ids = $tree->look_down( class => 'vt id col_0' );
+                        @ids =
+                          map {
+                            $_->content_array_ref->[0]->content_array_ref->[0]
+                          } @ids;
+                        push @{ $self->ids }, @ids;
+                    }
+                    else {
+                        die "failed to follow link: Next";
+                    }
+                }
+                else {
+                # XXX sometimes google's result number is wrong. google--
+                    warn "didn't find enough tickets, sometimes it's google's fault instead of ours ;)";
+                    last;
+                }
+            }
+        }
 
     }
     else {
         warn "no idea what the content like";
     }
-        
 }
 
-
 no Moose;
 
 1;



More information about the Bps-public-commit mailing list