File Coverage

blib/lib/WWW/TVMaze.pm
Criterion Covered Total %
statement 23 101 22.7
branch 0 10 0.0
condition 0 13 0.0
subroutine 8 27 29.6
pod 18 18 100.0
total 49 169 28.9


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