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   21063 use 5.006;
  1         5  
4 1     1   5 use strict;
  1         2  
  1         21  
5 1     1   27 use warnings;
  1         6  
  1         33  
6 1     1   752 use Mouse;
  1         32176  
  1         5  
7 1     1   17501 use LWP::UserAgent;
  1         78633  
  1         37  
8 1     1   1059 use JSON::XS;
  1         13804  
  1         87  
9 1     1   1393 use DateTime;
  1         122953  
  1         43  
10 1     1   10 use Params::Validate qw(:all);
  1         2  
  1         1546  
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.03';
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              
168             my $error = $tv_maze->error();
169              
170             Returns the last error
171              
172             =head2 http_status
173              
174             my $http_status = $tv_maze->http_status();
175              
176             Returns the last HTTP status received
177             =cut
178              
179              
180             sub shows {
181 0     0 1   my ($self, $id) = @_;
182 0           shift @_;
183 0           validate_pos(@_, { type => SCALAR } );
184              
185 0           return $self->_request('shows/' . $id);
186             }
187              
188             sub show_search {
189 0     0 1   my ($self, $search) = @_;
190 0           shift @_;
191 0           validate_pos(@_ , { type => SCALAR } );
192 0           return $self->_request('search/shows?q=' . $search);
193             }
194              
195              
196             sub show_single_search {
197 0     0 1   my ($self, $search) = @_;
198 0           shift @_;
199 0           validate_pos(@_ , { type => SCALAR } );
200 0           return $self->_request('singlesearch/shows?q=' . $search);
201             }
202              
203              
204             sub show_lookup {
205 0     0 1   my ($self, $id, $id_type) = @_;
206 0           shift @_;
207 0           validate_pos (@_ , { type => SCALAR }, { type => SCALAR, regex => qr/^tvrage$|^thetvdb$/ } );
208 0           return $self->_request('lookup/shows?' . $id_type .'=' . $id);
209             }
210              
211              
212              
213             sub people_search {
214 0     0 1   my ($self, $search) = @_;
215 0           shift @_;
216 0           validate_pos(@_ , { type => SCALAR } );
217 0           return $self->_request('search/people?q=' . $search);
218             }
219              
220              
221              
222             sub schedule {
223 0     0 1   my ($self, $country_code, $date) = @_;
224 0           shift @_;
225 0           validate_pos(@_ , { type => SCALAR, optional => 1 , regex => qr/^\w{2}$/}, { type => SCALAR , optional => 1 , regex => qw/\d{4}-\d{2}-\d{2}$/ } );
226 0   0       $country_code ||= 'US';
227 0   0       $date ||= DateTime->now->ymd('-');
228 0           return $self->_request('schedule?country=' . $country_code . '&date=' .$date);
229             }
230              
231              
232             sub full_schedule {
233 0     0 1   my ($self) = @_;
234 0           return $self->_request('schedule/full');
235             }
236              
237              
238              
239              
240             sub show_episode_list {
241 0     0 1   my ($self, $tvmaze_id, $specials) = @_;
242 0           shift @_;
243 0           validate_pos(@_, { type => SCALAR }, { type => BOOLEAN , optional => 1 } );
244 0   0       $specials ||= 0;
245 0           return $self->_request('shows/' . $tvmaze_id .'/episodes?specials=' . $specials);
246             }
247              
248              
249             sub episode_by_number {
250 0     0 1   my ($self, $tvmaze_id, $season, $number) = @_;
251 0           shift @_;
252 0           validate_pos(@_, { type => SCALAR }, { type => SCALAR , optional => 1, regex => qr/^\d+$/ }, { type => SCALAR , optional => 1, regex => qr/^\d+$/ } );
253 0           $season |= 1;
254 0           $number |= 1;
255 0           return $self->_request('shows/' . $tvmaze_id . '/episodebynumber?season=' . $season .'&number=' . $number);
256             }
257              
258              
259             sub episodes_by_date {
260 0     0 1   my ($self, $tvmaze_id, $date) = @_;
261              
262 0 0         if ($date !~/^\d{4}-\d{2}-\d{2}$/) {
263 0           $self->error('invalid date');
264 0           return undef;
265             }
266 0           return $self->_request('shows/' . $tvmaze_id .'/episodesbydate?date=' . $date);
267             }
268              
269              
270              
271              
272             sub show_cast {
273 0     0 1   my ($self, $tvmaze_id) = @_;
274 0           return $self->_request('shows/' . $tvmaze_id .'/cast');
275             }
276              
277              
278             sub show_akas {
279 0     0 1   my ($self, $tvmaze_id) = @_;
280 0           return $self->_request('shows/' . $tvmaze_id .'/akas');
281             }
282              
283              
284              
285              
286             sub show_index {
287 0     0 1   my ($self, $page) = @_;
288 0   0       $page ||= 0;
289 0           return $self->_request('shows?page=' . $page);
290             }
291              
292              
293              
294             sub people {
295 0     0 1   my ($self, $tvmaze_id) = @_;
296 0           return $self->_request('people/' . $tvmaze_id);
297             }
298              
299              
300              
301             sub person_cast_credits {
302 0     0 1   my ($self, $tvmaze_id, $embed) = @_;
303 0 0         return $self->_request('people/' . $tvmaze_id .'/castcredits' . ( $embed ? '?embed=show' : '' ));
304             }
305              
306              
307             sub person_crew_credits {
308 0     0 1   my ($self, $tvmaze_id, $embed) = @_;
309 0 0         return $self->_request('people/' . $tvmaze_id .'/crewcredits' . ( $embed ? '?embed=show' : '' ));
310             }
311              
312              
313              
314             sub updates {
315 0     0 1   my ($self) = @_;
316 0           return $self->_request('updates/shows');
317             }
318              
319              
320              
321              
322              
323             sub _request {
324 0     0     my ($self, $uri) = @_;
325              
326 0           my $ua = LWP::UserAgent->new();
327 0           my $url = $self->_endpoint .'/' . $uri;
328 0           my $response = $ua->get($url);
329              
330 0           $self->http_status($response->code);
331              
332 0 0         if (!$response->is_success) {
333 0           $self->error('request error');
334 0           return {};
335             }
336              
337 0           my $data = {};
338 0           eval {
339 0           $data = decode_json($response->decoded_content);
340             };
341 0 0         if ($@) {
342 0           $self->error('problem decoding json');
343 0           return undef;
344             }
345 0           return $data;
346             }
347              
348              
349              
350              
351              
352              
353              
354             =head1 AUTHOR
355              
356             Bruno Martins, C<< >>
357              
358             =head1 BUGS
359              
360             Please report any bugs or feature requests at L
361              
362              
363              
364              
365             =head1 SUPPORT
366              
367             You can find documentation for this module with the perldoc command.
368              
369             perldoc WWW::TVMaze
370              
371              
372              
373             =head1 LICENSE AND COPYRIGHT
374              
375             Copyright 2015 Bruno Martins.
376              
377             This program is free software; you can redistribute it and/or modify it
378             under the terms of either: the GNU General Public License as published
379             by the Free Software Foundation; or the Artistic License.
380              
381             See http://dev.perl.org/licenses/ for more information.
382              
383              
384             =cut
385              
386             1; # End of WWW::TVMaze