Moviepilot.pm

filter: Statementstmt 27.0% 26 / 96 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 8 / 18 Podpod 100% 10 / 10 totaltotal 29.9% 44 / 147 CC 12 SCAR 23.0
CC
12
Cov
23%
new
30.0
movie
12.0
search_movie
12.0
CRAP 9.9
« Moviepilot/Person.pm ↑ Summary
1 package WWW::Moviepilot;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use WWW::Moviepilot::Movie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 WWW::Moviepilot - Interface to the moviepilot.de database
17  
18 =head1 VERSION
19  
20 Version 0.04
21  
22 =cut
23  
24 2 our $VERSION = '0.04';
25  
26 =head1 SYNOPSIS
27  
28 use WWW::Moviepilot;
29 my $m = WWW::Moviepilot->new({
30 api_key => ...,
31 host => 'www.moviepilot.de',
32 });
33  
34 # direct retrieval
35 my $movie = $m->movie( 'matrix' );
36  
37 # search
38 my @movies = $m->search_movie( 'matrix' );
39 foreach my $movie ( @movies ) {
40 print $movie->display_title;
41 }
42  
43 # cast of a movie
44 my @cast = $m->cast( 'matrix' );
45 foreach my $person ( @cast ) {
46 print $person->last_name;
47 print $person->character;
48 }
49  
50 # filmography of a person
51 my @filmography = $m->filmography( 'paul-newman' );
52 foreach my $movie ( @filmography ) {
53 print $movie->display_title;
54 print $movie->character;
55 }
56  
57 I<Please note: This module is still in early development and subject to change.>
58  
59 =head1 METHODS
60  
61 =head2 new( $args )
62  
63 Creates a new WWW::Moviepilot instance.
64  
65 my $m = WWW::Moviepilot->new( $args );
66  
67 C<$args> must be a hash reference, you should supply an API key:
68  
69 $args->{api_key} = ...;
70  
71 To get a valid API key you should read L<http://wiki.github.com/moviepilot/moviepilot-API/>.
72  
73 Further optional arguments:
74  
75 =over 4
76  
77 =item * C<host> (default: C<www.moviepilot.de>)
78  
79 The host where the requests are sent to.
80  
81 =item * C<ua> (default: C<< LWP::UserAgent->new >>)
82  
83 A L<LWP::UserAgent> compatible user agent.
84  
85 =back
86  
87 =cut
88  
89 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
90 0 my ($class, $args) = @_;
91 0 my $self = bless {} => $class;
92 0 ▶ $self->{api_key} = $args->{api_key} || croak "api_key is missing at " . __PACKAGE__ . "->new()";
Condition0%0/2
$args->{'api_key'} || croak('api_key is missing at WWW::Moviepilot->new()')
l!l
00
MC/DC0%0/1
$args->{'api_key'} || croak('api_key is missing at WWW::Moviepilot->new()')
$args->{'api_key'}
Truth table0%0/2
A
A= $args->{'api_key'}
Aresult
11
00
93 0 ▶ $self->{host} = $args->{host} || 'www.moviepilot.de';
Condition0%0/2
$args->{'host'} || 'www.moviepilot.de'
l!l
00
MC/DC0%0/1
$args->{'host'} || 'www.moviepilot.de'
$args->{'host'}
Truth table0%0/2
A
A= $args->{'host'}
Aresult
11
00
94 0 ▶ $self->{ua} = $args->{ua} || LWP::UserAgent->new;
Condition0%0/3
$args->{'ua'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'ua'} || 'LWP::UserAgent'->new
$args->{'ua'} 'LWP::UserAgent'->new
Truth table0%0/3
A || B
A= $args->{'ua'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
95  
96 0 ▶ $self->{host} = 'http://' . $self->{host} unless $self->{host} =~ m{^http://};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"host"} =~ m[^http://] 0 0 0
97  
98 0 return $self;
99 }
100  
101 =head2 movie( $name ) | movie( $source => $id )
102  
103 Retrieve a movie as L<WWW::Moviepilot::Movie> object.
104 There are two ways to specify which movie to retrieve.
105 First, you can provide the name of the movie (this name is some kind of normalised,
106 I'm not sure how exactly):
107  
108 my $movie = $m->movie( 'matrix' );
109  
110 The second form is to provide an alternate ID:
111  
112 my $movie = $m->movie( imdb => '133093' );
113 my $movie = $m->movie( amazon => 'B00004R80K' );
114  
115 =cut
116  
117 0 ▶sub movie {
Subroutine movie: not called CC=3 SCAR=24.8
Pod documented
118 0 my ($self, @args) = @_;
119  
120 0 my $url = $self->host . '/movies/';
121 0 ▶ if ( @args > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 2) 0 0 0
122 0 croak "invalid usage of " . __PACKAGE__ . "->movie()";
123 }
124 0 $url .= join '-id-', map { uri_escape($_) } @args;
125 0 $url .= '.json';
126  
127 0 my $uri = URI->new( $url );
128 0 $uri->query_form( api_key => $self->api_key );
129 0 my $res = $self->ua->get( $uri->as_string );
130  
131 0 ▶ if( $res->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
132 0 croak $res->status_line;
133 }
134  
135 0 my $json = JSON::Any->from_json( $res->decoded_content );
136  
137 0 my $movie = WWW::Moviepilot::Movie->new({ m => $self });
138 0 $movie->populate({ data => { movie => $json } });
139 0 return $movie;
140 }
141  
142 =head2 search_movie( $query )
143  
144 Searches for a movie and returns a list with results:
145  
146 my @movielist = $m->search_movie( 'matrix' );
147 if ( @movielist == 0 ) {
148 print 'no movies found';
149 }
150 else {
151 # each $movie is a WWW::Moviepilot::Movie object
152 foreach my $movie ( @movielist ) {
153 print $movie->display_title; # e.g. Matrix
154 print $movie->production_year; # e.g. 1999
155 print scalar $movie->emotions_list; # e.g. Spannend,Aufregend
156  
157 # in list context, all *_list fields are split up by comma
158 my @emotions = $movie->emotions_list;
159 }
160 }
161  
162 At most there are 20 movies returned.
163  
164 See L<WWW::Moviepilot::Movie>.
165  
166 =cut
167  
168 0 ▶sub search_movie {
Subroutine search_movie: not called CC=3 SCAR=24.8
Pod documented
169 0 my ($self, $query) = @_;
170  
171 0 my $uri = URI->new( $self->host . '/searches/movies.json' );
172 0 $uri->query_form(
173 api_key => $self->api_key,
174 q => $query,
175 );
176  
177 0 my $res = $self->ua->get( $uri->as_string );
178 0 ▶ if ( $res->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
179 0 croak $res->status_line;
180 }
181  
182 0 my $o = JSON::Any->from_json( $res->decoded_content );
183  
184 0 my @result = ();
185 0 foreach my $entry ( @{ $o->{movies} } ) {
186 0 my $movie = WWW::Moviepilot::Movie->new({ m => $self });
187 0 $movie->populate({ data => { movie => $entry } });
188 0 push @result, $movie;
189 }
190  
191 0 return @result;
192 }
193  
194 =head2 person( $name )
195  
196 Retrieve a person as L<WWW::Moviepilot::Person> object.
197 You should provide the name of the movie (this name is some kind of normalised,
198 I'm not sure how exactly):
199  
200 my $person = $m->person( 'paul-newman' );
201  
202 =cut
203  
204 0 ▶sub person {
Subroutine person: not called CC=2 SCAR=17.9
Pod documented
205 0 my ($self, $name) = @_;
206  
207 0 my $uri = URI->new( $self->host . '/people/' . uri_escape($name) . '.json' );
208 0 $uri->query_form( api_key => $self->api_key );
209 0 my $res = $self->ua->get( $uri->as_string );
210  
211 0 ▶ if( $res->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
212 0 croak $res->status_line;
213 }
214  
215 0 my $json = JSON::Any->from_json( $res->decoded_content );
216 0 my $person = WWW::Moviepilot::Person->new({ m => $self });
217 0 $person->populate({ data => { person => $json } });
218 0 return $person;
219 }
220  
221 =head2 search_person( $query )
222  
223 Searches for a person and returns a list with results:
224  
225 my @people = $m->search_person( 'Paul Newman' );
226 if ( @people == 0 ) {
227 print 'no people found';
228 }
229 else {
230 # each $person is a WWW::Moviepilot::Person object
231 foreach my $person ( @person ) {
232 print $person->first_name; # e.g. Paul
233 print $person->last_name; # e.g. Newman
234 }
235 }
236  
237 See L<WWW::Moviepilot::Person>.
238  
239 =cut
240  
241 0 ▶sub search_person {
Subroutine search_person: not called CC=3 SCAR=24.8
Pod documented
242 0 my ($self, $query) = @_;
243  
244 0 my $uri = URI->new( $self->host . '/searches/people.json' );
245 0 $uri->query_form(
246 api_key => $self->api_key,
247 q => $query,
248 );
249  
250 0 my $res = $self->ua->get( $uri->as_string );
251 0 ▶ if ( $res->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
252 0 croak $res->status_line;
253 }
254  
255 0 my $o = JSON::Any->from_json( $res->decoded_content );
256  
257 0 my @result = ();
258 0 foreach my $entry ( @{ $o->{people} } ) {
259 0 my $person = WWW::Moviepilot::Person->new({ m => $self });
260 0 $person->populate({ data => { person => $entry } });
261 0 push @result, $person;
262 }
263  
264 0 return @result;
265 }
266  
267 =head2 cast( $name )
268  
269 Returns the cast of a movie.
270  
271 my $m = WWW::Moviepilot->new(...);
272 my @cast = $m->cast('brust-oder-keule');
273  
274 See L<WWW::Moviepilot::Person>.
275  
276 =cut
277  
278 0 ▶sub cast {
Subroutine cast: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self, $name) = @_;
280 0 my $movie = WWW::Moviepilot::Movie->new({ m => $self });
281 0 return $movie->cast( $name );
282 }
283  
284 =head2 filmography( $name )
285  
286 Returns the filmography of a person.
287  
288 my $m = WWW::Moviepilot->new(...);
289 my @filmography = $m->filmography('paul-newman');
290  
291 See L<WWW::Moviepilot::Movie>.
292  
293 =cut
294  
295 0 ▶sub filmography {
Subroutine filmography: not called CC=1 SCAR=6.9
Pod documented
296 0 my ($self, $name) = @_;
297 0 my $person = WWW::Moviepilot::Person->new({ m => $self });
298 0 return $person->filmography( $name );
299 }
300  
301 =head2 api_key
302  
303 my $api_key = $m->api_key;
304  
305 Returns the API key provided to the C<new> constructor.
306  
307 =cut
308  
309 0 ▶sub api_key { return shift->{api_key} }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod documented
310  
311 =head2 ua
312  
313 my $ua = $m->ua;
314  
315 Returns the user agent, usually a L<LWP::UserAgent>.
316  
317 =cut
318  
319 0 ▶sub ua { return shift->{ua} }
Subroutine ua: not called CC=1 SCAR=6.9
Pod documented
320  
321 =head2 host
322  
323 my $host = $m->host;
324  
325 Returns host which the requests are sent to provided to the C<new> constructor.
326  
327 =cut
328  
329 0 ▶sub host { return shift->{host} }
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
330  
331 2 1;
332 __END__
« Moviepilot/Person.pm ↑ Summary