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

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

revision 85 by dpavlin, Fri Feb 23 20:47:08 2007 UTC revision 92 by dpavlin, Sat Feb 24 11:16:05 2007 UTC
# Line 194  sub scrape { Line 194  sub scrape {
194          die "no uri" unless ($uri);          die "no uri" unless ($uri);
195          die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');          die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');
196    
197          sub save_html {          sub mech_warn {
198                  my ( $file, $content ) = @_;                  my $m = shift || return;
199                  if ( -w '/tmp/grep' ) {                  warn $m;
                         open(my $f, '>', "/tmp/grep/${file}.html") or die "can't open $file: $!";  
                         print $f $content or die "can't write to $file: $!";  
                         close $f or die "can't close $file: $!";  
                 }  
200          }          }
201    
202          my $mech = WWW::Mechanize->new();          my $mech = WWW::Mechanize->new(
203                    cookie_jar => {},
204                    onwarn => \&mech_warn,
205                    onerror => \&mech_warn,
206            );
207    
208          $mech->get( $uri );          $mech->get( $uri );
209    
210          save_html( 'get', $mech->content );          $self->save( 'get.html', $mech->content );
211    
212          if ( $args->{submit_form} ) {          if ( my $form = $args->{submit_form} ) {
213                  warn "submit form on $uri\n";                  warn "submit form on $uri with ", dump( $form ),"\n";
214                  $mech->submit_form( %{ $args->{submit_form} } ) or die "can't submit form";                  $mech->submit_form( %$form ) or die "can't submit form ", dump( $form );
215                  save_html( 'submit', $mech->content );                  $self->save( 'submit.html', $mech->content );
216          }          }
217    
218          warn "parse result page\n";          warn "parse result page\n";
# Line 230  sub scrape { Line 230  sub scrape {
230                          ( $_[0]->attr( $attr ) || '' ) eq $value;                          ( $_[0]->attr( $attr ) || '' ) eq $value;
231          });          });
232    
233          die "can't find results wrapper <$el $attr=\"$value\">" unless ( $div );          if ( ! $div ) {
234                    warn "can't find results wrapper <$el $attr=\"$value\">";
235                    return;
236            }
237    
238          my $max = 5;          my $max = 5;
239          my $nr = 1;          my $nr = 1;
# Line 250  sub scrape { Line 253  sub scrape {
253                          warn "fetching page: ",$a->as_text," from $page_uri\n";                          warn "fetching page: ",$a->as_text," from $page_uri\n";
254                          if ( $mech->follow_link( url => $a->attr('href') ) ) {                          if ( $mech->follow_link( url => $a->attr('href') ) ) {
255    
256                                  save_html( "page-${nr}", $mech->content );                                  $self->save( "page-${nr}.html", $mech->content );
257    
258                                  my $page_tree = HTML::TreeBuilder->new or die "can't create page tree";                                  my $page_tree = HTML::TreeBuilder->new or die "can't create page tree";
259                                  $page_tree->parse( $mech->content ) or die "can't parse page at $page_uri";                                  $page_tree->parse( $mech->content ) or die "can't parse page at $page_uri";
260    
261                                  my ( $el,$attr,$value ) = @{ $args->{scrape} };                                  ( $el,$attr,$value ) = @{ $args->{scrape} };
262                                  my $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } );                                  $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } );
263    
264                                  die "can't find <$el $attr=\"$value\">" unless ($div);                                  die "can't find <$el $attr=\"$value\">" unless ($div);
265    
# Line 288  sub scrape { Line 291  sub scrape {
291    
292  }  }
293    
294    =head2 save
295    
296      save( 'name', $content );
297    
298    Save dumps into C</tmp/grep> if writable
299    
300    =cut
301    
302    sub save {
303            my $self = shift;
304            my ( $file, $content ) = @_;
305            if ( -w '/tmp/grep' ) {
306                    open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!";
307                    print $f $content or die "can't write to $file: $!";
308                    close $f or die "can't close $file: $!";
309                    Jifty->log->debug("saved $file ",length($content)," bytes");
310            }
311    }
312    
313  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26