--- lib/Grep/Source.pm 2007/02/23 20:47:08 85 +++ lib/Grep/Source.pm 2007/02/23 21:16:44 86 @@ -194,25 +194,16 @@ die "no uri" unless ($uri); die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed'); - sub save_html { - my ( $file, $content ) = @_; - if ( -w '/tmp/grep' ) { - 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: $!"; - } - } - my $mech = WWW::Mechanize->new(); $mech->get( $uri ); - save_html( 'get', $mech->content ); + $self->save( 'get.html', $mech->content ); if ( $args->{submit_form} ) { warn "submit form on $uri\n"; $mech->submit_form( %{ $args->{submit_form} } ) or die "can't submit form"; - save_html( 'submit', $mech->content ); + $self->save( 'submit.html', $mech->content ); } warn "parse result page\n"; @@ -250,7 +241,7 @@ warn "fetching page: ",$a->as_text," from $page_uri\n"; if ( $mech->follow_link( url => $a->attr('href') ) ) { - save_html( "page-${nr}", $mech->content ); + $self->save( "page-${nr}.html", $mech->content ); 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"; @@ -288,4 +279,23 @@ } +=head2 save + + save( 'name', $content ); + +Save dumps into C if writable + +=cut + +sub save { + my $self = shift; + my ( $file, $content ) = @_; + if ( -w '/tmp/grep' ) { + open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; + print $f $content or die "can't write to $file: $!"; + close $f or die "can't close $file: $!"; + Jifty->log->debug("saved $file ",length($content)," bytes"); + } +} + 1;