| 1 | package Plagger::Plugin::Filter::FetchEnclosure; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Cwd; | ||
| 6 | use File::Spec; | ||
| 7 | use File::Path; | ||
| 8 | |||
| 9 | sub register { | ||
| 10 | my($self, $context) = @_; | ||
| 11 | $context->register_hook( | ||
| 12 | $self, | ||
| 13 | 'update.entry.fixup' => \&filter, | ||
| 14 | ); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $self = shift; | ||
| 19 | $self->SUPER::init(@_); | ||
| 20 | |||
| 21 | defined $self->conf->{dir} or Plagger->context->error("config 'dir' is not set."); | ||
| 22 | # XXX make it Plagger::Util function | ||
| 23 | if ($self->conf->{dir} =~ /^[a-zA-Z]/ && $self->conf->{dir} !~ /:/) { | ||
| 24 | $self->conf->{dir} = File::Spec->catfile( Cwd::cwd, $self->conf->{dir} ); | ||
| 25 | } | ||
| 26 | |||
| 27 | unless (-e $self->conf->{dir} && -d _) { | ||
| 28 | Plagger->context->log(warn => $self->conf->{dir} . " does not exist. Creating"); | ||
| 29 | mkpath $self->conf->{dir}; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | sub filter { | ||
| 34 | my($self, $context, $args) = @_; | ||
| 35 | |||
| 36 | my $ua = Plagger::UserAgent->new; | ||
| 37 | for my $enclosure ($args->{entry}->enclosures) { | ||
| 38 | my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); | ||
| 39 | unless (-e $feed_dir && -d _) { | ||
| 40 | $context->log(info => "mkdir $feed_dir"); | ||
| 41 | mkdir $feed_dir, 0777; | ||
| 42 | } | ||
| 43 | |||
| 44 | my $filename = $enclosure->filename; | ||
| 45 | Encode::_utf8_off($filename); | ||
| 46 | my $path = File::Spec->catfile($feed_dir, $filename); | ||
| 47 | $context->log(info => "fetch " . $enclosure->url . " to " . $path); | ||
| 48 | |||
| 49 | my $request = HTTP::Request->new(GET => $enclosure->url); | ||
| 50 | if ($self->conf->{fake_referer}) { | ||
| 51 | $context->log(debug => "Sending Referer: " . $args->{entry}->permalink); | ||
| 52 | $request->header('Referer' => $args->{entry}->permalink); | ||
| 53 | } | ||
| 54 | |||
| 55 | my $res = $ua->mirror($request, $path); | ||
| 56 | $enclosure->local_path($path); # set to be used in later plugins | ||
| 57 | |||
| 58 | # Fix length if it's broken | ||
| 59 | if ($res->header('Content-Length')) { | ||
| 60 | $enclosure->length( $res->header('Content-Length') ); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | __END__ |