--- lib/Grep/Source.pm 2007/02/24 12:32:31 100 +++ lib/Grep/Source.pm 2007/04/28 22:52:08 125 @@ -8,7 +8,7 @@ use Carp qw/verbose/; use Module::Pluggable search_path => 'Grep::Source', sub_name => 'sources', require => 1; use base qw(Class::Accessor Jifty::Object); -Grep::Source->mk_accessors( qw(feed uri q new_items collection) ); +Grep::Source->mk_accessors( qw(feed uri q new_items collection search_obj tree) ); use HTML::TreeBuilder; use WWW::Mechanize; @@ -108,11 +108,12 @@ my $class = $self->feed->source || 'Grep::Source::Feed'; $self->log->debug("using $class"); - my $parent = $self; - $class->fetch( $parent ); - undef $parent; + $self->search_obj( Grep::Search->new() ); + $self->log->debug("created " . $self->search_obj); - Grep::Search->finish if $self->new_items; + $class->fetch( $self ); + + $self->search_obj->finish; return $self->collection; } @@ -131,11 +132,13 @@ sub add_record { my $self = shift; + $self->log->confess("no search_obj") unless ($self->search_obj); + my $i = Grep::Model::Item->new(); my $rec = {@_}; - warn "resolving links"; + $self->log->debug("resolving links using base ", $rec->{link}); my $resolver = HTML::ResolveLink->new( base => $rec->{link} ); $rec->{content} = $resolver->resolve( $rec->{content} ); @@ -149,7 +152,7 @@ # is new record? if ( $msg !~ m/^Found/ ) { - Grep::Search->add( $i ); + $self->search_obj->add( $i ); $self->new_items( ( $self->new_items || 0 ) + 1 ); } } else { @@ -192,12 +195,65 @@ =cut +sub element_by_triplet { + my $self = shift; + + my $args = {@_}; + + my $tree = $args->{tree} || die "no tree"; + my $message = $args->{message} || ''; + my $fatal = $args->{fatal}; + die "no templates" unless defined( $args->{templates} ); + my @templates; +warn dump( $args->{templates} ); + if ( ref( $args->{templates} ) eq 'ARRAY' ) { + @templates = @{ $args->{templates} }; + } else { + @templates = ( $args->{templates} ); + } + + push @templates, ( undef, undef ) if ( $#templates == 0 ); + + die "wrapper doesn't have 3 elements but ", $#templates unless ( + ( $#templates + 1 ) % 3 == 0 + ); + + my ( $el, $attr, $value ); + + my @results; + my @tags; + + while ( @templates ) { + ( $el,$attr,$value ) = splice( @templates, 0, 3 ); + my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>"; + push @tags, $tag; + $self->log->debug("looking for $message $tag"); + @results = $tree->look_down( '_tag', $el, sub { + return 1 unless ( $attr && $value ); + ( $_[0]->attr( $attr ) || '' ) eq $value; + }); + last if @results; + } + + if ( ! @results ) { + my $msg = "can't find $message ", join(" ", @tags); + die $msg if ( $fatal ); + warn $msg; + return; + } + + $self->log->debug("found ", $#results + 1, " results"); + + return @results if wantarray; + return shift @results; +} + sub scrape { my $self = shift; my $args = {@_}; - warn "scrape got args ",dump($args); + $self->log->debug("scrape with args ",dump($args)); my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); die "no uri" unless ($uri); @@ -219,38 +275,36 @@ $self->save( 'get.html', $mech->content ); if ( my $form = $args->{submit_form} ) { - warn "submit form on $uri with ", dump( $form ),"\n"; + $self->log->debug("submit form on $uri with ", dump( $form )); $mech->submit_form( %$form ) or die "can't submit form ", dump( $form ); $self->save( 'submit.html', $mech->content ); } - warn "parse result page\n"; + $self->log->debug("parse result page"); my $tree = HTML::TreeBuilder->new or die "can't create html tree"; $tree->parse( $mech->content ) or die "can't parse fetched content"; - die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 ); - my ( $el,$attr,$value ) = @{ $args->{wrapper} }; - - warn "looking for <$el $attr=\"$value\">"; - - my $div = $tree->look_down( '_tag', $el, sub { - warn dump( $_[0]->attr( $attr ) ),$/; - ( $_[0]->attr( $attr ) || '' ) eq $value; - }); - - if ( ! $div ) { - warn "can't find results wrapper <$el $attr=\"$value\">"; - return; - } + my $div = $self->element_by_triplet( + tree => $tree, + templates => $args->{wrapper}, + message => 'wrapper for all results', + fatal => 1 + ); - my $max = 5; + my $max = 15; my $nr = 1; my $base_uri = $uri; $base_uri =~ s!\?.*$!!; - foreach my $dt ( $div->look_down( '_tag', $args->{results} ) ) { + my @r = $self->element_by_triplet( + tree => $tree, + templates => $args->{results}, + message => 'result element', + ); + + foreach my $dt ( @r ) { my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } ); if ( $a ) { @@ -259,18 +313,18 @@ $page_uri->query( undef ); $page_uri = $page_uri->canonical; - warn "fetching page: ",$a->as_text," from $page_uri\n"; + $self->log->debug("fetching page: ",$a->as_text," from $page_uri"); if ( $mech->follow_link( url => $a->attr('href') ) ) { $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"; - - ( $el,$attr,$value ) = @{ $args->{scrape} }; - $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } ); - - die "can't find <$el $attr=\"$value\">" unless ($div); + $div = $self->element_by_triplet( + tree => $page_tree, + message => "result $nr", + templates => $args->{scrape} + ); $self->add_record( in_feed => $feed, @@ -282,7 +336,7 @@ # author => # issued => # modified => - ); + ) if ( $div ); $mech->back; $page_tree->delete; @@ -290,6 +344,8 @@ } else { warn "can't follow uri $page_uri: $!\n"; } + } else { + $self->log->debug("result $nr doesn't have link inside, ignoring..."); } last if ($nr == $max);