2007-10-25
Flickr の feed の画像を upgrade する Filter::FindEnclosures
なんか、フィードに普通に元画像へのリンクあるので、基本的にいらない子みたいです。><
あえて使う場面があるとすれば、画像の大きさを label で指定して、必要な大きさの画像を取る場合のみです。><
woremacx flickr の API 使って、feed の画像を original 画像に upgrade する filter 書くと幸せになれそうな気がしてきた。
http://twitter.com/woremacx/statuses/360564352
otsune @woremacx TruePermalinkかFindEnclosureあたりで出来そう lang:ja
http://twitter.com/otsune/statuses/360602382
woremacx @otsune FindEnclosure で検討してみます!
http://twitter.com/woremacx/statuses/360651142
ということで、作りました。
以下に貼り付けた内容を http://pub.woremacx.com/.plagger/flickr/ に置いておきます。
apikey を呼んだりするために、モジュールにも手を入れています。
assets/plugins/Filter-FindEnclosures/flickr.pl
# author: woremacx sub init { my $self = shift; my $apikey; unless ($apikey = $self->config->{apikey}) { Plagger->context->log(error => "no apikey set"); return; } require Flickr::API::Photos; $self->{api} = Flickr::API::Photos->new($apikey); } sub handle { my ($self, $url) = @_; return 0 unless $self->{api}; $url =~ m!http://www.flickr.com/photos/(?:[^/]+)/\d+/!; } sub needs_content { 0 } sub find { my ($self, $args) = @_; my $url = $args->{url}; my ($id) = ($url =~ m!http://www.flickr.com/photos/(?:[^/]+)/(\d+)/!); my $res = $self->{api}->getSizes($id); unless ($res->{success}) { Plagger->context->log(error => "error returned from flickr API for $url"); return; } if (my $label = $self->config->{label}) { for my $photo (@{ $res->{sizes} }) { return $self->image($id, $photo) if $label eq $photo->{label}; } } my @sizes_reversed = reverse(@{ $res->{sizes} }); for my $attrib (qw(height width)) { my $conf = $self->config->{"max" . $attrib}; next unless $conf; SIZE: for my $photo (@sizes_reversed) { next SIZE if $photo->{attrib} > $conf; return $self->image($id, $photo); } } return $self->image($id, $res->{sizes}->[-1]); } sub image { my ($self, $id, $photo) = @_; my $enclo = Plagger::Enclosure->new; $enclo->url($photo->{source}); $enclo->type('image/jpeg'); $enclo->filename(sprintf("%d_%s.jpg", $id, $photo->{label})); return $enclo; }
lib/Plagger/Plugin/Filter/FindEnclosures.pm
================================================================== --- lib/Plagger/Plugin/Filter/FindEnclosures.pm (revision 15) +++ lib/Plagger/Plugin/Filter/FindEnclosures.pm (local) @@ -70,7 +70,13 @@ eval $code; Plagger->context->error($@) if $@; - return $plugin_class->new; + my $plugin = $plugin_class->new; + + my $config = $self->conf->{$pkg}; + $plugin->set_config($config) if $config; + $plugin->init; + + $plugin; } sub load_plugin_yaml { Plagger->context->error("NOT IMPLEMENTED YET") } @@ -190,6 +196,9 @@ package Plagger::Plugin::Filter::FindEnclosures::Site; sub new { bless {}, shift } +sub init { } +sub config { $_[0]->{config} } +sub set_config { $_[0]->{config} = $_[1] } sub handle { 0 } sub find { } sub needs_content { 1 }
トラックバック - http://d.hatena.ne.jp/woremacx/20071025/1193252168
