/[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 74 by dpavlin, Fri Feb 23 17:16:33 2007 UTC revision 88 by dpavlin, Fri Feb 23 21:52:29 2007 UTC
# Line 10  use Module::Pluggable search_path => 'Gr Line 10  use Module::Pluggable search_path => 'Gr
10  use base qw(Class::Accessor);  use base qw(Class::Accessor);
11  Grep::Source->mk_accessors( qw(feed uri q new_items collection) );  Grep::Source->mk_accessors( qw(feed uri q new_items collection) );
12    
13    use HTML::TreeBuilder;
14    use WWW::Mechanize;
15    use XML::Feed;
16    use URI;
17    
18  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
19    
20  =head1 NAME  =head1 NAME
# Line 158  sub content_class { Line 163  sub content_class {
163    
164          foreach my $s ( $self->sources ) {          foreach my $s ( $self->sources ) {
165                  Jifty->log->debug("testing source class $s");                  Jifty->log->debug("testing source class $s");
166                  if ($s->can('content_have') && $s->content_have( $content ) ) {                  if ( $s->can('content_have') ) {
167                          Jifty->log->debug("${s}->content_have succesful");                          my $regex =     $s->content_have( $content ) or
168                          return "$s";                                  die "${s}->content_have didn't return anything";
169                            die "${s}->content_have didn't return regex but ", dump( $regex ), " ref ", ref( $regex )
170                                    unless ( ref($regex) eq 'Regexp' );
171                            if ( $content =~ $regex ) {
172                                    Jifty->log->debug("${s}->content_have succesful");
173                                    return $s;
174                            }
175                  }                  }
176          }          }
177  }  }
178    
179    =head2 scrape
180    
181    Create semi-complex L<WWW::Mechanize> rules to scrape page
182    
183    
184    =cut
185    
186    sub scrape {
187            my $self = shift;
188    
189            my $args = {@_};
190    
191            warn "scrape got args ",dump($args);
192    
193            my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q);
194            die "no uri" unless ($uri);
195            die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');
196    
197            my $mech = WWW::Mechanize->new();
198    
199            $mech->get( $uri );
200    
201            $self->save( 'get.html', $mech->content );
202    
203            if ( $args->{submit_form} ) {
204                    warn "submit form on $uri\n";
205                    $mech->submit_form( %{ $args->{submit_form} } ) or die "can't submit form";
206                    $self->save( 'submit.html', $mech->content );
207            }
208    
209            warn "parse result page\n";
210    
211            my $tree = HTML::TreeBuilder->new or die "can't create html tree";
212            $tree->parse( $mech->content ) or die "can't parse fetched content";
213    
214            die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 );
215            my ( $el,$attr,$value ) = @{ $args->{wrapper} };
216    
217            warn "looking for <$el $attr=\"$value\">";
218    
219            my $div = $tree->look_down( '_tag', $el, sub {
220                            warn dump( $_[0]->attr( $attr ) ),$/;
221                            ( $_[0]->attr( $attr ) || '' ) eq $value;
222            });
223    
224            if ( ! $div ) {
225                    warn "can't find results wrapper <$el $attr=\"$value\">";
226                    return;
227            }
228    
229            my $max = 5;
230            my $nr = 1;
231    
232            my $base_uri = $uri;
233            $base_uri =~ s!\?.*$!!;
234    
235            foreach my $dt ( $div->look_down( '_tag', $args->{results} ) ) {
236                    my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } );
237                    if ( $a ) {
238    
239                            my $href = $a->attr('href') or die "can't find href inside <", $args->{results}, ">";
240                            my $page_uri = URI->new_abs( $a->attr('href'), $base_uri );
241                            $page_uri->query( undef );
242                            $page_uri = $page_uri->canonical;
243    
244                            warn "fetching page: ",$a->as_text," from $page_uri\n";
245                            if ( $mech->follow_link( url => $a->attr('href') ) ) {
246    
247                                    $self->save( "page-${nr}.html", $mech->content );
248    
249                                    my $page_tree = HTML::TreeBuilder->new or die "can't create page tree";
250                                    $page_tree->parse( $mech->content ) or die "can't parse page at $page_uri";
251    
252                                    my ( $el,$attr,$value ) = @{ $args->{scrape} };
253                                    my $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } );
254    
255                                    die "can't find <$el $attr=\"$value\">" unless ($div);
256    
257                                    $self->add_record(
258                                            in_feed => $feed,
259                                            title => $mech->title,
260                                            link => $page_uri,
261                                            content => $div->as_HTML,
262    #                                       summary =>
263    #                                       category =>
264    #                                       author =>
265    #                                       issued =>
266    #                                       modified =>
267                                    );
268    
269                                    $mech->back;
270                                    $page_tree->delete;
271    
272                            } else {
273                                    warn "can't follow uri $page_uri: $!\n";
274                            }
275                    }
276    
277                    last if ($nr == $max);
278                    $nr++;
279            }
280    
281            $tree->delete; # clear memory!
282    
283    }
284    
285    =head2 save
286    
287      save( 'name', $content );
288    
289    Save dumps into C</tmp/grep> if writable
290    
291    =cut
292    
293    sub save {
294            my $self = shift;
295            my ( $file, $content ) = @_;
296            if ( -w '/tmp/grep' ) {
297                    open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!";
298                    print $f $content or die "can't write to $file: $!";
299                    close $f or die "can't close $file: $!";
300                    Jifty->log->debug("saved $file ",length($content)," bytes");
301            }
302    }
303    
304  1;  1;

Legend:
Removed from v.74  
changed lines
  Added in v.88

  ViewVC Help
Powered by ViewVC 1.1.26