| 1 |
package Plagger::Plugin::Publish::PodCast; |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use base qw( Plagger::Plugin ); |
|---|
| 5 |
|
|---|
| 6 |
use XML::RSS; |
|---|
| 7 |
use File::Spec; |
|---|
| 8 |
|
|---|
| 9 |
sub register { |
|---|
| 10 |
my($self, $context) = @_; |
|---|
| 11 |
$context->register_hook( |
|---|
| 12 |
$self, |
|---|
| 13 |
'publish.feed' => \&publish_feed, |
|---|
| 14 |
); |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
sub publish_feed { |
|---|
| 18 |
my($self, $context, $args) = @_; |
|---|
| 19 |
|
|---|
| 20 |
my $f = $args->{feed}; |
|---|
| 21 |
my $rss = XML::RSS->new( version => '2.0' ); |
|---|
| 22 |
$rss->channel( |
|---|
| 23 |
title => $f->title, |
|---|
| 24 |
link => $f->link, |
|---|
| 25 |
description => $f->description, |
|---|
| 26 |
); |
|---|
| 27 |
|
|---|
| 28 |
for my $e ($f->entries) { |
|---|
| 29 |
$rss->add_item( |
|---|
| 30 |
title => $e->title, |
|---|
| 31 |
enclosure => { |
|---|
| 32 |
url => $e->enclosure->url, |
|---|
| 33 |
type => $e->enclosure->type, |
|---|
| 34 |
}, |
|---|
| 35 |
); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
my $filepath = File::Spec->catfile($self->conf->{dir}, $self->gen_filename($f)); |
|---|
| 40 |
|
|---|
| 41 |
$context->log(info => "save feed for " . $f->url . " to $filepath"); |
|---|
| 42 |
|
|---|
| 43 |
my $xml = $rss->as_string; |
|---|
| 44 |
utf8::decode($xml) unless utf8::is_utf8($xml); |
|---|
| 45 |
open my $output, ">:utf8", $filepath or $context->error("$filepath: $!"); |
|---|
| 46 |
print $output $xml; |
|---|
| 47 |
close $output; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
my %formats = ( |
|---|
| 51 |
'u' => sub { my $s = $_[0]->url; $s =~ s!^https?://!!; $s }, |
|---|
| 52 |
'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s }, |
|---|
| 53 |
't' => sub { $_[0]->title }, |
|---|
| 54 |
'i' => sub { $_[0]->id }, |
|---|
| 55 |
); |
|---|
| 56 |
|
|---|
| 57 |
my $format_re = qr/%(u|l|t|i)/; |
|---|
| 58 |
|
|---|
| 59 |
sub gen_filename { |
|---|
| 60 |
my($self, $feed) = @_; |
|---|
| 61 |
|
|---|
| 62 |
my $file = $self->conf->{filename} || |
|---|
| 63 |
'%i.' . ($self->conf->{format} eq 'RSS' ? 'rss' : 'atom'); |
|---|
| 64 |
$file =~ s{$format_re}{ |
|---|
| 65 |
$self->safe_filename($formats{$1}->($feed)) |
|---|
| 66 |
}egx; |
|---|
| 67 |
$file; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
sub safe_filename { |
|---|
| 71 |
my($self, $path) = @_; |
|---|
| 72 |
$path =~ s![^\w\s]+!_!g; |
|---|
| 73 |
$path =~ s!\s+!_!g; |
|---|
| 74 |
$path; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
1; |
|---|