Changeset 117

Show
Ignore:
Timestamp:
05/19/06 17:22:27 (2 years ago)
Author:
miya
Message:

Support plagger.org Ticket#236 (only for .pl)

Files:

Legend:

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

    r116 r117  
    44 
    55use HTML::TokeParser; 
    6 use Plagger::Util
     6use Plagger::Util qw( decode_content )
    77use URI; 
     8use DirHandle; 
     9use Plagger::UserAgent; 
    810 
    911sub register { 
     
    1517        'update.entry.fixup' => \&filter, 
    1618    ); 
     19} 
     20 
     21sub init { 
     22    my $self = shift; 
     23    $self->SUPER::init(@_); 
     24    $self->load_plugins(); 
     25 
     26    $self->{ua} = Plagger::UserAgent->new; 
     27} 
     28 
     29sub load_plugins { 
     30    my $self = shift; 
     31    my $context = Plagger->context; 
     32 
     33    my $dir = $self->assets_dir; 
     34    my $dh = DirHandle->new($dir) or $context->error("$dir: $!"); 
     35    for my $file (grep -f $_->[0] && $_->[0] =~ /\.(?:pl|yaml)$/, 
     36                  map [ File::Spec->catfile($dir, $_), $_ ], sort $dh->read) { 
     37        $self->load_plugin(@$file); 
     38    } 
     39} 
     40 
     41sub load_plugin { 
     42    my($self, $file, $base) = @_; 
     43 
     44    Plagger->context->log(debug => "loading $file"); 
     45 
     46    my $load_method = $file =~ /\.pl$/ ? 'load_plugin_perl' : 'load_plugin_yaml'; 
     47    push @{ $self->{plugins} }, $self->$load_method($file, $base); 
     48} 
     49 
     50sub load_plugin_perl { 
     51    my($self, $file, $base) = @_; 
     52 
     53    open my $fh, $file or Plagger->context->error("$file: $!"); 
     54    (my $pkg = $base) =~ s/\.pl$//; 
     55    my $plugin_class = "Plagger::Plugin::Filter::FindEnclosures::Site::$pkg"; 
     56 
     57    my $code = join '', <$fh>; 
     58    unless ($code =~ /^\s*package/s) { 
     59        $code = join "\n", 
     60            ( "package $plugin_class;", 
     61              "use strict;", 
     62              "use base qw( Plagger::Plugin::Filter::FindEnclosures::Site );", 
     63              "sub site_name { '$pkg' }", 
     64              $code, 
     65              "1;" ); 
     66    } 
     67 
     68    eval $code; 
     69    Plagger->context->error($@) if $@; 
     70 
     71    return $plugin_class->new; 
    1772} 
    1873 
     
    45100        $enclosure->is_inline($inline); 
    46101        $entry->add_enclosure($enclosure); 
     102        return; 
     103    } 
     104 
     105    my $url = $tag->[1]{$attr}; 
     106    my $res; 
     107    eval { 
     108        $res = $self->{ua}->fetch( $url, $self ); 
     109    }; 
     110    return if (!$@ and $res->http_response->is_error); 
     111 
     112    my $content = decode_content($res); 
     113 
     114    for my $plugin ( @{$self->{plugins}} ){ 
     115        if( $plugin->handle($url) ){ 
     116            my $enclosure = $plugin->find($content); 
     117            if($enclosure){ 
     118                Plagger->context->log(info => "Found enclosure $url"); 
     119                $entry->add_enclosure($enclosure); 
     120            } 
     121        } 
    47122    } 
    48123} 
     
    63138    $mime && $mime->mediaType =~ m!^(?:audio|video|image)$!; 
    64139} 
     140 
     141package Plagger::Plugin::Filter::FindEnclosures::Site; 
     142sub new { bless {}, shift } 
     143sub custom_feed_handle { 0 } 
     144sub custom_feed_follow_link { } 
     145sub handle_force { 0 } 
     146sub handle { 0 } 
    65147 
    661481;