| | 19 | } |
|---|
| | 20 | |
|---|
| | 21 | sub init { |
|---|
| | 22 | my $self = shift; |
|---|
| | 23 | $self->SUPER::init(@_); |
|---|
| | 24 | $self->load_plugins(); |
|---|
| | 25 | |
|---|
| | 26 | $self->{ua} = Plagger::UserAgent->new; |
|---|
| | 27 | } |
|---|
| | 28 | |
|---|
| | 29 | sub 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 | |
|---|
| | 41 | sub 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 | |
|---|
| | 50 | sub 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; |
|---|
| | 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 | } |
|---|