/[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 133 by dpavlin, Tue May 1 20:50:14 2007 UTC revision 171 by dpavlin, Wed Jul 4 14:10:43 2007 UTC
# Line 233  sub element_by_triplet { Line 233  sub element_by_triplet {
233          my @results;          my @results;
234          my @tags;          my @tags;
235    
236  warn "triplets = ",dump( @triplets );          $self->log->debug("looking for $message ", dump( @triplets ));
   
237          while ( @triplets ) {          while ( @triplets ) {
238                  ( $el,$attr,$value ) = splice( @triplets, 0, 3 );                  ( $el,$attr,$value ) = splice( @triplets, 0, 3 );
239                  my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>";                  my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>";
240                  push @tags, $tag;                  push @tags, $tag;
                 $self->log->debug("looking for $message $tag");  
241                  @results = $tree->look_down( '_tag', $el, sub {                  @results = $tree->look_down( '_tag', $el, sub {
242                                  return 1 unless ( $attr && $value );                                  return 1 unless ( $attr && $value );
243                                  ( $_[0]->attr( $attr ) || '' ) eq $value;                                  ( $_[0]->attr( $attr ) || '' ) =~ m/\b\Q$value\E\b/
244                  });                  });
245                  last if @results;                  last if @results;
246          }          }
247    
248          if ( ! @results ) {          if ( ! @results ) {
249                  my $msg = "can't find $message ", join(" ", @tags);                  my $msg = "can't find $message " . join(" ", @tags);
250                  die $msg if ( $fatal );                  die $msg if ( $fatal );
251                  warn $msg;                  #warn $msg;
252                  return;                  return;
253          }          }
254    
255          $self->log->debug("found ", $#results + 1, " results");          $self->log->debug("found ", $#results + 1, " elements");
   
256          #warn dump( map { $_->as_HTML } @results );          #warn dump( map { $_->as_HTML } @results );
257    
258          return @results if wantarray;          return @results if wantarray;
# Line 328  sub scrape { Line 325  sub scrape {
325                  tree => $tree,                  tree => $tree,
326                  triplets => $args->{wrapper},                  triplets => $args->{wrapper},
327                  message => 'wrapper for all results',                  message => 'wrapper for all results',
328                  fatal => $args->{redirect_single_result} ? 0 : 1,                  # on closer recollection, this shouldn't be ever fatal, because
329                    # "no results found" page might not contain wrapper
330                    #fatal => $args->{redirect_single_result} ? 0 : 1,
331          );          );
332    
333          my $max = 15;          my $max = 15;
# Line 385  sub scrape { Line 384  sub scrape {
384                          $page_uri->query( undef );                          $page_uri->query( undef );
385                          $page_uri = $page_uri->canonical;                          $page_uri = $page_uri->canonical;
386    
387                            if ( my $item = Grep::Model::Item->link_current( $page_uri ) ) {
388                                    Jifty->log->debug("using cached page for $page_uri");
389                                    $self->collection->add_record( $item );
390                                    next;
391                            }
392    
393                          $self->log->debug("fetching page: ",$a->as_text," from $page_uri");                          $self->log->debug("fetching page: ",$a->as_text," from $page_uri");
394                          if ( $mech->follow_link( url => $href ) ) {                          if ( $mech->follow_link( url => $href ) ) {
395    
# Line 392  sub scrape { Line 397  sub scrape {
397    
398                                  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";
399                                  $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";
400                                  my $div = $self->element_by_triplet(                                  my @divs = $self->element_by_triplet(
401                                          tree => $page_tree,                                          tree => $page_tree,
402                                          message => "result page $nr",                                          message => "result page $nr",
403                                          triplets => $args->{scrape}                                          triplets => $args->{scrape}
404                                  );                                  );
405    
406                                  $self->add_record(                                  if ( @divs ) {
407                                          in_feed => $feed,  
408                                          title => $mech->title,                                          my $html = join("<hr/>\n", map { $_->as_HTML } @divs );
409                                          link => $page_uri,                                          $self->log->debug("combined ", $#divs + 1, " elements elements in ", length($html), " bytes");
410                                          content => $div->as_HTML,  
411  #                                       summary =>                                          $self->add_record(
412  #                                       category =>                                                  in_feed => $feed,
413  #                                       author =>                                                  title => $mech->title,
414  #                                       issued =>                                                  link => $page_uri,
415  #                                       modified =>                                                  content => $html,
416                                  ) if ( $div );  #                                               summary =>
417    #                                               category =>
418    #                                               author =>
419    #                                               issued =>
420    #                                               modified =>
421                                            );
422    
423                                    } else {
424                                            $self->log->debug("NO CONTENT scraped from page $nr");
425                                    }
426    
427                                  $mech->back;                                  $mech->back;
428                                  $page_tree->delete;                                  $page_tree->delete;

Legend:
Removed from v.133  
changed lines
  Added in v.171

  ViewVC Help
Powered by ViewVC 1.1.26