Plagger::Plugin::Filter::FetchEnclosure - Fetch enclosure(s) in entry


Plagger documentation Contained in the Plagger distribution.

Index


Code Index:

NAME

Top

Plagger::Plugin::Filter::FetchEnclosure - Fetch enclosure(s) in entry

SYNOPSIS

Top

  - module: Filter::FetchEnclosure
    config:
      dir: /path/to/files

DESCRIPTION

Top

This plugin downloads enclosure files set for each entry.

CONFIG

Top

dir

Directory to store downloaded enclosures. Required.

fake_referer

Flag to set Referer HTTP header when downloading enclosures. Some sites would deny downloading images without Referer header. Defaults to 0.

TODO

Top

Support asynchronous download using POE

AUTHOR

Top

Tatsuhiko Miyagawa

SEE ALSO

Top

Plagger


Plagger documentation Contained in the Plagger distribution.

package Plagger::Plugin::Filter::FetchEnclosure;
use strict;
use base qw( Plagger::Plugin );

use Cwd;
use File::Spec;
use File::Path;

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'update.entry.fixup' => \&filter,
    );
}

sub init {
    my $self = shift;
    $self->SUPER::init(@_);

    defined $self->conf->{dir} or Plagger->context->error("config 'dir' is not set.");
    # XXX make it Plagger::Util function
    if ($self->conf->{dir} =~ /^[a-zA-Z]/ && $self->conf->{dir} !~ /:/) {
        $self->conf->{dir} = File::Spec->catfile( Cwd::cwd, $self->conf->{dir} );
    }
    
    unless (-e $self->conf->{dir} && -d _) {
        Plagger->context->log(warn => $self->conf->{dir} . " does not exist. Creating");
        mkpath $self->conf->{dir};
    }
}

sub filter {
    my($self, $context, $args) = @_;

    my $ua = Plagger::UserAgent->new;
    for my $enclosure ($args->{entry}->enclosures) {
        my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe);
        unless (-e $feed_dir && -d _) {
            $context->log(info => "mkdir $feed_dir");
            mkdir $feed_dir, 0777;
        }

        my $filename = $enclosure->filename;
        Encode::_utf8_off($filename);
        my $path = File::Spec->catfile($feed_dir, $filename);
        $context->log(info => "fetch " . $enclosure->url . " to " . $path);

        my $request = HTTP::Request->new(GET => $enclosure->url);
        if ($self->conf->{fake_referer}) {
            $context->log(debug => "Sending Referer: " . $args->{entry}->permalink);
            $request->header('Referer' => $args->{entry}->permalink);
        }

        my $res = $ua->mirror($request, $path);
        $enclosure->local_path($path); # set to be used in later plugins

        # Fix length if it's broken
        if ($res->header('Content-Length')) {
            $enclosure->length( $res->header('Content-Length') );
        }
    }
}

1;

__END__