root/trunk/plagger/lib/Plagger/Plugin/Filter/Thumbnail.pm

Revision 1369 (checked in by miyagawa, 14 years ago)

Filter::Thumbnail: Added set_per_entry config which defaults to 0. Added unit test and fixed bug (YAY)

Line 
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__
56
57 =head1 NAME
58
59 Plagger::Plugin::Filter::Thumbnail - use Website thumbnail tool(s) to create alternative image for feeds
60
61 =head1 SYNOPSIS
62
63   - module: Filter::Thumbnail
64
65 =head1 DESCRIPTION
66
67 This plugin puts image link to website thumbnail tool when a feed
68 doesn't have proper image set in feed itself (ala rss:image or atom:logo).
69
70 For now, it uses L<http://img.simpleapi.net/> as a default (and only)
71 URL to use with, but it should be configured when there's similar
72 (free) serviced out there.
73
74 =head1 CONFIG
75
76 =over 4
77
78 =item set_per_entry
79
80 With I<set_per_entry> set, it adds each entry thumbnail as entry's
81 icon, in addition to the feed logo. Optional and defaults to 0.
82
83 =back
84
85 =head1 AUTHOR
86
87 Tatsuhiko Miyagawa
88
89 =head1 SEE ALSO
90
91 L<Plagger>
92
93 =cut
Note: See TracBrowser for help on using the browser.