| 1 |
package Plagger::Plugin::Publish::CSV; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use warnings; |
|---|
| 4 |
use base qw ( Plagger::Plugin ); |
|---|
| 5 |
|
|---|
| 6 |
our $VERSION = 0.02; |
|---|
| 7 |
|
|---|
| 8 |
use Encode; |
|---|
| 9 |
use File::Spec; |
|---|
| 10 |
use Text::CSV_PP; |
|---|
| 11 |
use IO::File; |
|---|
| 12 |
|
|---|
| 13 |
sub register { |
|---|
| 14 |
my ($self, $context) = @_; |
|---|
| 15 |
$context->register_hook( |
|---|
| 16 |
$self, |
|---|
| 17 |
'publish.feed' => \&feed, |
|---|
| 18 |
); |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
sub feed { |
|---|
| 22 |
my ($self, $context, $args) = @_; |
|---|
| 23 |
my $csv = Text::CSV_PP->new({ binary => 1 }); |
|---|
| 24 |
my $append = ($self->conf->{mode} && $self->conf->{mode} eq 'append'); |
|---|
| 25 |
my $dir = $self->conf->{dir}; |
|---|
| 26 |
unless (-e $dir && -d _) { |
|---|
| 27 |
mkdir $dir, 0755 or $context->error("mkdir $dir: $!"); |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
my $file = Plagger::Util::filename_for($args->{feed}, $self->conf->{filename} || "%i.csv"); |
|---|
| 31 |
my $path = File::Spec->catfile($dir, $file); |
|---|
| 32 |
my $io = IO::File->new($append ? ">> $path" : "> $path"); |
|---|
| 33 |
|
|---|
| 34 |
my $columns = $self->conf->{column} || [qw(title permalink)]; |
|---|
| 35 |
for my $entry ($args->{feed}->entries) { |
|---|
| 36 |
my $st = $csv->combine(map { $entry->$_ } @$columns); |
|---|
| 37 |
$context->log(error => $self->convert($csv->error_input)) unless $st; |
|---|
| 38 |
$io->printf("%s\n", $self->convert($csv->string)) if $st; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
$context->log( |
|---|
| 42 |
info => sprintf( |
|---|
| 43 |
"%s to %s: %d entries", |
|---|
| 44 |
$append ? 'Append' : 'Write', |
|---|
| 45 |
$path, |
|---|
| 46 |
$args->{feed}->count |
|---|
| 47 |
) |
|---|
| 48 |
); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
sub convert { |
|---|
| 52 |
my ($self, $str) = @_; |
|---|
| 53 |
utf8::decode($str) unless utf8::is_utf8($str); |
|---|
| 54 |
return encode($self->conf->{encoding} || 'utf8', $str); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
1; |
|---|
| 58 |
|
|---|
| 59 |
__END__ |
|---|
| 60 |
|
|---|
| 61 |
=head1 |
|---|
| 62 |
|
|---|
| 63 |
Plagger::Plugin::Publish::CSV - Publish feeds as CSV |
|---|
| 64 |
|
|---|
| 65 |
=head1 SYNOPSIS |
|---|
| 66 |
|
|---|
| 67 |
- module: Publish::CSV |
|---|
| 68 |
config: |
|---|
| 69 |
dir: /var/web/csv |
|---|
| 70 |
encoding: euc-jp |
|---|
| 71 |
filename: my_%t.csv |
|---|
| 72 |
mode: append |
|---|
| 73 |
column: |
|---|
| 74 |
- title |
|---|
| 75 |
- permalink |
|---|
| 76 |
|
|---|
| 77 |
=head1 CONFIG |
|---|
| 78 |
|
|---|
| 79 |
=head2 dir |
|---|
| 80 |
|
|---|
| 81 |
Directory to save csv files in. |
|---|
| 82 |
|
|---|
| 83 |
=head2 filename |
|---|
| 84 |
|
|---|
| 85 |
Filename to be used to create csv files. It defaults to C<%i.csv>. It |
|---|
| 86 |
supports the following format like printf(): |
|---|
| 87 |
|
|---|
| 88 |
=over 4 |
|---|
| 89 |
|
|---|
| 90 |
=item * %u url |
|---|
| 91 |
|
|---|
| 92 |
=item * %l link |
|---|
| 93 |
|
|---|
| 94 |
=item * %t title |
|---|
| 95 |
|
|---|
| 96 |
=item * %i id |
|---|
| 97 |
|
|---|
| 98 |
=back |
|---|
| 99 |
|
|---|
| 100 |
=head2 mode |
|---|
| 101 |
|
|---|
| 102 |
Specify 'append' if you want to append entries to an existing file |
|---|
| 103 |
rather than creating a new file. |
|---|
| 104 |
|
|---|
| 105 |
=head2 column |
|---|
| 106 |
|
|---|
| 107 |
Chose the columns of entry which you want to write to the csv. It |
|---|
| 108 |
defaults to title and permalink. |
|---|
| 109 |
|
|---|
| 110 |
=head1 AUTHOR |
|---|
| 111 |
|
|---|
| 112 |
Naoya Ito E<lt>naoya@bloghackers.netE<gt> |
|---|
| 113 |
|
|---|
| 114 |
=head1 SEE ALSO |
|---|
| 115 |
|
|---|
| 116 |
L<Plagger>, L<Text::CSV_PP> |
|---|
| 117 |
|
|---|
| 118 |
=cut |
|---|