これをPaggerで。
config.yaml
plugins:
- module: Subscription::Config
config:
feed: http://localhost/
- module: CustomFeed::AmazonSearch
config:
developer_token: XXXXXXXXXX
keywords:
- げんしけん
- Perl
mode: books-jp
locale: jp
- module: Publish::iCal
config:
dir: /Users/user/Sites/plagger/amazon/
filename: books.icsSubscription::Configはダミー。
CustomFeed::AmazonSearch
Perlの練習に書いてみた。練習と言いつつ色々いい加減。
package Plagger::Plugin::CustomFeed::AmazonSearch;
use strict;
use base qw( Plagger::Plugin );
use Net::Amazon;
use Net::Amazon::Request::Keyword;
sub register {
my($self, $context) = @_;
$context->register_hook(
$self,
'customfeed.handle' => \&aggregate,
);
}
sub aggregate {
my($self, $context, $args) = @_;
my $feed = Plagger::Feed->new;
for my $keyword (@{$self->conf->{keywords}}) {
$self->_search($feed, $context, $keyword);
}
$context->update->add($feed);
}
sub _search {
my($self, $feed, $context, $keyword) = @_;
my $ua = Net::Amazon->new(
token => $self->conf->{developer_token},
locale => $self->conf->{locale},
);
my $req = Net::Amazon::Request::Keyword->new(
keyword => $keyword,
mode => $self->conf->{mode},
sort => "daterank",
);
my $res = $ua->request($req);
if($res->is_error) {
$context->log(error => $res->message);
return;
}
my $dt = Plagger::Date->now;
$dt->subtract(months => 1);
for my $prop ($res->properties) {
my $e = Plagger::Entry->new;
$e->title($prop->ProductName);
$e->body($prop->ProductDescription);
my $date = Plagger::Date->strptime("%Y/%m/%d", $prop->ReleaseDate);
$date = Plagger::Date->strptime("%Y/%m", $prop->ReleaseDate) unless $date;
$e->date($date);
my $d = $date - $dt;
next if $d->is_negative;
$feed->add_entry($e);
}
}
1;設定でカテゴリ(mode)を設定できる風で実はsortの引数なんかが書籍にしか対応していなかったりする。
実行結果
実行するとkeywordsで指定したキーワードで検索した結果がicsファイルとして出力されます。とりあえず、発売日が今日の日付から一ヶ月以上前のものは省くようになっています。発売日が月までしかないこともあるみたいなので、もうちょっと緩くした方がいいのかもしれない(設定できるべきか)。
出力されたicsファイルをiCalなんかで読むと以下のような感じに。
試してないけどGoogle Calenderでも読めるのかな。
ここまで書いて
CustomFeedじゃなくてSubscription::AmazonSearchとして書くべきだったんじゃないかと思った。……後で見直そう。
