MoviePosterDB.pm

filter: Statementstmt 77.4% 96 / 124 Branchbran 45.4% 20 / 44 Conditioncond 58.6% 17 / 29 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 67.2% 154 / 229 CC 33 SCAR 27.0
CC
33
Cov
68%
search
33.2
_get_secret
14.5
new
11.3
CRAP 14.9
↑ Summary MoviePosterDB/Movie.pm »
1 # $Id: MoviePosterDB.pm 6486 2011-06-13 13:42:02Z chris $
2  
3 =head1 NAME
4  
5 WebService::MoviePosterDB - OO Perl interface to the movie poster database MoviePosterDB.
6  
7  
8 =head1 SYNOPSIS
9  
10 use WebService::MoviePosterDB;
11  
12 my $ws = WebService::MoviePosterDB->new(api_key => "key", api_secret => "secret", cache => 1, cache_exp => "12h");
13  
14 my $movie = $ws->search(type => "Movie", imdbid => "tt0114814", width => 300);
15  
16 print $movie->title(), ": \n\n";
17 print $movie->page(), "\n\n";
18  
19 foreach ( @{$movie->posters()} ) {
20 print $_->image_location(), "\n";
21 }
22  
23  
24 =head1 DESCRIPTION
25  
26 WebService::MusicBrainz is an object-oriented interface to MoviePosterDB. It can
27 be used to retrieve artwork for IMDB titles.
28  
29 =cut
30  
31  
32 package WebService::MoviePosterDB;
33  
34 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 our $VERSION = '0.18';
38  
39 1 ▶use Cache::FileCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 ▶use File::Spec::Functions qw(tmpdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 1 ▶use WebService::MoviePosterDB::Movie;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 =head1 METHODS
54  
55 =head2 new(%opts)
56  
57 Constructor.
58  
59 %opts can contain:
60  
61 =over 4
62  
63 =item api_key, api_secret
64  
65 A key and secret are required to use the API. Contact movieposterdb.com for details.
66  
67 =item cache
68  
69 Whether to cache responses. Defaults to true
70  
71 =item cache_root
72  
73 The root dir for the cache. Defaults to tmpdir();
74  
75 =item cache_exp
76  
77 How long to cache responses for. Defaults to "1h"
78  
79 =back
80  
81 =cut
82  
83 5 ▶sub new {
Subroutine new: called CC=11 SCAR=24.2
Pod documented
84 5 my $class = shift;
85 5 my %args = @_;
86 5 my $self = {};
87  
88 5 bless $self, $class;
89  
90 5 ▶ if ((!exists $args{'api_version'} || !defined $args{'api_version'} || $args{'api_version'} == 1) && !exists $args{'api_key'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(exists $args{'api_version'} && defined $args{'api_version'}) || $args{'api_version'} == 1 and not exists $args{'api_key'}) { } 2 3 5
Condition88%8/9
!(exists $args{"api_version"} && defined $args{"api_version"}) || $args{"api_version"} == 1 and not exists $args{"api_key"}
!ll&&!rl&&r
122
not exists $args{"api_version"} && defined $args{"api_version"} or $args{"api_version"} == 1
l!l&&r!l&&!r
311
exists $args{'api_version'} && defined $args{'api_version'}
!ll&&!rl&&r
302
MC/DC75%3/4
!(exists $args{"api_version"} && defined $args{"api_version"}) || $args{"api_version"} == 1 and not exists $args{"api_key"}
exists $args{'api_version'} defined $args{'api_version'} $args{"api_version"} == 1 not exists $args{"api_key"}
Truth table57%4/7
not(A && B) or C and not(D)
A= exists $args{'api_version'}B= defined $args{'api_version'}C= $args{"api_version"} == 1D= not exists $args{"api_key"}
ABCDresult
110X0
0XX00
10X00
11100
0XX11
10X11
11111
91 2 carp "version 1 API is no longer available, using demo credentials";
92 2 $self->{'api_key'} = "demo";
93 2 $self->{'api_secret'} = "demo";
94 } else {
95 3 $self->{'api_key'} = $args{'api_key'};
96 3 $self->{'api_secret'} = $args{'api_secret'};
97 }
98  
99 5 ▶ if (!defined $self->{'api_key'} || !defined $self->{'api_secret'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"api_key"} and defined $self->{"api_secret"}) 2 3 5
Condition100%3/3
defined $self->{"api_key"} and defined $self->{"api_secret"}
!ll&&!rl&&r
113
MC/DC100%2/2
defined $self->{"api_key"} and defined $self->{"api_secret"}
defined $self->{"api_key"} defined $self->{"api_secret"}
Truth table100%3/3
A and B
A= defined $self->{"api_key"}B= defined $self->{"api_secret"}
ABresult
0X0
100
111
100 2 croak "api_key and/or api_secret missing";
101 }
102  
103 3 ▶ $self->{'_cache_root'} = $args{'cache_root'} || tmpdir();
Condition33%1/3
$args{'cache_root'} || tmpdir()
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{'cache_root'} || tmpdir()
$args{'cache_root'} tmpdir()
Truth table33%1/3
A || B
A= $args{'cache_root'}B= tmpdir()
ABresult
1X1
011
000
104 3 ▶ $self->{'_cache_exp'} = $args{'cache_exp'} || "1h";
Condition50%1/2
$args{'cache_exp'} || '1h'
l!l
03
MC/DC0%0/1
$args{'cache_exp'} || '1h'
$args{'cache_exp'}
Truth table50%1/2
A
A= $args{'cache_exp'}
Aresult
11
00
105 3 ▶ $self->{'_cache'} = defined $args{'cache'} ? $args{'cache'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'cache'} ? : 0 3 3
106  
107 3 ▶ if ($self->{'_cache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache"}) 3 0 3
108 3 $self->{'_cacheObj'} = Cache::FileCache->new( {'cache_root' => $self->{'_cache_root'}, 'namespace' => "WebService-MoviePosterDB", 'default_expires_in' => $self->{'_cache_exp'}} );
109 }
110  
111 3 $self->{'_useragent'} = LWP::UserAgent->new();
112 3 $self->{'_useragent'}->env_proxy();
113 3 $self->{'_useragent'}->agent("WebService::MoviePosterDB/$VERSION");
114  
115 3 return $self;
116 }
117  
118  
119 =head2 search(type => "Movie", %args)
120  
121 Accesses MoviePosterDB and returns a WebService::MoviePosterDB::Movie object.
122  
123 %args can contain:
124  
125 =over 4
126  
127 =item type
128  
129 Controls the type of resource being requested. Currently only supports "Movie".
130  
131 =item tconst
132  
133 IMDB id for the title, e.g. tt0114814
134  
135 =item imdbid
136  
137 Alias for tconst
138  
139 =item title
140  
141 Name of the title
142  
143 =item width
144  
145 Image width for returned artwork
146  
147 =back
148  
149 =cut
150  
151 1 ▶sub search {
Subroutine search: called CC=12 SCAR=35.0
Pod documented
152 1 my $self = shift;
153 1 my %args = @_;
154  
155 1 ▶ croak "Unknown type" unless ($args{'type'} eq "Movie");
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"type"} eq "Movie" 0 1 1
156  
157 1 my %_args;
158  
159 1 ▶ if (exists $args{'imdb_code'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'imdb_code'}) { } 0 1 1
elsif (exists $args{'tconst'} or exists $args{'imdbid'}) { } 1 0 1
160 0 $_args{'imdb_code'} = sprintf("%d", $args{'imdb_code'}); # Trim leading zeroes
161 ▶ } elsif (exists $args{'tconst'} || exists $args{'imdbid'}) {
Condition33%1/3
exists $args{"tconst"} or exists $args{"imdbid"}
l!l&&r!l&&!r
100
MC/DC0%0/2
exists $args{"tconst"} or exists $args{"imdbid"}
exists $args{"tconst"} exists $args{"imdbid"}
Truth table33%1/3
A or B
A= exists $args{"tconst"}B= exists $args{"imdbid"}
ABresult
1X1
011
000
162 1 ▶ my $tconst = exists $args{'tconst'} ? $args{'tconst'} : $args{'imdbid'};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'tconst'} ? : 1 0 1
163 1 ▶ my ($id) = $tconst =~ m/^tt(\d{6,7})$/ or croak "Unable to parse tconst '$tconst'";
Branch: 1 branch
BranchTrueFalseTotal
my($id) = $tconst =~ /^tt(\d{6,7})$/ or croak("Unable to parse tconst '${tconst}'") 1 0 1
164 1 $_args{'imdb_code'} = sprintf("%d", $id); # Trim leading zeroes
165 }
166 0 ▶ if (exists $args{'title'}) { $_args{'title'} = $args{'title'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"title"}) 0 1 1
167 1 ▶ if (exists $args{'width'}) { $_args{'width'} = $args{'width'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"width"}) 1 0 1
168  
169 # Ugly hack. The demi api service appears to normalise the title key to lower case before returning the secret hash.
170 0 ▶ if (exists $_args{'title'} && $self->{'api_key'} eq "demo" && $self->{'api_secret'} eq "demo") { $_args{'title'} = lc $_args{'title'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_args{"title"} and $self->{"api_key"} eq "demo" and $self->{"api_secret"} eq "demo") 0 1 1
Condition33%2/6
exists $_args{"title"} and $self->{"api_key"} eq "demo" and $self->{"api_secret"} eq "demo"
!ll&&!rl&&r
100
exists $_args{"title"} and $self->{"api_key"} eq "demo"
!ll&&!rl&&r
100
MC/DC0%0/3
exists $_args{"title"} and $self->{"api_key"} eq "demo" and $self->{"api_secret"} eq "demo"
exists $_args{"title"} $self->{"api_key"} eq "demo" $self->{"api_secret"} eq "demo"
Truth table25%1/4
A and B and C
A= exists $_args{"title"}B= $self->{"api_key"} eq "demo"C= $self->{"api_secret"} eq "demo"
ABCresult
0XX0
10X0
1100
1111
171  
172 1 $_args{'api_key'} = $self->{'api_key'};
173 1 $_args{'secret'} = $self->_get_secret(%_args);
174  
175 0 my $uri = URI->new();
176 0 $uri->scheme("http");
177 0 $uri->host("api.movieposterdb.com");
178 0 $uri->path("json");
179 0 $uri->query_form( map { my ($n, $v) = ($_, $_args{$_}); utf8::encode($n); utf8::encode($v); ($n => $v); } sort keys %_args );
180  
181 0 my $json = JSON->new()->decode($self->_get_page($uri->as_string()));
182  
183 0 return WebService::MoviePosterDB::Movie->_new($json);
184  
185 }
186  
187 1 ▶sub _get_secret {
Subroutine _get_secret: called CC=8 SCAR=26.7
188 1 my $self = shift;
189 1 my %args = @_;
190  
191 1 ▶ if ($self->{'api_key'} eq "demo" && $self->{'api_secret'} eq "demo") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'api_key'} eq 'demo' and $self->{'api_secret'} eq 'demo') { } 1 0 1
Condition33%1/3
$self->{"api_key"} eq "demo" and $self->{"api_secret"} eq "demo"
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"api_key"} eq "demo" and $self->{"api_secret"} eq "demo"
$self->{"api_key"} eq "demo" $self->{"api_secret"} eq "demo"
Truth table33%1/3
A and B
A= $self->{"api_key"} eq "demo"B= $self->{"api_secret"} eq "demo"
ABresult
0X0
100
111
192  
193 1 my %_args;
194  
195 0 ▶ if (exists $args{'title'}) {$_args{'title'} = $args{'title'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"title"}) 0 1 1
196 1 ▶ if (exists $args{'imdb_code'}) {$_args{'imdb_code'} = $args{'imdb_code'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"imdb_code"}) 1 0 1
197  
198 1 $_args{'type'} = "JSON";
199 1 $_args{'api_key'} = $self->{'api_key'};
200 1 $_args{'api_secret'} = $self->{'api_secret'};
201  
202 1 my $uri = URI->new();
203 1 $uri->scheme("http");
204 1 $uri->host("api.movieposterdb.com");
205 1 $uri->path("console");
206 1 $uri->query_form( map { my ($n, $v) = ($_, $_args{$_}); utf8::encode($n); utf8::encode($v); ($n => $v); } sort keys %_args );
207  
208 1 my $page = $self->_get_page($uri->as_string());
209 0 ▶ my ($s) = $page =~ m/secret=([a-f0-9]{12})/ or die "Failed to extract secret";
Branch: 1 branch
BranchTrueFalseTotal
my($s) = $page =~ /secret=([a-f0-9]{12})/ or die "Failed to extract secret" 0 0 0
210  
211 0 return $s;
212  
213 } else {
214 0 my $v = $self->{'api_secret'};
215 0 ▶ if (exists $args{'imdb_code'}) { $v .= sprintf("%d", $args{'imdb_code'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"imdb_code"}) 0 0 0
216 0 ▶ if (exists $args{'title'}) { $v .= $args{'title'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"title"}) 0 0 0
217  
218 0 utf8::encode($v);
219  
220 0 return substr(md5_hex($v), 10, 12);
221 }
222  
223 }
224  
225 1 ▶sub _get_page {
Subroutine _get_page: called CC=5 SCAR=19.4
226 1 my $self = shift;
227 1 my $url = shift;
228  
229 1 my $content;
230  
231 1 ▶ if ($self->{'_cache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache"}) 1 0 1
232 1 $content = $self->{'_cacheObj'}->get($url);
233 }
234  
235 1 ▶ if (! defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 1 0 1
236 1 my $response = $self->{'_useragent'}->get($url);
237  
238 1 ▶ if($response->code() ne "200") {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code ne 200) 1 0 1
239 1 croak "URL (", $url, ") Request Failed - Code: ", $response->code(), " Error: ", $response->message(), "\n";
240 }
241  
242 0 $content = $response->decoded_content();
243  
244 0 ▶ if ($self->{'_cache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache"}) 0 0 0
245 0 $self->{'_cacheObj'}->set($url, $content);
246 }
247 }
248  
249 0 return $content;
250 }
251  
252 1 1;
253  
254  
255 =head1 NOTES
256  
257 The version 1 API, previously used by default, stopped as of 2011-09-27, and credentials
258 are required to access the version 2 API. It is possible to access the
259 version 2 API using test credentials (key, secret = "demo"), and this will be
260 done for legacy applications that try to use the version 1 API. However, this
261 feature is only intended for test purposes: legacy applications should be adapted,
262 and new applications should not use it.
263  
264  
265 =head1 AUTHOR
266  
267 Christopher Key <cjk32@cam.ac.uk>
268  
269  
270 =head1 COPYRIGHT AND LICENCE
271  
272 Copyright (C) 2010-2011 Christopher Key <cjk32@cam.ac.uk>
273  
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself, either Perl version 5.8.4 or,
276 at your option, any later version of Perl 5 you may have available.
277  
278 =cut
↑ Summary MoviePosterDB/Movie.pm »