概要: なんか色々やってるうちにローカルにある Feed を購読するプラグインができた。
ローカルにある Feed 自体は Subscription::Config でfile://
から始まるパスを指定すればできるんだけど
(で知った)、大量の Feed があるときに、
一ファイルずつパスを書くのは面倒なので、その手間を省略するために使える。ような気がする。
以下プラグインのコード。
package Plagger::Plugin::Subscription::LocalFeed; use strict; use warnings; use base qw(Plagger::Plugin); our $VERSION = '0.1'; use Path::Class::Dir; use File::Spec; use File::Find; sub register { my ( $self, $c ) = @_; $c->register_hook( $self, 'subscription.load' => \&load, ); } sub load { my ( $self, $c ) = @_; for my $config ( @{ $self->conf->{'dir'} } ) { if ( ! ref($config) ) { $config = { dir => $config }; } my $depth = $config->{'depth'} || 0; $depth = 0 if ( $depth < 0 ); my $dir = $config->{'path'} or $c->log( error => 'Feed directory is missing.' ) and next; unless ( -d $dir ) { $c->log( error => "$dir is not directory." ); next; } find(sub { my $curr_dir = $File::Find::dir; my $curr_dep = $curr_dir =~ tr{/}{}; return if ( $depth && $curr_dep > $depth ); my $name = $File::Find::name; return if ( -d $name ); return unless ( -r $name ); my $file = Path::Class::Dir->new( File::Spec->rel2abs($name) )->as_foreign('Unix'); my $path = 'file://' . $file; my $feed = Plagger::Feed->new; $feed->url( $path ); $c->subscription->add( $feed ); }, $dir); } } 1; __END__ =head1 NAME Plagger::Plugin::Subscription::LocalFeed - Subscribe entries in local feeds. =head1 SYNOPSIS - module: Subscription::LocalFeed config: dir: - path: /path/to/local/directory depth: 0 =head1 DESCRIPTION This plug-in subscribes to entries in local feeds. =head1 CONFIGURATION =over 2 =item path Directory with Feed. =item depth Depth in which subdirectory is traced When it is 0, everything is traced. =back =head1 AUTHOR Nyarla, E<lt>thotep@nyarla.netE<gt> =head1 COPYRIGHT AND LICENSE Copyright (c) "Nyarla," All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut
確認した限りではたぶん問題なく動く。と言ってもあんまり自信が無いんだけど。 あと POD の英文が非常に怪しい。(ほとんど機械翻訳そのまま)
Plagger のプラグインを書いたのは初めてなので、 何か問題があればコメントか何かで教えてください。
ちなみに RSS や Atom で無いものを購読するときには CustomFeed::Config と組み合わせればいいかも。 ただ、ディレクトリを移動させたりする(URI が変わる)と asset を書き直すことになるので、 そのあたりの手間を何とかできたらいいんだけど。
書いた割には使い道が思いつきません。