7 |
|
|
8 |
use Carp qw/verbose/; |
use Carp qw/verbose/; |
9 |
use Module::Pluggable search_path => 'Grep::Source', sub_name => 'sources', require => 1; |
use Module::Pluggable search_path => 'Grep::Source', sub_name => 'sources', require => 1; |
10 |
use base qw(Class::Accessor); |
use base qw(Class::Accessor Jifty::Object); |
11 |
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) ); |
12 |
|
|
13 |
use HTML::TreeBuilder; |
use HTML::TreeBuilder; |
14 |
use WWW::Mechanize; |
use WWW::Mechanize; |
15 |
use XML::Feed; |
use XML::Feed; |
16 |
use URI; |
use URI; |
17 |
|
use HTML::ResolveLink; |
18 |
|
|
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
|
101 |
|
|
102 |
$self->uri( $uri ); |
$self->uri( $uri ); |
103 |
|
|
104 |
Jifty->log->info( $message ); |
$self->log->info( $message ); |
105 |
|
|
106 |
$self->collection( Grep::Model::ItemCollection->new() ); |
$self->collection( Grep::Model::ItemCollection->new() ); |
107 |
|
|
108 |
my $class = $self->feed->source || 'Grep::Source::Feed'; |
my $class = $self->feed->source || 'Grep::Source::Feed'; |
109 |
Jifty->log->debug("using $class"); |
$self->log->debug("using $class"); |
110 |
|
|
111 |
|
$self->search_obj( Grep::Search->new() ); |
112 |
|
$self->log->debug("created " . $self->search_obj); |
113 |
|
|
114 |
$class->fetch( $self ); |
$class->fetch( $self ); |
115 |
|
|
116 |
Grep::Search->finish if $self->new_items; |
$self->search_obj->finish; |
117 |
|
|
118 |
return $self->collection; |
return $self->collection; |
119 |
} |
} |
132 |
sub add_record { |
sub add_record { |
133 |
my $self = shift; |
my $self = shift; |
134 |
|
|
135 |
|
$self->log->confess("no search_obj") unless ($self->search_obj); |
136 |
|
|
137 |
my $i = Grep::Model::Item->new(); |
my $i = Grep::Model::Item->new(); |
138 |
|
|
139 |
my ($ok,$msg) = $i->load_or_create( @_ ); |
my $rec = {@_}; |
140 |
|
|
141 |
|
$self->log->debug("resolving links using base ", $rec->{link}); |
142 |
|
my $resolver = HTML::ResolveLink->new( base => $rec->{link} ); |
143 |
|
$rec->{content} = $resolver->resolve( $rec->{content} ); |
144 |
|
|
145 |
|
my ($ok,$msg) = $i->load_or_create( %$rec ); |
146 |
|
|
147 |
$msg ||= ''; |
$msg ||= ''; |
148 |
|
|
149 |
if ( $ok ) { |
if ( $ok ) { |
150 |
Jifty->log->debug("item ", $i->id, ": $msg"); |
$self->log->debug("item ", $i->id, ": $msg"); |
151 |
$self->collection->add_record( $i ); |
$self->collection->add_record( $i ); |
152 |
|
|
153 |
# is new record? |
# is new record? |
154 |
if ( $msg !~ m/^Found/ ) { |
if ( $msg !~ m/^Found/ ) { |
155 |
Grep::Search->add( $i ); |
$self->search_obj->add( $i ); |
156 |
$self->new_items( ( $self->new_items || 0 ) + 1 ); |
$self->new_items( ( $self->new_items || 0 ) + 1 ); |
157 |
} |
} |
158 |
} else { |
} else { |
174 |
my $content = shift or die "no content?"; |
my $content = shift or die "no content?"; |
175 |
|
|
176 |
foreach my $s ( $self->sources ) { |
foreach my $s ( $self->sources ) { |
177 |
Jifty->log->debug("testing source class $s"); |
$self->log->debug("testing source class $s"); |
178 |
if ( $s->can('content_have') ) { |
if ( $s->can('content_have') ) { |
179 |
my $regex = $s->content_have( $content ) or |
my $regex = $s->content_have( $content ) or |
180 |
die "${s}->content_have didn't return anything"; |
die "${s}->content_have didn't return anything"; |
181 |
die "${s}->content_have didn't return regex but ", dump( $regex ), " ref ", ref( $regex ) |
die "${s}->content_have didn't return regex but ", dump( $regex ), " ref ", ref( $regex ) |
182 |
unless ( ref($regex) eq 'Regexp' ); |
unless ( ref($regex) eq 'Regexp' ); |
183 |
if ( $content =~ $regex ) { |
if ( $content =~ $regex ) { |
184 |
Jifty->log->debug("${s}->content_have succesful"); |
$self->log->debug("${s}->content_have succesful"); |
185 |
return $s; |
return $s; |
186 |
} |
} |
187 |
} |
} |
188 |
} |
} |
189 |
} |
} |
190 |
|
|
191 |
|
|
192 |
|
=head2 element_by_triplet |
193 |
|
|
194 |
|
Helper method to select element(s) using C<element/attribute/value> triplet using |
195 |
|
L<HTML::TreeBuilder> trees. |
196 |
|
|
197 |
|
my $el = $self->element_by_triplet( |
198 |
|
tree => $tree_or_element, |
199 |
|
triplets => [ qw/ |
200 |
|
div id target |
201 |
|
div class another |
202 |
|
/ ], |
203 |
|
message => 'find search result element', |
204 |
|
fatal => 1, # die instead of warn |
205 |
|
); |
206 |
|
|
207 |
|
=cut |
208 |
|
|
209 |
|
sub element_by_triplet { |
210 |
|
my $self = shift; |
211 |
|
|
212 |
|
my $args = {@_}; |
213 |
|
|
214 |
|
my $tree = $args->{tree} || die "no tree"; |
215 |
|
my $message = $args->{message} || ''; |
216 |
|
my $fatal = $args->{fatal}; |
217 |
|
die "no triplets" unless defined( $args->{triplets} ); |
218 |
|
my @triplets; |
219 |
|
if ( ref( $args->{triplets} ) eq 'ARRAY' ) { |
220 |
|
@triplets = @{ $args->{triplets} }; |
221 |
|
} else { |
222 |
|
@triplets = ( $args->{triplets} ); |
223 |
|
} |
224 |
|
|
225 |
|
push @triplets, ( undef, undef ) if ( $#triplets == 0 ); |
226 |
|
|
227 |
|
die "triplet doesn't have 3 elements but ", $#triplets unless ( |
228 |
|
( $#triplets + 1 ) % 3 == 0 |
229 |
|
); |
230 |
|
|
231 |
|
my ( $el, $attr, $value ); |
232 |
|
|
233 |
|
my @results; |
234 |
|
my @tags; |
235 |
|
|
236 |
|
$self->log->debug("looking for $message ", dump( @triplets )); |
237 |
|
while ( @triplets ) { |
238 |
|
( $el,$attr,$value ) = splice( @triplets, 0, 3 ); |
239 |
|
my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>"; |
240 |
|
push @tags, $tag; |
241 |
|
@results = $tree->look_down( '_tag', $el, sub { |
242 |
|
return 1 unless ( $attr && $value ); |
243 |
|
( $_[0]->attr( $attr ) || '' ) =~ m/\b\Q$value\E\b/ |
244 |
|
}); |
245 |
|
last if @results; |
246 |
|
} |
247 |
|
|
248 |
|
if ( ! @results ) { |
249 |
|
my $msg = "can't find $message " . join(" ", @tags); |
250 |
|
die $msg if ( $fatal ); |
251 |
|
#warn $msg; |
252 |
|
return; |
253 |
|
} |
254 |
|
|
255 |
|
$self->log->debug("found ", $#results + 1, " elements"); |
256 |
|
#warn dump( map { $_->as_HTML } @results ); |
257 |
|
|
258 |
|
return @results if wantarray; |
259 |
|
return shift @results; |
260 |
|
} |
261 |
|
|
262 |
=head2 scrape |
=head2 scrape |
263 |
|
|
264 |
Create semi-complex L<WWW::Mechanize> rules to scrape page |
Create semi-complex L<WWW::Mechanize> rules to scrape page easily |
265 |
|
|
266 |
|
$parent->scrape( |
267 |
|
# if search string isn't part or URI |
268 |
|
submit_form => { |
269 |
|
fields => { |
270 |
|
value => $parent->q, |
271 |
|
}, |
272 |
|
button => 'fullsearch', |
273 |
|
}, |
274 |
|
# element with search results |
275 |
|
wrapper => [ qw/div class searchresults/ ], |
276 |
|
# element (or tripple) for each result with link |
277 |
|
# <a href=".."> inside it to full-text result |
278 |
|
results => 'dt', |
279 |
|
# collect which element on page linked from results |
280 |
|
scrape => [ qw/div id page/ ], |
281 |
|
# when search returns just single hit, it will redirect to result page |
282 |
|
redirect_single_result => 1, |
283 |
|
); |
284 |
|
|
285 |
=cut |
=cut |
286 |
|
|
289 |
|
|
290 |
my $args = {@_}; |
my $args = {@_}; |
291 |
|
|
292 |
warn "scrape got args ",dump($args); |
$self->log->debug("scrape with args ",dump($args)); |
293 |
|
|
294 |
my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); |
my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); |
295 |
die "no uri" unless ($uri); |
die "no uri" unless ($uri); |
296 |
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'); |
297 |
|
|
298 |
sub save_html { |
sub mech_warn { |
299 |
my ( $file, $content ) = @_; |
my $m = shift || return; |
300 |
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: $!"; |
|
|
} |
|
301 |
} |
} |
302 |
|
|
303 |
my $mech = WWW::Mechanize->new(); |
my $mech = WWW::Mechanize->new( |
304 |
|
cookie_jar => {}, |
305 |
|
onwarn => \&mech_warn, |
306 |
|
onerror => \&mech_warn, |
307 |
|
); |
308 |
|
|
309 |
$mech->get( $uri ); |
$mech->get( $uri ); |
310 |
|
|
311 |
save_html( 'get', $mech->content ); |
$self->save( 'get.html', $mech->content ); |
312 |
|
|
313 |
if ( $args->{submit_form} ) { |
if ( my $form = $args->{submit_form} ) { |
314 |
warn "submit form on $uri\n"; |
$self->log->debug("submit form on $uri with ", dump( $form )); |
315 |
$mech->submit_form( %{ $args->{submit_form} } ) or die "can't submit form"; |
$mech->submit_form( %$form ) or die "can't submit form ", dump( $form ); |
316 |
save_html( 'submit', $mech->content ); |
$self->save( 'submit.html', $mech->content ); |
317 |
} |
} |
318 |
|
|
319 |
warn "parse result page\n"; |
$self->log->debug("parse result page"); |
320 |
|
|
321 |
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
322 |
$tree->parse( $mech->content ) or die "can't parse fetched content"; |
$tree->parse( $mech->content ) or die "can't parse fetched content"; |
323 |
|
|
324 |
die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 ); |
my @wrapper_divs = $self->element_by_triplet( |
325 |
my ( $el,$attr,$value ) = @{ $args->{wrapper} }; |
tree => $tree, |
326 |
|
triplets => $args->{wrapper}, |
327 |
warn "looking for <$el $attr=\"$value\">"; |
message => 'wrapper for all results', |
328 |
|
# 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 $div = $tree->look_down( '_tag', $el, sub { |
my $max = 15; |
|
warn dump( $_[0]->attr( $attr ) ),$/; |
|
|
( $_[0]->attr( $attr ) || '' ) eq $value; |
|
|
}); |
|
|
|
|
|
die "can't find results wrapper <$el $attr=\"$value\">" unless ( $div ); |
|
|
|
|
|
my $max = 5; |
|
334 |
my $nr = 1; |
my $nr = 1; |
335 |
|
|
336 |
my $base_uri = $uri; |
my $base_uri = $uri; |
337 |
$base_uri =~ s!\?.*$!!; |
$base_uri =~ s!\?.*$!!; |
338 |
|
|
339 |
foreach my $dt ( $div->look_down( '_tag', $args->{results} ) ) { |
# directly got first result |
340 |
|
if ( $args->{redirect_single_result} && ! @wrapper_divs ) { |
341 |
|
|
342 |
|
my $uri = $mech->uri; $uri->query( undef ); $uri = $uri->canonical; |
343 |
|
|
344 |
|
my $div = $self->element_by_triplet( |
345 |
|
tree => $tree, |
346 |
|
message => "single result - redirect to $uri", |
347 |
|
triplets => $args->{scrape}, |
348 |
|
fatal => 0, |
349 |
|
); |
350 |
|
|
351 |
|
$self->add_record( |
352 |
|
in_feed => $feed, |
353 |
|
title => $mech->title, |
354 |
|
link => $uri, |
355 |
|
content => $div->as_HTML, |
356 |
|
); |
357 |
|
|
358 |
|
$tree->delete; # clear memory! |
359 |
|
return; |
360 |
|
} |
361 |
|
|
362 |
|
my @r; |
363 |
|
|
364 |
|
foreach my $div ( @wrapper_divs ) { |
365 |
|
|
366 |
|
my @r_here = $self->element_by_triplet( |
367 |
|
tree => $div, |
368 |
|
triplets => $args->{results}, |
369 |
|
message => 'result element', |
370 |
|
); |
371 |
|
|
372 |
|
push @r, @r_here if (@r_here); |
373 |
|
} |
374 |
|
|
375 |
|
$self->log->debug("in total, found ", $#r + 1, " results in ", $#wrapper_divs + 1, " result wrapper elements"); |
376 |
|
|
377 |
|
foreach my $dt ( @r ) { |
378 |
my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } ); |
my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } ); |
379 |
if ( $a ) { |
if ( $a ) { |
380 |
|
|
381 |
my $href = $a->attr('href') or die "can't find href inside <", $args->{results}, ">"; |
my $href = $a->attr('href') or die "can't find href inside <", $args->{results}, ">"; |
382 |
my $page_uri = URI->new_abs( $a->attr('href'), $base_uri ); |
|
383 |
|
my $page_uri = URI->new_abs( $href, $base_uri ); |
384 |
$page_uri->query( undef ); |
$page_uri->query( undef ); |
385 |
$page_uri = $page_uri->canonical; |
$page_uri = $page_uri->canonical; |
386 |
|
|
387 |
warn "fetching page: ",$a->as_text," from $page_uri\n"; |
if ( my $item = Grep::Model::Item->link_current( $page_uri ) ) { |
388 |
if ( $mech->follow_link( url => $a->attr('href') ) ) { |
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"); |
394 |
|
if ( $mech->follow_link( url => $href ) ) { |
395 |
|
|
396 |
save_html( "page-${nr}", $mech->content ); |
$self->save( "page-${nr}.html", $mech->content ); |
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 @divs = $self->element_by_triplet( |
401 |
|
tree => $page_tree, |
402 |
|
message => "result page $nr", |
403 |
|
triplets => $args->{scrape} |
404 |
|
); |
405 |
|
|
406 |
my ( $el,$attr,$value ) = @{ $args->{scrape} }; |
if ( @divs ) { |
|
my $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } ); |
|
407 |
|
|
408 |
die "can't find <$el $attr=\"$value\">" unless ($div); |
my $html = join("<hr/>\n", map { $_->as_HTML } @divs ); |
409 |
|
$self->log->debug("combined ", $#divs + 1, " elements elements in ", length($html), " bytes"); |
410 |
|
|
411 |
$self->add_record( |
$self->add_record( |
412 |
in_feed => $feed, |
in_feed => $feed, |
413 |
title => $mech->title, |
title => $mech->title, |
414 |
link => $page_uri, |
link => $page_uri, |
415 |
content => $div->as_HTML, |
content => $html, |
416 |
# summary => |
# summary => |
417 |
# category => |
# category => |
418 |
# author => |
# author => |
419 |
# issued => |
# issued => |
420 |
# modified => |
# 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; |
430 |
} else { |
} else { |
431 |
warn "can't follow uri $page_uri: $!\n"; |
warn "can't follow uri $page_uri: $!\n"; |
432 |
} |
} |
433 |
|
} else { |
434 |
|
$self->log->debug("result $nr doesn't have link inside, ignoring..."); |
435 |
} |
} |
436 |
|
|
437 |
last if ($nr == $max); |
last if ($nr == $max); |
442 |
|
|
443 |
} |
} |
444 |
|
|
445 |
|
=head2 save |
446 |
|
|
447 |
|
save( 'name', $content ); |
448 |
|
|
449 |
|
Save dumps into C</tmp/grep> if writable |
450 |
|
|
451 |
|
=cut |
452 |
|
|
453 |
|
sub save { |
454 |
|
my $self = shift; |
455 |
|
my ( $file, $content ) = @_; |
456 |
|
return unless ( defined($file) && defined($content) ); |
457 |
|
if ( -w '/tmp/grep' ) { |
458 |
|
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
459 |
|
print $f $content or die "can't write to $file: $!"; |
460 |
|
close $f or die "can't close $file: $!"; |
461 |
|
$self->log->debug("saved $file ",length($content)," bytes"); |
462 |
|
} |
463 |
|
} |
464 |
|
|
465 |
1; |
1; |