続々とPlaggerのPlugin晒し。 Entryのlinkが楽天商品へのものだった場合に、楽天ウェブサービスを使って情報を取得して、 Entryのbodyの中に入れたりするPlugin。 ってかたぶん俺しか使わないと思うよ。
package Plagger::Plugin::Filter::RakutenWebService;
use strict;
use base qw( Plagger::Plugin );
use WebService::Rakuten;
use HTML::TokeParser;
use Encode;
use URI;
our $VERSION = '0.01';
sub register {
my($self, $context) = @_;
defined $self->conf->{developer_id} or
Plagger->context->error("config 'developer_id' is not set.");
$self->{ua} = Plagger::UserAgent->new;
$context->register_hook(
$self,
'update.entry.fixup' => \&filter,
);
}
sub filter {
my($self, $context, $args) = @_;
my $entry = $args->{entry};
my ($shop_code, $keyword,$title);
$shop_code = $1 if $entry->link =~ m!item.rakuten.co.jp/(.*?)/!;
return unless $shop_code;
# can't regex 'keyword' with exactness by Util::load_uri and extract_title
# i don't know why ...
my $file = $self->cache->path_to('rakuten_search_result.html');
my $res = $self->{ua}->fetch($entry->link);
if($res->is_error){
$context->log( error => $res->status );
return;
}
my $content = $res->content;
my $p = HTML::TokeParser->new(\$content);
$p->get_tag("title");
$title = $p->get_trimmed_text;
Encode::from_to($title,"euc-jp","utf-8");
$keyword = $1 if $title =~ m/【楽天市場】(.*?):.*/;
my $api = WebService::Rakuten->new(
dev_id => $self->conf->{developer_id},
aff_id => $self->conf->{affiliate_id},
);
my $res = $api->item_search(
$keyword,
{
shopCode => $shop_code,
hits => 1,
page => 1,
}
);
if ($res->status eq 'Success'){
$context->log(info=> "success : " . $entry->link );
my $item = @{$res->items}[0];
$entry->title($item->itemName);
$entry->body($item->itemCaption);
$entry->icon({
url => $item->mediumImageUrl,
});
$entry->link($item->affiliateUrl) if $self->conf->{set_url};
$entry->meta->{affiliate_url} = $item->affiliateUrl;
$entry->meta->{icon_url} = $item->mediumImageUrl;
}else{
$context->log(info=> "fail : " . $entry->link );
$entry->title($keyword);
}
}
1;
__END__
=head1 NAME
Plagger::Plugin::Filter::RakutenWebService - Set information to entries from Rakuten Web Service
=head1 SYNOPSIS
- module: Filter::RakutenWebService
config:
developer_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
affiliate_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
set_url : 1 # default is off
=head1 DESCRIPTION
This plugin fetches related data from Rakuten Web Service and
sets summary to the entry if entry link is rakuten product URL.
=head1 AUTHOR
Yusuke Wada
=head1 SEE ALSO
L<Plagger>
=cut

