File Coverage

blib/lib/WWW/TVMaze.pm
Criterion Covered Total %
statement 23 97 23.7
branch 0 10 0.0
condition 0 9 0.0
subroutine 8 26 30.7
pod 17 17 100.0
total 48 159 30.1


line stmt bran cond sub pod time code
1             package WWW::TVMaze;
2              
3 1     1   21889 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         20  
5 1     1   5 use warnings;
  1         10  
  1         28  
6 1     1   860 use Mouse;
  1         32844  
  1         5  
7 1     1   1401 use LWP::UserAgent;
  1         63170  
  1         48  
8 1     1   1082 use JSON::XS;
  1         6419  
  1         66  
9 1     1   1311 use DateTime;
  1         116842  
  1         40  
10 1     1   10 use Params::Validate qw(:all);
  1         2  
  1         1630  
11              
12             =head1 NAME
13              
14             WWW::TVMaze - Interface to TVMaze API
15              
16             =head1 VERSION
17              
18             Version 0.01
19              
20             =cut
21              
22             our $VERSION = '0.02';
23              
24             has '_endpoint' => (
25             isa => 'Str',
26             is =>'ro',
27             default => 'http://api.tvmaze.com'
28             );
29              
30             has 'error' => (
31             isa => 'Maybe[Str]',
32             is => 'rw',
33             default => undef,
34             );
35              
36             has 'http_status' => (
37             isa => 'Maybe[Str]',
38             is =>'rw',
39             default => undef,
40             );
41              
42              
43             =head1 SYNOPSIS
44              
45             This module allows you to user TVMaze API (L)
46              
47             use WWW::TVMaze;
48              
49             my $tv_maze = WWW::TVMaze->new();
50              
51             my $shows = $tv_maze->show_search('Arrow');
52              
53              
54             =head1 METHODS
55              
56             =head2 shows
57              
58             my $show = $tv_maze->shows($id);
59              
60             Returns a show by its ID
61              
62              
63             =head2 show_search
64              
65             my $shows = $tv_maze->show_search($search_keyword);
66              
67             Returns a list of shows that match your search keyword
68              
69              
70             =head2 show_single_search
71              
72             my $show = $tv_maze->show_single_search($search_keyword);
73              
74             Returns a single show that match your search keyword
75              
76             =head2 show_lookup
77              
78             my $show = $tv_maze->show_lookup( $id, $id_type ); # $id_type can be 'tvrage' or 'thetvdb'
79              
80             Returns a show by its TVRage ID or by its THETVDB ID
81              
82             =head2 show_episode_list
83              
84             my $ep_list = $self->show_episode_list($show_id, $include_specials); # $include_specials can be 0 or 1 and is optional;
85              
86             Returns a complete list of episodes for a given show. by defauls specials are not included
87              
88             =head2 show_cast
89              
90             my $cast = $tv_maze->show_cast($show_id);
91              
92             Returns a list of main cast for a given show
93              
94             =head2 show_akas
95              
96             my $akas = $tv_maze->show_akas($show_id);
97              
98             Returns a list of AKA's for a show
99              
100             =head2 show_index
101              
102             my $index = $tv_maze->show_index($page); ## $page is optional, pagination starts on page 0
103              
104             Returns all TV Maze shows , 250 results per page
105              
106             =head2 episode_by_number
107              
108             my $ep = $tv_maze->episode_by_number($show_id, $season, $ep_number);
109              
110             Returns a show episode
111              
112             =head2 episodes_by_date
113              
114             my $eps = $tv_maze->episodes_by_date($show_id, $date);
115              
116             Returns a list of episodes for a given show aired on a given date
117              
118              
119             =head2 schedule
120              
121             my $schedule = $tv_maze->schedule($country_code, $date); # $country_code is an ISO 3166-1 code of the country, $date an ISO 8601 formatted date, both parameters are optional, defaults to 'US' and current day
122              
123             Returns a complete list of episodes for the date and country provided
124              
125              
126             =head2 full_schedule
127              
128             my $schedule = $tv_maze->full_schedule();
129              
130             Returns a list of all future episodes known to TVmaze
131              
132              
133             =head2 people_search
134              
135             my $people = $tv_maze->people_search($search_keyword);
136              
137             Returns a list of persons that match your search keyword
138              
139              
140             =head2 people
141              
142             my $people = $tv_maze->people($id);
143              
144             Returns a person by its ID
145              
146              
147             =head2 person_cast_credits
148              
149             my $person_credits = $tv_maze->person_cast_credits($person_id, $emded_show); # $embed_show is optional, can be 1 or 0;
150              
151             Returns alll show-level cast credits for a person
152              
153             =head2 person_crew_credits
154              
155             my $person_credits = $tv_maze->person_crew_credits($person_id, $emded_show); # $embed_show is optional, can be 1 or 0;
156              
157             Returns alll show-level crew credits for a person
158              
159             =head2 updates
160              
161             my $updates = $tv_maze->updates();
162              
163             Returns a list of all the shows in the database with a timestamp of when they were last updated
164              
165              
166             =head2 error
167             my $error = $tv_maze->error();
168              
169             Returns the last error
170              
171             =head2 http_status
172             my $http_status = $tv_maze->http_status();
173              
174             Returns the last HTTP status received
175             =cut
176              
177              
178             sub shows {
179 0     0 1   my ($self, $id) = @_;
180 0           shift @_;
181 0           validate_pos(@_, { type => SCALAR } );
182              
183 0           return $self->_request('shows/' . $id);
184             }
185              
186             sub show_search {
187 0     0 1   my ($self, $search) = @_;
188 0           shift @_;
189 0           validate_pos(@_ , { type => SCALAR } );
190 0           return $self->_request('search/shows?q=' . $search);
191             }
192              
193              
194             sub show_single_search {
195 0     0 1   my ($self, $search) = @_;
196 0           shift @_;
197 0           validate_pos(@_ , { type => SCALAR } );
198 0           return $self->_request('singlesearch/shows?q=' . $search);
199             }
200              
201              
202             sub show_lookup {
203 0     0 1   my ($self, $id, $id_type) = @_;
204 0           shift @_;
205 0           validate_pos (@_ , { type => SCALAR }, { type => SCALAR, regex => qr/^tvrage$|^thetvdb$/ } );
206 0           return $self->_request('lookup/shows?' . $id_type .'=' . $id);
207             }
208              
209              
210              
211             sub people_search {
212 0     0 1   my ($self, $search) = @_;
213 0           shift @_;
214 0           validate_pos(@_ , { type => SCALAR } );
215 0           return $self->_request('search/people?q=' . $search);
216             }
217              
218              
219              
220             sub schedule {
221 0     0 1   my ($self, $country_code, $date) = @_;
222 0           shift @_;
223 0           validate_pos(@_ , { type => SCALAR, optional => 1 , regex => qr/^\w{2}$/}, { type => SCALAR , optional => 1 , regex => qw/\d{4}-\d{2}-\d{2}$/ } );
224 0   0       $country_code ||= 'US';
225 0   0       $date ||= DateTime->now->ymd('-');
226 0           return $self->_request('schedule?country=' . $country_code . '&date=' .$date);
227             }
228              
229              
230             sub full_schedule {
231 0     0 1   my ($self) = @_;
232 0           return $self->_request('schedule/full');
233             }
234              
235              
236              
237              
238             sub show_episode_list {
239 0     0 1   my ($self, $tvmaze_id, $specials) = @_;
240 0           shift @_;
241 0           validate_pos(@_, { type => SCALAR }, { type => BOOLEAN , optional => 1 } );
242 0   0       $specials ||= 0;
243 0           return $self->_request('shows/' . $tvmaze_id .'/episodes?specials=' . $specials);
244             }
245              
246              
247             sub episode_by_number {
248 0     0 1   my ($self, $tvmaze_id, $season, $number) = @_;
249 0           shift @_;
250 0           validate_pos(@_, { type => SCALAR }, { type => SCALAR , optional => 1, regex => qr/^\d+$/ }, { type => SCALAR , optional => 1, regex => qr/^\d+$/ } );
251 0           $season |= 1;
252 0           $number |= 1;
253 0           return $self->_request('shows/' . $tvmaze_id . '/episodebynumber?season=' . $season .'&number=' . $number);
254             }
255              
256              
257             sub episodes_by_date {
258 0     0 1   my ($self, $tvmaze_id, $date) = @_;
259              
260 0 0         if ($date !~/^\d{4}-\d{2}-\d{2}$/) {
261 0           $self->error('invalid date');
262 0           return undef;
263             }
264 0           return $self->_request('shows/' . $tvmaze_id .'/episodesbydate?date=' . $date);
265             }
266              
267              
268              
269              
270             sub show_cast {
271 0     0 1   my ($self, $tvmaze_id) = @_;
272 0           return $self->_request('shows/' . $tvmaze_id .'/cast');
273             }
274              
275              
276             sub show_akas {
277 0     0 1   my ($self, $tvmaze_id) = @_;
278 0           return $self->_request('shows/' . $tvmaze_id .'/akas');
279             }
280              
281              
282              
283              
284             sub show_index {
285 0     0 1   my ($self, $page) = @_;
286 0   0       $page ||= 0;
287 0           return $self->_request('shows?page=' . $page);
288             }
289              
290              
291              
292             sub people {
293 0     0 1   my ($self, $tvmaze_id) = @_;
294 0           return $self->_request('people/' . $tvmaze_id);
295             }
296              
297              
298              
299             sub person_cast_credits {
300 0     0 1   my ($self, $tvmaze_id, $embed) = @_;
301 0 0         return $self->_request('people/' . $tvmaze_id .'/castcredits' . ( $embed ? '?embed=show' : '' ));
302             }
303              
304              
305             sub person_crew_credits {
306 0     0 1   my ($self, $tvmaze_id, $embed) = @_;
307 0 0         return $self->_request('people/' . $tvmaze_id .'/crewcredits' . ( $embed ? '?embed=show' : '' ));
308             }
309              
310              
311              
312             sub updates {
313 0     0 1   my ($self) = @_;
314 0           return $self->_request('updates/shows');
315             }
316              
317              
318              
319              
320              
321             sub _request {
322 0     0     my ($self, $uri) = @_;
323              
324 0           my $ua = LWP::UserAgent->new();
325 0           my $url = $self->_endpoint .'/' . $uri;
326 0           my $response = $ua->get($url);
327              
328 0           $self->http_status($response->code);
329              
330 0 0         if (!$response->is_success) {
331 0           $self->error('request error');
332 0           return {};
333             }
334              
335 0           my $data = {};
336 0           eval {
337 0           $data = decode_json($response->decoded_content);
338             };
339 0 0         if ($@) {
340 0           $self->error('problem decoding json');
341 0           return undef;
342             }
343 0           return $data;
344             }
345              
346              
347              
348              
349              
350              
351              
352             =head1 AUTHOR
353              
354             Bruno Martins, C<< >>
355              
356             =head1 BUGS
357              
358             Please report any bugs or feature requests at L
359              
360              
361              
362              
363             =head1 SUPPORT
364              
365             You can find documentation for this module with the perldoc command.
366              
367             perldoc WWW::TVMaze
368              
369              
370              
371             =head1 LICENSE AND COPYRIGHT
372              
373             Copyright 2015 Bruno Martins.
374              
375             This program is free software; you can redistribute it and/or modify it
376             under the terms of either: the GNU General Public License as published
377             by the Free Software Foundation; or the Artistic License.
378              
379             See http://dev.perl.org/licenses/ for more information.
380              
381              
382             =cut
383              
384             1; # End of WWW::TVMaze