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) ); |
12 |
|
|
13 |
use HTML::TreeBuilder; |
use HTML::TreeBuilder; |
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 |
my $parent = $self; |
my $parent = $self; |
112 |
$class->fetch( $parent ); |
$class->fetch( $parent ); |
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? |
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') ) { |
if ( $s->can('content_have') ) { |
176 |
my $regex = $s->content_have( $content ) or |
my $regex = $s->content_have( $content ) or |
177 |
die "${s}->content_have didn't return anything"; |
die "${s}->content_have didn't return anything"; |
178 |
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 ) |
179 |
unless ( ref($regex) eq 'Regexp' ); |
unless ( ref($regex) eq 'Regexp' ); |
180 |
if ( $content =~ $regex ) { |
if ( $content =~ $regex ) { |
181 |
Jifty->log->debug("${s}->content_have succesful"); |
$self->log->debug("${s}->content_have succesful"); |
182 |
return $s; |
return $s; |
183 |
} |
} |
184 |
} |
} |
311 |
sub save { |
sub save { |
312 |
my $self = shift; |
my $self = shift; |
313 |
my ( $file, $content ) = @_; |
my ( $file, $content ) = @_; |
314 |
|
return unless ( defined($file) && defined($content) ); |
315 |
if ( -w '/tmp/grep' ) { |
if ( -w '/tmp/grep' ) { |
316 |
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
open(my $f, '>', "/tmp/grep/$file") or die "can't open $file: $!"; |
317 |
print $f $content or die "can't write to $file: $!"; |
print $f $content or die "can't write to $file: $!"; |
318 |
close $f or die "can't close $file: $!"; |
close $f or die "can't close $file: $!"; |
319 |
Jifty->log->debug("saved $file ",length($content)," bytes"); |
$self->log->debug("saved $file ",length($content)," bytes"); |
320 |
} |
} |
321 |
} |
} |
322 |
|
|