| 1 |
package Plagger::Plugin::Filter::FetchEnclosure; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use File::Spec; |
|---|
| 6 |
use File::Path; |
|---|
| 7 |
|
|---|
| 8 |
sub register { |
|---|
| 9 |
my($self, $context) = @_; |
|---|
| 10 |
$context->register_hook( |
|---|
| 11 |
$self, |
|---|
| 12 |
'update.entry.fixup' => \&filter, |
|---|
| 13 |
); |
|---|
| 14 |
} |
|---|
| 15 |
|
|---|
| 16 |
sub init { |
|---|
| 17 |
my $self = shift; |
|---|
| 18 |
$self->SUPER::init(@_); |
|---|
| 19 |
|
|---|
| 20 |
defined $self->conf->{dir} or Plagger->context->error("config 'dir' is not set."); |
|---|
| 21 |
unless (-e $self->conf->{dir} && -d _) { |
|---|
| 22 |
Plagger->context->log(warn => $self->conf->{dir} . " does not exist. Creating"); |
|---|
| 23 |
mkpath $self->conf->{dir}; |
|---|
| 24 |
} |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
sub filter { |
|---|
| 28 |
my($self, $context, $args) = @_; |
|---|
| 29 |
|
|---|
| 30 |
my $ua = Plagger::UserAgent->new; |
|---|
| 31 |
for my $enclosure ($args->{entry}->enclosures) { |
|---|
| 32 |
my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); |
|---|
| 33 |
unless (-e $feed_dir && -d _) { |
|---|
| 34 |
$context->log(info => "mkdir $feed_dir"); |
|---|
| 35 |
mkdir $feed_dir, 0777; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
my $path = File::Spec->catfile($feed_dir, $enclosure->filename); |
|---|
| 39 |
$context->log(info => "fetch " . $enclosure->url . " to " . $path); |
|---|
| 40 |
|
|---|
| 41 |
my $request = HTTP::Request->new(GET => $enclosure->url); |
|---|
| 42 |
if ($self->conf->{fake_referer}) { |
|---|
| 43 |
$context->log(debug => "Sending Referer: " . $args->{entry}->permalink); |
|---|
| 44 |
$request->header('Referer' => $args->{entry}->permalink); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
my $res = $ua->mirror($request, $path); |
|---|
| 48 |
$enclosure->local_path($path); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
if ($res->header('Content-Length')) { |
|---|
| 52 |
$enclosure->length( $res->header('Content-Length') ); |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
1; |
|---|
| 58 |
|
|---|
| 59 |
__END__ |
|---|
| 60 |
|
|---|
| 61 |
=head1 NAME |
|---|
| 62 |
|
|---|
| 63 |
Plagger::Plugin::Filter::FetchEnclosure - Fetch enclosure(s) in entry |
|---|
| 64 |
|
|---|
| 65 |
=head1 SYNOPSIS |
|---|
| 66 |
|
|---|
| 67 |
- module: Filter::FetchEnclosure |
|---|
| 68 |
config: |
|---|
| 69 |
dir: /path/to/files |
|---|
| 70 |
|
|---|
| 71 |
=head1 DESCRIPTION |
|---|
| 72 |
|
|---|
| 73 |
This plugin downloads enclosure files set for each entry. |
|---|
| 74 |
|
|---|
| 75 |
=head1 CONFIG |
|---|
| 76 |
|
|---|
| 77 |
=over 4 |
|---|
| 78 |
|
|---|
| 79 |
=item dir |
|---|
| 80 |
|
|---|
| 81 |
Directory to store downloaded enclosures. Required. |
|---|
| 82 |
|
|---|
| 83 |
=item fake_referer |
|---|
| 84 |
|
|---|
| 85 |
Flag to set I<Referer> HTTP header when downloading enclosures. Some |
|---|
| 86 |
sites would deny downloading images without Referer header. Defaults |
|---|
| 87 |
to 0. |
|---|
| 88 |
|
|---|
| 89 |
=back |
|---|
| 90 |
|
|---|
| 91 |
=head1 TODO |
|---|
| 92 |
|
|---|
| 93 |
=over 4 |
|---|
| 94 |
|
|---|
| 95 |
=item Support asynchronous download using POE |
|---|
| 96 |
|
|---|
| 97 |
=back |
|---|
| 98 |
|
|---|
| 99 |
=head1 AUTHOR |
|---|
| 100 |
|
|---|
| 101 |
Tatsuhiko Miyagawa |
|---|
| 102 |
|
|---|
| 103 |
=head1 SEE ALSO |
|---|
| 104 |
|
|---|
| 105 |
L<Plagger> |
|---|
| 106 |
|
|---|
| 107 |
=cut |
|---|
| 108 |
|
|---|