/[Grep]/lib/Grep/Source/PhpWiki.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/PhpWiki.pm

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

revision 73 by dpavlin, Fri Feb 23 11:48:39 2007 UTC revision 85 by dpavlin, Fri Feb 23 20:47:08 2007 UTC
# Line 7  package Grep::Source::PhpWiki; Line 7  package Grep::Source::PhpWiki;
7    
8  =head1 NAME  =head1 NAME
9    
10  Grep::Source::  Grep::Source::PhpWiki - scraiper for PhpWiki search results
   
 use HTML::TreeBuilder;  
 use WWW::Mechanize;  
 use XML::Feed;  
   
 =head2 content_have  
   
 Return regex to match against content  
11    
12  =cut  =cut
13    
# Line 23  sub content_have { Line 15  sub content_have {
15          qr(generator.*PhpWiki);          qr(generator.*PhpWiki);
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 );  
   
         warn "submit $self form on $uri\n";  
   
         $mech->submit_form(  
                 form_number => 3,  
                 fields => {  
                         s => $q,  
                 },  
 #               button => 'FullTextSearch',  
         ) or die "can't submit $self form";  
   
         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('id') || '' ) eq 'FullTextSearchPlugin'  
         });  
   
         die "can't find div with id FullTextSearchPlugin" unless ( $div );  
   
         my $max = 5;  
         my $nr = 1;  
   
         foreach my $dt ( $div->look_down( '_tag', 'dt' ) ) {  
                 my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } );  
                 if ( $a ) {  
   
                         my $page_uri = $uri . $a->attr('href');  
   
                         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('class') || '' ) eq 'wikitext' } );  
   
                                 die "can't find <div class=wikitext>" 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++;  
         }  
21    
22          $tree->delete; # clear memory!          $parent->scrape(
23    #               submit_form => {
24    #                       fields => {
25    #                               value => $parent->q,
26    #                       },
27    #                       button => 'fullsearch',
28    #               },
29                    wrapper => [ qw/div id FullTextSearchPlugin/ ],
30                    results => 'dt',
31                    scrape => [ qw/div class wikitext/ ],
32            );
33    
34  }  }
35    

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

  ViewVC Help
Powered by ViewVC 1.1.26