File Coverage

blib/lib/WebService/Bluga/Webthumb.pm
Criterion Covered Total %
statement 24 66 36.3
branch 0 30 0.0
condition 0 11 0.0
subroutine 8 13 61.5
pod 3 3 100.0
total 35 123 28.4


line stmt bran cond sub pod time code
1             package WebService::Bluga::Webthumb;
2              
3 1     1   20568 use warnings;
  1         3  
  1         31  
4 1     1   6 use strict;
  1         1  
  1         33  
5 1     1   5 use Carp;
  1         6  
  1         75  
6 1     1   7 use Digest::MD5;
  1         2  
  1         41  
7 1     1   847 use LWP::Simple;
  1         77979  
  1         10  
8 1     1   474 use URI;
  1         3  
  1         21  
9 1     1   1031 use Path::Class;
  1         51007  
  1         70  
10 1     1   1410 use POSIX qw(strftime);
  1         5276  
  1         7  
11              
12             =head1 NAME
13              
14             WebService::Bluga::Webthumb - fetch website thumbnails via webthumb.bluga.net
15              
16             =cut
17              
18             our $VERSION = '0.05';
19              
20             =head1 SYNOPSIS
21              
22             use WebService::Bluga::Webthumb;
23             my $wt = WebService::Bluga::Webthumb->new(
24             user => $user_id,
25             api_key => $api_key,
26             size => $size, # small, medium, medium2, large (default: medium)
27             cache => $cache_days, # optional - default 14
28            
29             # optional settings for local caching:
30             cache_dir => '....',
31             cache_url_stub => '/images/thumbs/',
32             );
33              
34             # get a thumbnail URL using the default settings
35             my $thumb_url = wt->thumb_url($url);
36              
37             # Get a thumbnail URL overriding some settings:
38             my $thumb_url = $wt->thumb_url($url, { size => 'large' });
39              
40              
41              
42             =head1 Class methods
43              
44             =over 4
45              
46             =item new
47              
48             Create a new WebService::Bluga::Webthumb object. Takes the following params:
49              
50             =over 4
51              
52             =item user
53              
54             Your webthumb user ID, available from your L
55             page.
56              
57             =item api_key
58              
59             Your webthumb API key. also available from your user page. (This is used to
60             construct the hash of the thumbnail URL, but not sent directly.)
61              
62             =item size
63              
64             The size of the thumbnail to generate. Size can be:
65              
66             =over 4
67              
68             =item * small - 80x60
69              
70             =item * medium - 160x120
71              
72             =item * medium2 - 320x240
73              
74             =item * large - 640x480
75              
76             =back
77              
78              
79             =item cache
80              
81             How many days a generated thumbnail can be cached on the webthumb servers before
82             a fresh one is generated. Generating a thumbnail uses a credit whereas serving
83             up a cached one uses a fraction of a credit, so don't set this too low.
84              
85             If not specified, defaults to 14 days.
86              
87             =item cache_dir
88              
89             If set, generated thumbnails will be saved into this directory, and the URL
90             returned will be constructed using C (so the C
91             setting should be set to the URL at which the contents of C are
92             available).
93              
94             The age of the cached thumbnail will be compared against the C setting,
95             and if it's too old, the cached thumbnail will be replaced with a fresh one.
96              
97             =back
98              
99             =cut
100              
101             sub new {
102 0     0 1   my $class = shift;
103 0 0         if (@_ % 2 != 0) {
104 0           croak "Uneven number of parameters provided";
105             }
106              
107 0           my %params = @_;
108            
109             # TODO: more extensive validation
110 0 0 0       if (!$params{user} || !$params{api_key}) {
111 0           croak "'user' and 'api_key' params must be provided";
112             }
113              
114 0 0 0       if (exists $params{size}
  0 0          
115             && !grep { $params{size} eq $_ } qw(small medium medium2 large)
116             ) {
117 0           croak "Invalid size $params{size} supplied!";
118             } elsif (!exists $params{size}) {
119 0           $params{size} = 'medium';
120             }
121              
122 0 0         if (!exists $params{cache}) {
123 0           $params{cache} = 14;
124             }
125              
126 0           my $self = \%params;
127 0           bless $self => $class;
128 0           return $self;
129             }
130              
131             =back
132              
133             =head1 Instance methods
134              
135             =over 4
136              
137             =item thumb_url
138              
139             Given an URL, and optionally C / C params to override those from
140             the object, returns an URL to the thumbnail, to use in an IMG tag.
141              
142             =cut
143              
144             sub thumb_url {
145 0     0 1   my ($self, $url, $params) = @_;
146              
147             # Get our params, use defaults from the object
148 0   0       $params ||= {};
149             $params->{$_} ||= $self->{$_}
150 0   0       for qw(size cache cache_dir cache_url_stub);
151              
152             # First, if we're caching locally, we need to see if we already have a
153             # cached version; if so, it's easy
154 0 0         if (my $url = $self->_get_cached_url($url, $params)) {
155 0           return $url;
156             }
157              
158             # Generate the appropriate URL:
159 0           my $uri = URI->new('http://webthumb.bluga.net/easythumb.php');
160 0           $uri->query_form(
161             url => $url,
162             size => $params->{size},
163             cache => $params->{cache},
164             user => $self->{user},
165             hash => Digest::MD5::md5_hex(join '',
166             strftime("%Y%m%d", gmtime(time())),
167             $url,
168             $self->{api_key}
169             ),
170             );
171              
172             # If we're caching, we want to fetch the resulting thumbnail and store it
173             # locally, then return the URL to that instead
174 0 0         if ($params->{cache_dir}) {
175 0           my $img_content = LWP::Simple::get($uri);
176 0 0         if ($img_content) {
177 0           my $url = $self->_cache_image($url, $params, $img_content);
178 0 0         return $url if defined $url;
179             }
180             }
181              
182 0           return $uri->as_string;
183             }
184              
185             =item easy_thumb
186              
187             An alias for C. This name was used in 0.01 to reflect the fact that
188             it used the L rather than
189             the full API; however, I think C is rather clearer as to the actual
190             purpose of the method, and the implementation of it is somewhat unimportant, so
191             consider this method somewhat deprecated (but likely to be supported
192             indefinitely.)
193              
194             =cut
195              
196 0     0 1   sub easy_thumb { shift->thumb_url(@_); }
197              
198              
199             sub _get_cached_url {
200 0     0     my ($self, $url, $params) = @_;
201              
202 0 0         my $dir = Path::Class::dir($params->{cache_dir})
203             or return;
204 0 0         my $file = $dir->file(
205             Digest::MD5::md5_hex($url . $params->{size})
206             ) or return;
207 0 0         my $stat = $file->stat or return;
208 0 0         if ($stat->mtime < time - ($params->{cache} * 24 * 60 * 60)) {
209 0           $file->remove;
210 0           return;
211             } else {
212 0           return $params->{cache_url_stub} . $file->basename;
213             }
214             }
215              
216             sub _cache_image {
217 0     0     my ($self, $url, $params, $img_content) = @_;
218              
219 0 0         my $dir = Path::Class::dir($params->{cache_dir})
220             or return;
221 0 0         my $file = $dir->file(
222             Digest::MD5::md5_hex($url . $params->{size})
223             ) or return;
224 0           $file->spew($img_content);
225 0           return $params->{cache_url_stub} . $file->basename;
226             }
227              
228              
229             =back
230              
231             =head1 AUTHOR
232              
233             David Precious, C<< >>
234              
235             =head1 ACKNOWLEDGEMENTS
236              
237             James Ronan
238              
239              
240             =head1 CONTRIBUTING
241              
242             This module is developed on GitHub at:
243              
244             L
245              
246             Bug reports / suggestions / pull requests are all very welcome.
247              
248             If you find this module useful, please feel free to
249             L
250              
251              
252             =head1 BUGS
253              
254             Bug reports via L
255             GitHub|https://github.com/bigpresh/WebService-Bluga-Webthumb/issues> are
256             preferred, as the module is developed on GitHub, and issues can be correlated to
257             commits. Bug reports via L
258             queue|http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Bluga-Webthumb>
259             are still valued though, if you'd prefer that way.
260              
261             =head1 SEE ALSO
262              
263             See the API documentation at L
264              
265             For a basic description of the service, see L
266              
267              
268             =head1 SUPPORT
269              
270             You can find documentation for this module with the perldoc command.
271              
272             perldoc WebService::Bluga::Webthumb
273              
274              
275              
276             =head1 LICENSE AND COPYRIGHT
277              
278             Copyright 2011 David Precious.
279              
280             This program is free software; you can redistribute it and/or modify it
281             under the terms of either: the GNU General Public License as published
282             by the Free Software Foundation; or the Artistic License.
283              
284             See http://dev.perl.org/licenses/ for more information.
285              
286              
287             =cut
288              
289             1; # End of WebService::Bluga::Webthumb