/[Grep]/lib/Grep/Source/MoinMoin.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Grep/Source/MoinMoin.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 84 by dpavlin, Fri Feb 23 18:38:48 2007 UTC revision 85 by dpavlin, Fri Feb 23 20:47:08 2007 UTC
# Line 11  Grep::Source::MoinMoin - scraper for Moi Line 11  Grep::Source::MoinMoin - scraper for Moi
11    
12  =cut  =cut
13    
 use HTML::TreeBuilder;  
 use WWW::Mechanize;  
 use XML::Feed;  
   
 =head2 content_have  
   
 Return regex to match against content  
   
 =cut  
   
14  sub content_have {  sub content_have {
15          qr/MoinMoin/          qr/MoinMoin/
16  }  }
17    
 =head2 fetch  
   
   $plugin->fetch( $source );  
   
 =cut  
   
18  sub fetch {  sub fetch {
19          my $self = shift;          my $self = shift;
   
20          my $parent = shift;          my $parent = shift;
         my ($feed,$uri,$q) = ($parent->feed, $parent->uri,$parent->q);  
         die "no uri" unless ($uri);  
         die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');  
   
         my $mech = WWW::Mechanize->new();  
   
         $mech->get( $uri );  
21    
22          warn "submit $self form on $uri\n";          $parent->scrape(
23                    submit_form => {
24          $mech->submit_form(                          fields => {
25                  fields => {                                  value => $parent->q,
26                          value => $q,                          },
27                            button => 'fullsearch',
28                  },                  },
29                  button => 'fullsearch',                  wrapper => [ qw/div class searchresults/ ],
30          ) or die "can't $self submit";                  results => 'dt',
31                    scrape => [ qw/div id page/ ],
32          warn "parse result page\n";          );
   
         my $tree = HTML::TreeBuilder->new or die "can't create html tree";  
         $tree->parse( $mech->content ) or die "can't parse fetched content";  
   
         my $div = $tree->look_down( '_tag', 'div', sub {  
                         ( $_[0]->attr('class') || '' ) eq 'searchresults'  
         });  
   
         die "can't find div with class searchresults" unless ( $div );  
   
         my $max = 5;  
         my $nr = 1;  
   
         my $base_uri = $uri;  
         $base_uri =~ s!\?.*$!!;  
   
         foreach my $dt ( $div->look_down( '_tag', 'dt' ) ) {  
                 my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } );  
                 if ( $a ) {  
   
                         my $page_uri = $base_uri . $a->attr('href');  
                         $page_uri =~ s!\Q?highlight=\E.*$!!;  
   
                         warn "fetching page: ",$a->as_text," from $page_uri\n";  
                         if ( $mech->follow_link( url => $a->attr('href') ) ) {  
   
                                 my $page_tree = HTML::TreeBuilder->new or die "can't create page tree";  
                                 $page_tree->parse( $mech->content ) or die "can't parse page at $page_uri";  
   
                                 my $div = $page_tree->look_down( '_tag', 'div', sub { ( $_[0]->attr('id') || '' ) eq 'page' } );  
   
                                 die "can't find <div id=page>" unless ($div);  
   
                                 $parent->add_record(  
                                         in_feed => $feed,  
                                         title => $mech->title,  
                                         link => $page_uri,  
                                         content => $div->as_HTML,  
 #                                       summary =>  
 #                                       category =>  
 #                                       author =>  
 #                                       issued =>  
 #                                       modified =>  
                                 );  
   
                                 $mech->back;  
   
                         } else {  
                                 warn "can't follow uri $page_uri: $!\n";  
                         }  
                 }  
   
                 last if ($nr == $max);  
                 $nr++;  
         }  
   
         $tree->delete; # clear memory!  
33    
34  }  }
35    

Legend:
Removed from v.84  
changed lines
  Added in v.85

  ViewVC Help
Powered by ViewVC 1.1.26