File Coverage

blib/lib/WWW/TMDB/API.pm
Criterion Covered Total %
statement 33 82 40.2
branch 0 16 0.0
condition 0 3 0.0
subroutine 11 21 52.3
pod 2 10 20.0
total 46 132 34.8


line stmt bran cond sub pod time code
1             package WWW::TMDB::API;
2              
3 1     1   28559 use 5.006;
  1         4  
  1         42  
4 1     1   7 use strict;
  1         2  
  1         136  
5 1     1   6 use warnings;
  1         7  
  1         39  
6 1     1   7 use Carp;
  1         2  
  1         117  
7              
8             our $VERSION = '0.04';
9 1     1   1244 use utf8;
  1         12  
  1         6  
10 1     1   1330 use LWP::UserAgent;
  1         79929  
  1         46  
11 1     1   13 use HTTP::Request;
  1         2  
  1         26  
12 1     1   1397 use JSON;
  1         19032  
  1         8  
13 1     1   184 use URI;
  1         2  
  1         947  
14              
15             our @namespaces = qw( Person Movie );
16             for (@namespaces) {
17             my $package = __PACKAGE__ . "::$_";
18             my $name = "\L$_";
19 1 0   1 0 845 eval qq(
  1 0   1 0 2  
  1     0 1 180  
  1     0 0 723  
  1     0 0 3  
  1     0 0 159  
  0     0      
  0     0      
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
20             use $package;
21             sub $name {
22             my \$self = shift;
23             if ( \$self->{'_$name'} ) {
24             return \$self->{'_$name'};
25             }else{
26             \$self->{'_$name'} = $package->new( api => \$self );
27             }
28             };
29              
30             package $package;
31             sub api {
32             return shift->{api};
33             };
34              
35             sub new {
36             my ( \$class, \%params ) = \@_;
37             my \$self = bless \\\%params, \$class;
38             \$self->{api} = \$params{api};
39             return \$self;
40             };
41              
42             1;
43             );
44             croak "Cannot create namespace $name: $@\n" if $@;
45             }
46              
47             sub send_api {
48 0     0 0   my ( $self, $command, $params_spec, $params ) = @_;
49              
50 0           $self->check_parameters( $params_spec, $params );
51 0           my $url = $self->url( $command, $params );
52 0           my $request = HTTP::Request->new( GET => $url );
53 0           $request->header( 'Accept' => 'application/json' );
54 0           my $json_response = $self->{ua}->request($request);
55 0 0         if ( $json_response->is_success ) {
56 0           return decode_json $json_response->content();
57             }
58             else {
59 0           croak
60             sprintf( "%s returned by %s", $json_response->status_line, $url );
61             }
62             }
63              
64             # Checks items that will be sent to the API($input)
65             # $params - an array that identifies valid parameters
66             # example :
67             # {'ID' => 1 }, 1- field is required, 0- field is optional
68             sub check_parameters {
69 0     0 0   my $self = shift;
70 0           my ( $params, $input ) = @_;
71              
72 0           foreach my $k ( keys(%$params) ) {
73 0 0 0       croak "Required parameter $k missing."
74             if ( $params->{$k} == 1 and !defined $input->{$k} );
75             }
76 0           foreach my $k ( keys(%$input) ) {
77 0 0         croak "Unknown parameter - $k." if ( !defined $params->{$k} );
78             }
79             }
80              
81             sub url {
82 0     0 0   my $self = shift;
83 0           my ( $command, $params ) = @_;
84 0           my $url = new URI( $self->{url} );
85 0           $url->path_segments( $self->{ver}, @$command );
86 0           $params->{api_key} = $self->{api_key};
87 0           $url->query_form($params);
88 0           return $url->as_string();
89             }
90              
91             sub new {
92 0     0 1   my $class = shift;
93 0           my (%params) = @_;
94              
95 0 0         croak "Required parameter api_key not provided." unless $params{api_key};
96 0 0         if ( !defined $params{ua} ) {
97 0           $params{ua} =
98             LWP::UserAgent->new( 'agent' => "Perl-WWW-TMDB-API/$VERSION", );
99             }
100             else {
101 0 0         croak "LWP::UserAgent expected."
102             unless $params{ua}->isa('LWP::UserAgent');
103             }
104              
105 0           my $self = {
106             api_key => $params{api_key},
107             ua => $params{ua},
108             ver => '3',
109             url => 'http://api.themoviedb.org',
110             };
111              
112 0           bless $self, $class;
113 0           return $self;
114             }
115              
116             =head1 NAME
117              
118             WWW::TMDB::API - TMDb API (http://api.themoviedb.org) client
119              
120             =head1 VERSION
121              
122             Version 0.04
123              
124             =head1 SYNOPSIS
125              
126             use WWW::TMDB::API;
127              
128             # The constructor has 2 parameters - the api_key and the optional LWP::UserAgent object, ua.
129             my $tmdb_client = WWW::TMDB::API->new( 'api_key' => 'your tmdb api key' );
130              
131             # Retrieve information about the person with ID == 287
132             $tmdb_client->person->info( ID => 287 );
133              
134             # Searches the themoviedb.org database for an actor, actress or production member with name 'Brad+Pitt'
135             $tmdb_client->person->search( query => 'Brad+Pitt' );
136              
137             # Searches the themoviedb.org database for an actor, actress or production member with name 'Brad'
138             $tmdb_client->person->search( query => 'Brad' );
139              
140             # Determines the last movie created in the themoviedb.org database.
141             $tmdb_client->movie->latest();
142              
143              
144             =head1 DESCRIPTION
145              
146             This module implements version 3 of the TMDb API. See L for the documentation.
147             The module uses the same parameter names used by the API.
148             The method names have been slightly changed. Here's the mapping of the method names used by this this module and the corresponding method names in the TMDb API:
149              
150             TMDB API WWW::TMDB::API
151             ---------------------- -------------------
152             Search Movies search/movie movie->search()
153             Search People search/person person->search()
154             Movie Info movie/[TMDb ID] movie->info()
155             Movie Alternative Titles movie/[TMDb ID]/alternative_titles movie->alternative_titles()
156             Movie Casts movie/[TMDb ID]/casts movie->casts()
157             Movie Images movie/[TMDb ID]/images movie->images()
158             Movie Keywords movie/[TMDb ID]/keywords movie->keywords()
159             Movie Release Info movie/[TMDb ID]/releases movie->releases()
160             Movie Trailers movie/[TMDb ID]/trailers movie->trailers()
161             Movie Translations movie/[TMDb ID]/translations movie->translations()
162             Person Info person/[TMDb ID]/info person->info()
163             Person Credits person/[TMDb ID]/credits person->credits()
164             Person Images person/[TMDb ID]/images person->images()
165             Latest Movie latest/movie movie->latest()
166              
167              
168             The API requires an API key which can be generated from http://api.themoviedb.org.
169             This module converts the API output to Perl data structure using the module JSON.
170             This module does not support update the method, Movie Add Rating.
171              
172             =head1 SUBROUTINES/METHODS
173              
174             =head2 new( %params )
175              
176             Returns a new instance of the B class.
177              
178             =over 4
179              
180             =item * B
181              
182             Required. This is the TMDb API key. Go to the L to signup and generate an API key.
183              
184             =item * B
185              
186             Optional. The LWP::UserAgent used to communicate with the TMDb server.
187              
188              
189             my $tmdb_client = WWW::TMDB::API->new( 'api_key' => 'your tmdb api key' );
190              
191             require LWP::UserAgent;
192             $ua = LWP::UserAgent->new(
193             'agent' => "Perl-WWW-TMDB-API",
194             );
195              
196             my $tmdb_client =
197             WWW::TMDB::API->new( 'api_key' => 'your tmdb api key', 'ua' => $ua );
198              
199              
200             =back
201              
202             =head2 movie->search( %params )
203              
204             Searches for movies.
205              
206             =over 4
207              
208             =item * B
209              
210             Required. This is the search text. The query can include the year the movie was released (e.g. B) to narrow the search results.
211              
212             =item * B
213              
214             Optional. Use this parameter to iterate through the search results. Search results that exceed 20 items are paginated.
215              
216             =item * B
217              
218             Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
219              
220             =item * B
221              
222             Optional. [true/false, defaults to false if unspecified]. Set this to true to include adult items in the search results.
223              
224             =back
225             $result = $api->movie->search( 'query' => 'Cool Hand' );
226              
227              
228             =head2 person->search( %params )
229              
230             Searches for actors, actresses, or production members.
231              
232             =over 4
233              
234             =item * B
235              
236             Required. This is the search text.
237              
238              
239             =item * B
240              
241             Optional. Use this parameter to iterate through the search results. Search results that exceed 20 items are paginated.
242              
243             =back
244              
245             $result = $api->person->search( 'query' => 'Newman' );
246              
247              
248             =head2 movie->info( %params )
249              
250             Retrieves basic information about a movie.
251             Building image urls from the file_paths returned by this method is discussed in the document L.
252              
253             =over 4
254              
255             =item * B
256              
257             Required. The TMDb ID of the movie.
258              
259             =item * B
260              
261             Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
262              
263             =back
264              
265             $result = $api->movie->info( ID => 903 );
266              
267              
268             =head2 movie->alternative_titles( %params )
269              
270             Retrieves a movie's alternative titles.
271              
272             =over 4
273              
274             =item * B
275              
276             Required. The TMDb ID of the movie.
277              
278             =item * B
279              
280             Optional. This limits the result to items tagged with the specified country. The expected value is a ISO 3166-1 code.
281              
282             =back
283              
284             $result = $api->movie->alternative_titles( ID => 903 );
285             $result = $api->movie->alternative_titles( ID => 903, 'language' => 'fr' );
286              
287              
288             =head2 movie->casts( %params )
289              
290             Retrieves a movie's cast information.
291              
292             =over 4
293              
294             =item * B
295              
296             Required. The TMDb ID of the movie.
297              
298             =back
299              
300             $result = $api->movie->casts( ID => 903 );
301              
302              
303             =head2 movie->images( %params )
304              
305             Retrieves all of the images for a particular movie.
306             Building image urls from the file_paths returned by this method is discussed in the document:
307             L
308              
309             =over 4
310              
311             =item * B
312              
313             Required. The TMDb ID of the movie.
314              
315             =item * B
316              
317             Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
318              
319             =back
320              
321             $result = $api->movie->images( ID => 903 );
322             $result = $api->movie->images( ID => 903, 'language' => 'en' );
323              
324              
325             =head2 movie->keywords( %params )
326              
327             Retrieves the keywords for a movie.
328              
329             =over 4
330              
331             =item * B
332              
333             Required. The TMDb ID of the movie.
334              
335             =back
336              
337             $result = $api->movie->keywords( ID => 903 );
338              
339             =head2 movie->releases( %params )
340              
341             Retrieves release and certification data for a specific movie.
342              
343             =over 4
344              
345             =item * B
346              
347             Required. The TMDb ID of the movie.
348              
349             =back
350              
351             $result = $api->movie->releases( ID => 903 );
352              
353              
354             =head2 movie->trailers( %params )
355              
356             Retrieves the trailers for a movie.
357              
358             =over 4
359              
360             =item * B
361              
362             Required. The TMDb ID of the movie.
363              
364             =item * B
365              
366             Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
367              
368             =back
369              
370             $result = $api->movie->trailers( ID => 903 );
371              
372              
373             =head2 movie->translations( %params )
374              
375             Retrieves the list of translations for a movie.
376              
377             =over 4
378              
379             =item * B
380              
381             Required. The TMDb ID of the movie.
382              
383             =back
384              
385             $result = $api->movie->translations( ID => 903 );
386              
387              
388             =head2 person->info( %params )
389              
390             Retrieves basic information about a person.
391             Building image urls from the file_paths returned by this method is discussed in the document:
392             L
393              
394             =over 4
395              
396             =item * B
397              
398             Required. The TMDb ID of the person.
399              
400             =back
401              
402             $result = $api->person->info( ID => 3636 );
403              
404             =head2 person->credits( %params )
405              
406             Retrieves the movies that have cast or crew credits for a person.
407              
408             =over 4
409              
410             =item * B
411              
412             Required. The TMDb ID of the person.
413              
414             =item * B
415              
416             Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
417              
418             =back
419              
420             $result = $api->person->credits( ID => 3636 );
421              
422              
423             =head2 person->images( %params )
424              
425             Retrieves all the profile images of the person.
426             Building image urls from the file_paths returned by this method is discussed in the document:
427             L]
428              
429             =over 4
430              
431             =item * B
432              
433             Required. The TMDb ID of the person.
434              
435             =back
436              
437             $result = $api->person->images( ID => 3636 );
438              
439              
440             =head2 movie->latest( )
441              
442             Returns the newest movie created in the themoviedb.org database.
443              
444              
445             =head1 AUTHOR
446              
447             Maria Celina Baratang, C<< >>
448              
449             =head1 BUGS
450              
451             Please report any bugs or feature requests to C, or through
452             the web interface at L. I will be notified, and then you'll
453             automatically be notified of progress on your bug as I make changes.
454              
455              
456             =head1 SUPPORT
457              
458             You can find documentation for this module with the perldoc command.
459              
460             perldoc WWW::TMDB::API
461              
462              
463             You can also look for information at:
464              
465              
466             =over 4
467              
468             =item * TMDb The open movie database
469              
470             L
471              
472             =item * themoviedb.org API Documentation
473              
474             L
475             L
476              
477             =item * RT: CPAN's request tracker (report bugs here)
478              
479             L
480              
481             =item * AnnoCPAN: Annotated CPAN documentation
482              
483             L
484              
485             =item * CPAN Ratings
486              
487             L
488              
489             =item * Search CPAN
490              
491             L
492              
493             =back
494              
495              
496             =head1 ACKNOWLEDGEMENTS
497              
498             =head1 LICENSE AND COPYRIGHT
499              
500             Copyright 2012 Maria Celina Baratang.
501              
502             This program is free software; you can redistribute it and/or modify it
503             under the terms of either: the GNU General Public License as published
504             by the Free Software Foundation; or the Artistic License.
505              
506             See http://dev.perl.org/licenses/ for more information.
507              
508              
509             =cut
510              
511             1; # End of WWW::TMDB::API
512