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) ); |
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 |
} |
} |
200 |
|
|
201 |
my $args = {@_}; |
my $args = {@_}; |
202 |
|
|
203 |
warn "scrape got args ",dump($args); |
$self->log->debug("scrape with args ",dump($args)); |
204 |
|
|
205 |
my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); |
my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); |
206 |
die "no uri" unless ($uri); |
die "no uri" unless ($uri); |
222 |
$self->save( 'get.html', $mech->content ); |
$self->save( 'get.html', $mech->content ); |
223 |
|
|
224 |
if ( my $form = $args->{submit_form} ) { |
if ( my $form = $args->{submit_form} ) { |
225 |
warn "submit form on $uri with ", dump( $form ),"\n"; |
$self->log->debug("submit form on $uri with ", dump( $form )); |
226 |
$mech->submit_form( %$form ) or die "can't submit form ", dump( $form ); |
$mech->submit_form( %$form ) or die "can't submit form ", dump( $form ); |
227 |
$self->save( 'submit.html', $mech->content ); |
$self->save( 'submit.html', $mech->content ); |
228 |
} |
} |
229 |
|
|
230 |
warn "parse result page\n"; |
$self->log->debug("parse result page"); |
231 |
|
|
232 |
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
233 |
$tree->parse( $mech->content ) or die "can't parse fetched content"; |
$tree->parse( $mech->content ) or die "can't parse fetched content"; |
235 |
die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 ); |
die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 ); |
236 |
my ( $el,$attr,$value ) = @{ $args->{wrapper} }; |
my ( $el,$attr,$value ) = @{ $args->{wrapper} }; |
237 |
|
|
238 |
warn "looking for <$el $attr=\"$value\">"; |
$self->log->debug("looking for <$el $attr=\"$value\">"); |
239 |
|
|
240 |
my $div = $tree->look_down( '_tag', $el, sub { |
my $div = $tree->look_down( '_tag', $el, sub { |
|
warn dump( $_[0]->attr( $attr ) ),$/; |
|
241 |
( $_[0]->attr( $attr ) || '' ) eq $value; |
( $_[0]->attr( $attr ) || '' ) eq $value; |
242 |
}); |
}); |
243 |
|
|
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
249 |
my $max = 5; |
my $max = 15; |
250 |
my $nr = 1; |
my $nr = 1; |
251 |
|
|
252 |
my $base_uri = $uri; |
my $base_uri = $uri; |
261 |
$page_uri->query( undef ); |
$page_uri->query( undef ); |
262 |
$page_uri = $page_uri->canonical; |
$page_uri = $page_uri->canonical; |
263 |
|
|
264 |
warn "fetching page: ",$a->as_text," from $page_uri\n"; |
$self->log->debug("fetching page: ",$a->as_text," from $page_uri"); |
265 |
if ( $mech->follow_link( url => $a->attr('href') ) ) { |
if ( $mech->follow_link( url => $a->attr('href') ) ) { |
266 |
|
|
267 |
$self->save( "page-${nr}.html", $mech->content ); |
$self->save( "page-${nr}.html", $mech->content ); |
313 |
sub save { |
sub save { |
314 |
my $self = shift; |
my $self = shift; |
315 |
my ( $file, $content ) = @_; |
my ( $file, $content ) = @_; |
316 |
|
return unless ( defined($file) && defined($content) ); |
317 |
if ( -w '/tmp/grep' ) { |
if ( -w '/tmp/grep' ) { |
318 |
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
319 |
print $f $content or die "can't write to $file: $!"; |
print $f $content or die "can't write to $file: $!"; |
320 |
close $f or die "can't close $file: $!"; |
close $f or die "can't close $file: $!"; |
321 |
Jifty->log->debug("saved $file ",length($content)," bytes"); |
$self->log->debug("saved $file ",length($content)," bytes"); |
322 |
} |
} |
323 |
} |
} |
324 |
|
|