/[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 109 by dpavlin, Wed Mar 14 18:46:37 2007 UTC
# Line 7  package Grep::Source; Line 7  package Grep::Source;
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) );
12    
13    use HTML::TreeBuilder;
14    use WWW::Mechanize;
15    use XML::Feed;
16    use URI;
17    use HTML::ResolveLink;
18    
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    
# Line 95  sub search { Line 101  sub search {
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");
   
         $class->fetch( $self );  
110    
111          Grep::Search->finish if $self->new_items;          my $parent = $self;
112            $class->fetch( $parent );
113            undef $parent;
114    
115          return $self->collection;          return $self->collection;
116  }  }
# Line 123  This will also update L</new_items> Line 129  This will also update L</new_items>
129  sub add_record {  sub add_record {
130          my $self = shift;          my $self = shift;
131    
132            $self->search( Grep::Search->new() ) unless ($self->search);
133    
134          my $i = Grep::Model::Item->new();          my $i = Grep::Model::Item->new();
135    
136          my ($ok,$msg) = $i->load_or_create( @_ );          my $rec = {@_};
137    
138            $self->log->debug("resolving links using base ", $rec->{link});
139            my $resolver = HTML::ResolveLink->new( base => $rec->{link} );
140            $rec->{content} = $resolver->resolve( $rec->{content} );
141    
142            my ($ok,$msg) = $i->load_or_create( %$rec );
143    
144          $msg ||= '';          $msg ||= '';
145    
146          if ( $ok ) {          if ( $ok ) {
147                  Jifty->log->debug("item ", $i->id, ": $msg");                  $self->log->debug("item ", $i->id, ": $msg");
148                  $self->collection->add_record( $i );                  $self->collection->add_record( $i );
149    
150                  # is new record?                  # is new record?
151                  if ( $msg !~ m/^Found/ ) {                  if ( $msg !~ m/^Found/ ) {
152                          Grep::Search->add( $i );                          $search->add( $i );
153                          $self->new_items( ( $self->new_items || 0 ) + 1 );                          $self->new_items( ( $self->new_items || 0 ) + 1 );
154                  }                  }
155          } else {          } else {
# Line 157  sub content_class { Line 171  sub content_class {
171          my $content = shift or die "no content?";          my $content = shift or die "no content?";
172    
173          foreach my $s ( $self->sources ) {          foreach my $s ( $self->sources ) {
174                  Jifty->log->debug("testing source class $s");                  $self->log->debug("testing source class $s");
175                  if ($s->can('content_have') && $s->content_have( $content ) ) {                  if ( $s->can('content_have') ) {
176                          Jifty->log->debug("${s}->content_have succesful");                          my $regex =     $s->content_have( $content ) or
177                          return "$s";                                  die "${s}->content_have didn't return anything";
178                            die "${s}->content_have didn't return regex but ", dump( $regex ), " ref ", ref( $regex )
179                                    unless ( ref($regex) eq 'Regexp' );
180                            if ( $content =~ $regex ) {
181                                    $self->log->debug("${s}->content_have succesful");
182                                    return $s;
183                            }
184                    }
185            }
186    }
187    
188    =head2 scrape
189    
190    Create semi-complex L<WWW::Mechanize> rules to scrape page
191    
192    
193    =cut
194    
195    sub scrape {
196            my $self = shift;
197    
198            my $args = {@_};
199    
200            $self->log->debug("scrape with args ",dump($args));
201    
202            my ($feed,$uri,$q) = ($self->feed, $self->uri,$self->q);
203            die "no uri" unless ($uri);
204            die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');
205    
206            sub mech_warn {
207                    my $m = shift || return;
208                    warn $m;
209            }
210    
211            my $mech = WWW::Mechanize->new(
212                    cookie_jar => {},
213                    onwarn => \&mech_warn,
214                    onerror => \&mech_warn,
215            );
216    
217            $mech->get( $uri );
218    
219            $self->save( 'get.html', $mech->content );
220    
221            if ( my $form = $args->{submit_form} ) {
222                    $self->log->debug("submit form on $uri with ", dump( $form ));
223                    $mech->submit_form( %$form ) or die "can't submit form ", dump( $form );
224                    $self->save( 'submit.html', $mech->content );
225            }
226    
227            $self->log->debug("parse result page");
228    
229            my $tree = HTML::TreeBuilder->new or die "can't create html tree";
230            $tree->parse( $mech->content ) or die "can't parse fetched content";
231    
232            die "wrapper doesn't have 3 elements but ", $#{ $args->{wrapper} } unless ( $#{ $args->{wrapper} } == 2 );
233            my ( $el,$attr,$value ) = @{ $args->{wrapper} };
234    
235            $self->log->debug("looking for <$el $attr=\"$value\">");
236    
237            my $div = $tree->look_down( '_tag', $el, sub {
238                            ( $_[0]->attr( $attr ) || '' ) eq $value;
239            });
240    
241            if ( ! $div ) {
242                    warn "can't find results wrapper <$el $attr=\"$value\">";
243                    return;
244            }
245    
246            my $max = 15;
247            my $nr = 1;
248    
249            my $base_uri = $uri;
250            $base_uri =~ s!\?.*$!!;
251    
252            foreach my $dt ( $div->look_down( '_tag', $args->{results} ) ) {
253                    my $a = $dt->look_down( '_tag', 'a', sub { $_[0]->attr('href') } );
254                    if ( $a ) {
255    
256                            my $href = $a->attr('href') or die "can't find href inside <", $args->{results}, ">";
257                            my $page_uri = URI->new_abs( $a->attr('href'), $base_uri );
258                            $page_uri->query( undef );
259                            $page_uri = $page_uri->canonical;
260    
261                            $self->log->debug("fetching page: ",$a->as_text," from $page_uri");
262                            if ( $mech->follow_link( url => $a->attr('href') ) ) {
263    
264                                    $self->save( "page-${nr}.html", $mech->content );
265    
266                                    my $page_tree = HTML::TreeBuilder->new or die "can't create page tree";
267                                    $page_tree->parse( $mech->content ) or die "can't parse page at $page_uri";
268    
269                                    ( $el,$attr,$value ) = @{ $args->{scrape} };
270                                    $div = $page_tree->look_down( '_tag', $el, sub { ( $_[0]->attr( $attr ) || '' ) eq $value } );
271    
272                                    die "can't find <$el $attr=\"$value\">" unless ($div);
273    
274                                    $self->add_record(
275                                            in_feed => $feed,
276                                            title => $mech->title,
277                                            link => $page_uri,
278                                            content => $div->as_HTML,
279    #                                       summary =>
280    #                                       category =>
281    #                                       author =>
282    #                                       issued =>
283    #                                       modified =>
284                                    );
285    
286                                    $mech->back;
287                                    $page_tree->delete;
288    
289                            } else {
290                                    warn "can't follow uri $page_uri: $!\n";
291                            }
292                  }                  }
293    
294                    last if ($nr == $max);
295                    $nr++;
296            }
297    
298            $tree->delete; # clear memory!
299    
300    }
301    
302    =head2 save
303    
304      save( 'name', $content );
305    
306    Save dumps into C</tmp/grep> if writable
307    
308    =cut
309    
310    sub save {
311            my $self = shift;
312            my ( $file, $content ) = @_;
313            return unless ( defined($file) && defined($content) );
314            if ( -w '/tmp/grep' ) {
315                    open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!";
316                    print $f $content or die "can't write to $file: $!";
317                    close $f or die "can't close $file: $!";
318                    $self->log->debug("saved $file ",length($content)," bytes");
319          }          }
320  }  }
321    

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

  ViewVC Help
Powered by ViewVC 1.1.26