Changeset 758

Show
Ignore:
Timestamp:
05/17/06 02:50:06
Author:
miyagawa
Message:
  • Support pheedo.jp ad pattern. Fixes #243
  • Add .yaml based Ad pattern file. condition: *, strip: 1 to strip entry. Fixes #242
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin/Filter/StripRSSAd.pm

    r604 r758  
    1717    my $dir = $self->assets_dir; 
    1818    my $dh = DirHandle->new($dir) or Plagger->context->error("$dir: $!"); 
    19     for my $file (grep -f $_->[0] && $_->[1] =~ /^[\w\-]+$/, 
     19    for my $file (grep -f $_->[0] && $_->[1] =~ /^[\w\-\.]+$/, 
    2020                  map [ File::Spec->catfile($dir, $_), $_ ], sort $dh->read) { 
    2121        $self->load_pattern(@$file); 
     
    2828    Plagger->context->log(debug => "loading $file"); 
    2929 
     30    if ($file =~ /\.yaml$/) { 
     31        $self->load_yaml($file, $base); 
     32    } else { 
     33        $self->load_regexp($file, $base); 
     34    } 
     35} 
     36 
     37sub load_regexp { 
     38    my($self, $file, $base) = @_; 
     39 
    3040    open my $fh, $file or Plagger->context->error("$file: $!"); 
    3141    my $re = join '', <$fh>; 
     
    3343 
    3444    push @{$self->{pattern}}, { site => $base, re => qr/$re/ }; 
     45} 
     46 
     47sub load_yaml { 
     48    my($self, $file, $base) = @_; 
     49 
     50    my $pattern = eval { YAML::LoadFile($file) } 
     51        or Plagger->context->error("$file: $@"); 
     52 
     53    push @{$self->{pattern}}, { site => $base, %$pattern }; 
    3554} 
    3655 
     
    4564sub update { 
    4665    my($self, $context, $args) = @_; 
    47     my $body = $self->filter($args->{entry}->body, $args->{entry}->link); 
    48     $args->{entry}->body($body); 
    49 
    50  
    51 sub filter { 
    52     my($self, $body, $link) = @_; 
     66    my $body = $args->{entry}->body; 
    5367 
    5468    for my $pattern (@{ $self->{pattern} }) { 
    55         my $re = $pattern->{re}; 
    56         if (my $count = $body =~ s!$re!defined($1) ? $1 : ''!egs) { 
    57             Plagger->context->log(debug => "Stripped $pattern->{site} Ad on $link"); 
     69        if (my $re = $pattern->{re}) { 
     70            if (my $count = $body =~ s!$re!defined($1) ? $1 : ''!egs) { 
     71                Plagger->context->log(info => "Stripped $pattern->{site} Ad on " . $args->{entry}->link); 
     72            } 
     73        } elsif (my $cond = $pattern->{condition}) { 
     74            local $args->{body} = $body; 
     75            if (eval $cond && $pattern->{strip}) { 
     76                $args->{feed}->delete_entry($args->{entry}); 
     77                Plagger->context->log(info => "Stripped Ad entry " . $args->{entry}->link); 
     78            } elsif ($@) { 
     79                Plagger->context->log(error => "Error evaluating $cond: $@"); 
     80            } 
    5881        } 
    5982    } 
    6083 
    61     $body
     84    $args->{entry}->body($body)
    6285} 
    6386