1 |
dpavlin |
72 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 02/22/07 20:30:00 CET |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
package Grep::Source; |
7 |
|
|
|
8 |
|
|
use Carp qw/verbose/; |
9 |
|
|
use Module::Pluggable search_path => 'Grep::Source', sub_name => 'sources', require => 1; |
10 |
dpavlin |
100 |
use base qw(Class::Accessor Jifty::Object); |
11 |
dpavlin |
121 |
Grep::Source->mk_accessors( qw(feed uri q new_items collection search_obj tree) ); |
12 |
dpavlin |
72 |
|
13 |
dpavlin |
85 |
use HTML::TreeBuilder; |
14 |
|
|
use WWW::Mechanize; |
15 |
|
|
use XML::Feed; |
16 |
|
|
use URI; |
17 |
dpavlin |
96 |
use HTML::ResolveLink; |
18 |
dpavlin |
85 |
|
19 |
dpavlin |
72 |
use Data::Dump qw/dump/; |
20 |
|
|
|
21 |
|
|
=head1 NAME |
22 |
|
|
|
23 |
|
|
Grep::Source - base class for implementation of different sources for Grep |
24 |
|
|
|
25 |
|
|
=head1 METHODS |
26 |
|
|
|
27 |
|
|
This is mostly documentation because most of methods are implemented by plugins. |
28 |
|
|
|
29 |
|
|
=head2 sources |
30 |
|
|
|
31 |
|
|
my @sources = Grep::Source->sources(); |
32 |
|
|
|
33 |
|
|
Returns all available sources. |
34 |
|
|
|
35 |
|
|
=cut |
36 |
|
|
|
37 |
dpavlin |
73 |
Jifty->log->debug("Found source plugins: ", join(", ", __PACKAGE__->sources() ) ); |
38 |
dpavlin |
72 |
|
39 |
|
|
=head2 new |
40 |
|
|
|
41 |
|
|
my $source = Grep::Source->new({ feed => $feed_record }); |
42 |
|
|
|
43 |
dpavlin |
73 |
This will also setup: |
44 |
|
|
|
45 |
|
|
=head2 feed |
46 |
|
|
|
47 |
|
|
isa L<Grep::Model::Feed> |
48 |
|
|
|
49 |
dpavlin |
72 |
=head2 search |
50 |
|
|
|
51 |
|
|
my $collection = $source->search( 'query string' ); |
52 |
|
|
|
53 |
|
|
It will also setup following accessors: |
54 |
|
|
|
55 |
dpavlin |
73 |
=head2 q |
56 |
dpavlin |
72 |
|
57 |
|
|
Search query |
58 |
|
|
|
59 |
dpavlin |
73 |
=head2 uri |
60 |
dpavlin |
72 |
|
61 |
|
|
URI of feed with embedded search query |
62 |
|
|
|
63 |
dpavlin |
73 |
=head2 new_items |
64 |
dpavlin |
72 |
|
65 |
|
|
Number of new items in result collection |
66 |
|
|
|
67 |
|
|
=head2 collection |
68 |
|
|
|
69 |
|
|
Actuall results which is L<Grep::Model::ItemCollection>, so following will |
70 |
|
|
work: |
71 |
|
|
|
72 |
|
|
print "and ", $self->collection->count, " total items"; |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
Also setups number of new items |
76 |
|
|
|
77 |
|
|
print $source->new_items, " items new"; |
78 |
|
|
|
79 |
|
|
=cut |
80 |
|
|
|
81 |
|
|
sub search { |
82 |
|
|
my $self = shift; |
83 |
|
|
|
84 |
|
|
my $q = shift; |
85 |
|
|
|
86 |
|
|
$q ? $self->q( $q ) : $q = $self->q; |
87 |
|
|
|
88 |
|
|
die "no q?" unless ( $self->q ); |
89 |
|
|
die "no feed?" unless ( $self->feed ); |
90 |
|
|
die "feed not Grep::Model::Feed" unless ( $self->feed->isa('Grep::Model::Feed') ); |
91 |
|
|
|
92 |
|
|
my $message; |
93 |
|
|
my $uri = $self->feed->uri; |
94 |
|
|
if ($uri =~ m/%s/) { |
95 |
|
|
$uri = $self->feed->search_uri( $q ); |
96 |
|
|
$message = 'Searching'; |
97 |
|
|
} else { |
98 |
|
|
$message = 'Fetching'; |
99 |
|
|
} |
100 |
|
|
$message .= ' ' . $self->feed->title . " at $uri"; |
101 |
|
|
|
102 |
|
|
$self->uri( $uri ); |
103 |
|
|
|
104 |
dpavlin |
100 |
$self->log->info( $message ); |
105 |
dpavlin |
72 |
|
106 |
|
|
$self->collection( Grep::Model::ItemCollection->new() ); |
107 |
|
|
|
108 |
dpavlin |
73 |
my $class = $self->feed->source || 'Grep::Source::Feed'; |
109 |
dpavlin |
100 |
$self->log->debug("using $class"); |
110 |
dpavlin |
72 |
|
111 |
dpavlin |
110 |
$self->search_obj( Grep::Search->new() ); |
112 |
|
|
$self->log->debug("created " . $self->search_obj); |
113 |
dpavlin |
72 |
|
114 |
dpavlin |
110 |
$class->fetch( $self ); |
115 |
|
|
|
116 |
|
|
$self->search_obj->finish; |
117 |
|
|
|
118 |
dpavlin |
72 |
return $self->collection; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
=head2 add_record |
122 |
|
|
|
123 |
dpavlin |
73 |
Plugins will be called with parametar C<$parent> so they can call this method to add |
124 |
|
|
record into result collection (and store in cache and index). |
125 |
|
|
|
126 |
dpavlin |
72 |
$parent->add_record( id => 42, foo => 'bar', ... ); |
127 |
|
|
|
128 |
dpavlin |
73 |
This will also update L</new_items> |
129 |
|
|
|
130 |
dpavlin |
72 |
=cut |
131 |
|
|
|
132 |
|
|
sub add_record { |
133 |
|
|
my $self = shift; |
134 |
|
|
|
135 |
dpavlin |
110 |
$self->log->confess("no search_obj") unless ($self->search_obj); |
136 |
dpavlin |
109 |
|
137 |
dpavlin |
72 |
my $i = Grep::Model::Item->new(); |
138 |
|
|
|
139 |
dpavlin |
96 |
my $rec = {@_}; |
140 |
dpavlin |
72 |
|
141 |
dpavlin |
102 |
$self->log->debug("resolving links using base ", $rec->{link}); |
142 |
dpavlin |
96 |
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 |
dpavlin |
72 |
$msg ||= ''; |
148 |
|
|
|
149 |
|
|
if ( $ok ) { |
150 |
dpavlin |
100 |
$self->log->debug("item ", $i->id, ": $msg"); |
151 |
dpavlin |
72 |
$self->collection->add_record( $i ); |
152 |
|
|
|
153 |
|
|
# is new record? |
154 |
|
|
if ( $msg !~ m/^Found/ ) { |
155 |
dpavlin |
110 |
$self->search_obj->add( $i ); |
156 |
dpavlin |
74 |
$self->new_items( ( $self->new_items || 0 ) + 1 ); |
157 |
dpavlin |
72 |
} |
158 |
|
|
} else { |
159 |
|
|
warn "can't add entry ", dump( @_ ), "\n"; |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
dpavlin |
73 |
=head2 content_class |
164 |
|
|
|
165 |
|
|
Return class registred for particular content. |
166 |
|
|
|
167 |
|
|
my $class = $source->content_class( $content ); |
168 |
|
|
|
169 |
|
|
=cut |
170 |
|
|
|
171 |
|
|
sub content_class { |
172 |
|
|
my $self = shift; |
173 |
|
|
|
174 |
|
|
my $content = shift or die "no content?"; |
175 |
|
|
|
176 |
|
|
foreach my $s ( $self->sources ) { |
177 |
dpavlin |
100 |
$self->log->debug("testing source class $s"); |
178 |
dpavlin |
82 |
if ( $s->can('content_have') ) { |
179 |
|
|
my $regex = $s->content_have( $content ) or |
180 |
|
|
die "${s}->content_have didn't return anything"; |
181 |
|
|
die "${s}->content_have didn't return regex but ", dump( $regex ), " ref ", ref( $regex ) |
182 |
|
|
unless ( ref($regex) eq 'Regexp' ); |
183 |
|
|
if ( $content =~ $regex ) { |
184 |
dpavlin |
100 |
$self->log->debug("${s}->content_have succesful"); |
185 |
dpavlin |
82 |
return $s; |
186 |
|
|
} |
187 |
dpavlin |
73 |
} |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
|
191 |
dpavlin |
85 |
=head2 scrape |
192 |
|
|
|
193 |
|
|
Create semi-complex L<WWW::Mechanize> rules to scrape page |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
=cut |
197 |
|
|
|
198 |
dpavlin |
121 |
sub element_by_triplet { |
199 |
|
|
my $self = shift; |
200 |
|
|
|
201 |
|
|
my $args = {@_}; |
202 |
|
|
|
203 |
|
|
my $tree = $args->{tree} || die "no tree"; |
204 |
|
|
my $message = $args->{message}; |
205 |
|
|
my $fatal = $args->{fatal}; |
206 |
|
|
die "no templates" unless defined( $args->{templates} ); |
207 |
|
|
my @templates = @{ $args->{templates} }; |
208 |
|
|
|
209 |
|
|
push @templates, ( undef, undef ) if ( $#templates == 0 ); |
210 |
|
|
|
211 |
|
|
die "wrapper doesn't have 3 elements but ", $#templates unless ( |
212 |
|
|
( $#templates + 1 ) % 3 == 0 |
213 |
|
|
); |
214 |
|
|
|
215 |
|
|
my ( $result, $el, $attr, $value ); |
216 |
|
|
|
217 |
|
|
my @tags; |
218 |
|
|
|
219 |
|
|
while ( @templates ) { |
220 |
|
|
( $el,$attr,$value ) = splice( @templates, 0, 3 ); |
221 |
|
|
my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>"; |
222 |
|
|
push @tags, $tag; |
223 |
|
|
$self->log->debug("looking for $message $tag"); |
224 |
|
|
$result = $tree->look_down( '_tag', $el, sub { |
225 |
|
|
return 1 unless ( $attr && $value ); |
226 |
|
|
( $_[0]->attr( $attr ) || '' ) eq $value; |
227 |
|
|
}); |
228 |
|
|
last if $result; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
if ( ! $result ) { |
232 |
|
|
my $msg = "can't find $message ", join(" ", @tags); |
233 |
|
|
die $msg if ( $fatal ); |
234 |
|
|
warn $msg; |
235 |
|
|
return; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
return $result; |
239 |
|
|
} |
240 |
|
|
|
241 |
dpavlin |
85 |
sub scrape { |
242 |
|
|
my $self = shift; |
243 |
|
|
|
244 |
|
|
my $args = {@_}; |
245 |
|
|
|
246 |
dpavlin |
102 |
$self->log->debug("scrape with args ",dump($args)); |
247 |
dpavlin |
85 |
|
248 |
|
|
my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q); |
249 |
|
|
die "no uri" unless ($uri); |
250 |
|
|
die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed'); |
251 |
|
|
|
252 |
dpavlin |
92 |
sub mech_warn { |
253 |
|
|
my $m = shift || return; |
254 |
|
|
warn $m; |
255 |
|
|
} |
256 |
dpavlin |
85 |
|
257 |
dpavlin |
92 |
my $mech = WWW::Mechanize->new( |
258 |
|
|
cookie_jar => {}, |
259 |
|
|
onwarn => \&mech_warn, |
260 |
|
|
onerror => \&mech_warn, |
261 |
|
|
); |
262 |
|
|
|
263 |
dpavlin |
85 |
$mech->get( $uri ); |
264 |
|
|
|
265 |
dpavlin |
86 |
$self->save( 'get.html', $mech->content ); |
266 |
dpavlin |
85 |
|
267 |
dpavlin |
92 |
if ( my $form = $args->{submit_form} ) { |
268 |
dpavlin |
102 |
$self->log->debug("submit form on $uri with ", dump( $form )); |
269 |
dpavlin |
92 |
$mech->submit_form( %$form ) or die "can't submit form ", dump( $form ); |
270 |
dpavlin |
86 |
$self->save( 'submit.html', $mech->content ); |
271 |
dpavlin |
85 |
} |
272 |
|
|
|
273 |
dpavlin |
102 |
$self->log->debug("parse result page"); |
274 |
dpavlin |
85 |
|
275 |
|
|
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
276 |
|
|
$tree->parse( $mech->content ) or die "can't parse fetched content"; |
277 |
|
|
|
278 |
dpavlin |
121 |
my $div = $self->element_by_triplet( |
279 |
|
|
tree => $tree, |
280 |
|
|
templates => $args->{wrapper}, |
281 |
|
|
message => 'wrapper for all results', |
282 |
|
|
fatal => 1 |
283 |
|
|
); |
284 |
dpavlin |
85 |
|
285 |
dpavlin |
103 |
my $max = 15; |
286 |
dpavlin |
85 |
my $nr = 1; |
287 |
|
|
|
288 |
|
|
my $base_uri = $uri; |
289 |
|
|
$base_uri =~ s!\?.*$!!; |
290 |
|
|
|
291 |
dpavlin |
121 |
my @r = $self->element_by_triplet( |
292 |
|
|
tree => $tree, |
293 |
|
|
templates => $args->{results}, |
294 |
|
|
message => 'result element', |
295 |
|
|
); |
296 |
dpavlin |
113 |
|
297 |
|
|
foreach my $dt ( @r ) { |
298 |
dpavlin |
85 |
my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } ); |
299 |
|
|
if ( $a ) { |
300 |
|
|
|
301 |
|
|
my $href = $a->attr('href') or die "can't find href inside <", $args->{results}, ">"; |
302 |
|
|
my $page_uri = URI->new_abs( $a->attr('href'), $base_uri ); |
303 |
|
|
$page_uri->query( undef ); |
304 |
|
|
$page_uri = $page_uri->canonical; |
305 |
|
|
|
306 |
dpavlin |
102 |
$self->log->debug("fetching page: ",$a->as_text," from $page_uri"); |
307 |
dpavlin |
85 |
if ( $mech->follow_link( url => $a->attr('href') ) ) { |
308 |
|
|
|
309 |
dpavlin |
86 |
$self->save( "page-${nr}.html", $mech->content ); |
310 |
dpavlin |
85 |
|
311 |
|
|
my $page_tree = HTML::TreeBuilder->new or die "can't create page tree"; |
312 |
|
|
$page_tree->parse( $mech->content ) or die "can't parse page at $page_uri"; |
313 |
dpavlin |
121 |
$div = $self->element_by_triplet( |
314 |
|
|
tree => $page_tree, |
315 |
|
|
message => "result $nr", |
316 |
|
|
templates => $args->{scrape} |
317 |
|
|
); |
318 |
dpavlin |
85 |
|
319 |
|
|
$self->add_record( |
320 |
|
|
in_feed => $feed, |
321 |
|
|
title => $mech->title, |
322 |
|
|
link => $page_uri, |
323 |
|
|
content => $div->as_HTML, |
324 |
|
|
# summary => |
325 |
|
|
# category => |
326 |
|
|
# author => |
327 |
|
|
# issued => |
328 |
|
|
# modified => |
329 |
dpavlin |
121 |
) if ( $div ); |
330 |
dpavlin |
85 |
|
331 |
|
|
$mech->back; |
332 |
|
|
$page_tree->delete; |
333 |
|
|
|
334 |
|
|
} else { |
335 |
|
|
warn "can't follow uri $page_uri: $!\n"; |
336 |
|
|
} |
337 |
dpavlin |
121 |
} else { |
338 |
|
|
$self->log->debug("result $nr doesn't have link inside, ignoring..."); |
339 |
dpavlin |
85 |
} |
340 |
|
|
|
341 |
|
|
last if ($nr == $max); |
342 |
|
|
$nr++; |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
$tree->delete; # clear memory! |
346 |
|
|
|
347 |
|
|
} |
348 |
|
|
|
349 |
dpavlin |
86 |
=head2 save |
350 |
|
|
|
351 |
|
|
save( 'name', $content ); |
352 |
|
|
|
353 |
|
|
Save dumps into C</tmp/grep> if writable |
354 |
|
|
|
355 |
|
|
=cut |
356 |
|
|
|
357 |
|
|
sub save { |
358 |
|
|
my $self = shift; |
359 |
|
|
my ( $file, $content ) = @_; |
360 |
dpavlin |
100 |
return unless ( defined($file) && defined($content) ); |
361 |
dpavlin |
86 |
if ( -w '/tmp/grep' ) { |
362 |
|
|
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
363 |
|
|
print $f $content or die "can't write to $file: $!"; |
364 |
|
|
close $f or die "can't close $file: $!"; |
365 |
dpavlin |
100 |
$self->log->debug("saved $file ",length($content)," bytes"); |
366 |
dpavlin |
86 |
} |
367 |
|
|
} |
368 |
|
|
|
369 |
dpavlin |
72 |
1; |