Plagger/Plugin/Filter/ImageInfo.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 70 CC 5 SCAR 21.0
CC
5
Cov
0%
fixup
20.0
fetch_image_info
6.0
register
2.0
CRAP 8.2
1 package Plagger::Plugin::Filter::ImageInfo;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use Image::Info;
6 use Plagger::UserAgent;
7  
8 sub register {
9 my($self, $context) = @_;
10 $context->register_hook(
11 $self,
12 'update.entry.fixup' => \&entry,
13 'update.feed.fixup' => \&feed,
14 );
15 }
16  
17 sub entry {
18 my($self, $context, $args) = @_;
19 $self->fixup($context, $args->{entry}->icon);
20 }
21  
22 sub feed {
23 my($self, $context, $args) = @_;
24 $self->fixup($context, $args->{feed}->image);
25 }
26  
27 sub fixup {
28 my($self, $context, $image) = @_;
29  
30 # do nothing if there's no image, or image already has width/height
31 return unless $image && $image->{url};
32 return if $image->{width} && $image->{height};
33  
34 $context->log(info => "Trying to fetch image size of $image->{url}");
35  
36 my $info = $self->cache->get_callback(
37 $image->{url},
38 sub { $self->fetch_image_info($image->{url}) },
39 "3 days",
40 );
41  
42 if ($info) {
43 $context->log(debug => "width=$info->{width}, height=$info->{height}");
44 $image->{width} = $info->{width};
45 $image->{height} = $info->{height};
46 }
47 }
48  
49 sub fetch_image_info {
50 my($self, $url) = @_;
51  
52 my $ua = Plagger::UserAgent->new;
53 my $res = $ua->fetch($url);
54  
55 if ($res->is_error) {
56 Plagger->context->log(error => "Error fetching $url");
57 return;
58 }
59  
60 my $info = eval { Image::Info::image_info(\$res->content) };
61 $info;
62 }
63  
64 1;
65  
66 __END__