Plagger::Plugin::Filter::RewriteEnclosureURL - Rewrite enclosure URL for republishing


Plagger documentation Contained in the Plagger distribution.

Index


Code Index:

NAME

Top

Plagger::Plugin::Filter::RewriteEnclosureURL - Rewrite enclosure URL for republishing

SYNOPSIS

Top

  - module: Filter::FetchEnclosure
    config:
      dir: /home/miyagawa/public_html

  - module: Filter::RewriteEnclosureURL
    config:
      rewrite:
        - local: /home/miyagawa/public_html/
          url:   http://rock/~miyagawa/

DESCRIPTION

Top

This plugin rewrites enclosure URL using rewrite rule.

CONFIG

Top

rewrite

List of hash that defines rule to rewrite URL. local to represent local path, which should match with enclosure's local_path and url to represent publicly accessible HTTP base URL.

In this example, /home/miyagawa/public_html/foo.mp3 is rewritten to http://rock/~miyagawa/foo.mp3.

AUTHOR

Top

Tatsuhiko Miyagawa

SEE ALSO

Top

Plagger


Plagger documentation Contained in the Plagger distribution.

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

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

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

    $self->conf->{rewrite} or Plagger->context->error("config 'rewrite' is not set.");
    $self->conf->{rewrite} = [ $self->conf->{rewrite} ] unless ref $self->conf->{rewrite};
}

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

    for my $enclosure ($args->{entry}->enclosures) {
        my $local_path = $enclosure->local_path;
        unless ($local_path) {
            $context->log(warn => "\$enclosure->local_path is not set. You need to load Filter::FetchEnclosure to use this plugin.");
            return;
        }

        for my $rewrite (@{ $self->conf->{rewrite} }) {
            if ($local_path =~ s/^$rewrite->{local}//) {
                $enclosure->url( $rewrite->{url} . $local_path );
                $context->log(info => "enclosure URL set to " . $enclosure->url);
                last;
            }
        }
    }
}

1;

__END__