| 1 | package Plagger::Plugin::Filter::Thumbnail; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | my($self, $context) = @_; | ||
| 7 | $context->register_hook( | ||
| 8 | $self, | ||
| 9 | 'update.feed.fixup' => \&feed, | ||
| 10 | ); | ||
| 11 | |||
| 12 | if ($self->conf->{set_per_entry}) { | ||
| 13 | $context->register_hook( | ||
| 14 | $self, | ||
| 15 | 'update.entry.fixup' => \&entry, | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | sub feed { | ||
| 21 | my($self, $context, $args) = @_; | ||
| 22 | |||
| 23 | # do nothing if there's already feed logo | ||
| 24 | return if $args->{feed}->image; | ||
| 25 | |||
| 26 | $context->log(info => "Add thumbnail as image to " . $args->{feed}->link); | ||
| 27 | $args->{feed}->image( $self->build_image($args->{feed}->title, $args->{feed}->link) ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub entry { | ||
| 31 | my($self, $context, $args) = @_; | ||
| 32 | |||
| 33 | # do nothing if there's already entry icon | ||
| 34 | return if $args->{entry}->icon; | ||
| 35 | |||
| 36 | $context->log(info => "Add thumbnail as image to " . $args->{entry}->permalink); | ||
| 37 | $args->{entry}->icon( $self->build_image($args->{entry}->title, $args->{entry}->permalink) ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub build_image { | ||
| 41 | my($self, $title, $link) = @_; | ||
| 42 | |||
| 43 | # TODO: use other serivces here | ||
| 44 | return { | ||
| 45 | url => "http://img.simpleapi.net/small/" . $link, | ||
| 46 | title => $title, | ||
| 47 | link => $link, | ||
| 48 | width => 128, | ||
| 49 | height => 128, | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |