TV/Series.pm

filter: Statementstmt 65.4% 87 / 133 Branchbran 44.8% 26 / 58 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 83.3% 15 / 18 Podpod 100% 12 / 12 totaltotal 60.4% 147 / 243 CC 38 SCAR 22.7
CC
38
Cov
58%
cast
30.0
site
17.5
new
13.4
CRAP 9.7
↑ Summary TV/Episode.pm »
1 =head1 NAME
2  
3 WWW::TV::Series - Parse TV.com for TV Series information.
4  
5 =head1 SYNOPSIS
6  
7 use WWW::TV::Series qw();
8 my $series = WWW::TV::Series->new(name => 'Prison Break');
9  
10 my @episodes = $series->episodes;
11 print $series->summary;
12  
13 =head1 DESCRIPTION
14  
15 The L<WWW::TV::Series> module parses TV.com series information using
16 L<LWP::UserAgent>.
17  
18 =head1 METHODS
19  
20 =cut
21  
22 package WWW::TV::Series;
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '0.14';
27  
28 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use LWP::UserAgent qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head2 new
32  
33 The new() method is the constructor. It takes the id of the show if
34 you have previously looked that up, or the name of the show which
35 will be used to perform a search and the id will be taken from the
36 first result.
37  
38 Optional parameters let you set the season number or LWP user agent.
39  
40 # default usage
41 my $series = WWW::TV::Series->new(name => 'Prison Break');
42 my $series = WWW::TV::Series->new(id => 31635);
43  
44 # change user-agent from the default of "libwww-perl/#.##"
45 my $series = WWW::TV::Series->new(id => 31635, agent => 'WWW::TV');
46  
47 It is recommended that you lookup the show first and use the ID,
48 otherwise you just don't know what will be returned.
49  
50 The constructor also takes a single scalar as an argument and does
51 it's best to figure out what you want. But due to some shows being
52 all digits as a name (e.g. "24"), use of this is not recommended
53 (and in future may be deprecated).
54  
55 =cut
56  
57 2 ▶sub new {
Subroutine new: called CC=9 SCAR=26.0
Pod documented
58 2 ▶ my $class = ref $_[0] ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
59  
60 2 my %data;
61  
62 2 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
elsif (scalar(@_) % 2 == 0) { } 2 0 2
63 # If they gave us a plain scalar argument, try our best to figure out
64 # what it is. Of course this dies in the arse if you want to search
65 # for a program with a name like '24'.
66 0 ▶ if ($_[0] =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\d+$/) { } 0 0 0
67 0 $data{id} = shift;
68 }
69 else {
70 0 $data{name} = shift;
71 }
72 }
73 elsif (scalar(@_) % 2 == 0) {
74 2 %data = @_;
75 }
76  
77 2 $data{agent} = $class->agent($data{agent});
78 2 $data{site} = $class->site ($data{site});
79
80 $data{id} = $class->_get_first_search_result($data{name}, $data{agent}, $data{site})
81 2 ▶ if exists $data{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data{"name"} 1 1 2
82  
83 1 ▶ croak 'No id or name given to constructor' unless exists $data{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data{"id"} 0 1 1
84 1 ▶ croak "Invalid id: $data{id}" unless $data{id} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"id"} =~ /^\d+$/ 0 1 1
85  
86 return bless {
87 id => $data{id},
88 _season => $data{season} || 0,
89 _agent => $data{agent},
90 _site => $data{site},
91 1 ▶ filled => { id => 1 },
Condition50%1/2
$data{'season'} || 0
l!l
01
MC/DC0%0/1
$data{'season'} || 0
$data{'season'}
Truth table50%1/2
A
A= $data{'season'}
Aresult
11
00
92 }, $class;
93 }
94  
95 1 ▶sub _get_first_search_result {
Subroutine _get_first_search_result: called CC=4 SCAR=19.7
96 1 my ($class, $name, $agent, $site) = @_;
97  
98 1 my $ua = LWP::UserAgent->new( agent => $agent );
99 1 my $rc = $ua->get(
100 sprintf("http://%s.tv.com/search.php?type=Search&stype=ajax_search&search_type=program&qs=%s",
101 $site, $name)
102 );
103 1 ▶ croak "Unable to get search results for $name" unless $rc->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc->is_success 1 0 1
104  
105 0 for (split /\n/, $rc->content) {
106 0 ▶ next unless m{<a href="http://\w+.tv.com/.*?show/(\d+)/summary.html};
Branch: 1 branch
BranchTrueFalseTotal
unless m[<a href="http://\w+.tv.com/.*?show/(\d+)/summary.html] 0 0 0
107 0 return $1;
108 }
109 0 croak 'Unable to find a show in the search results.';
110 }
111  
112 =head2 summary
113  
114 Returns a string containing basic information about this series.
115  
116 =cut
117  
118 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
119 0 my $self = shift;
120  
121 0 ▶ unless (exists $self->{filled}->{summary}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filled"}{"summary"}) 0 0 0
122 0 ($self->{summary}) = $self->_html =~ m{
123 <span\sclass="long">(.*?)</span>
124 }smx;
125 0 $self->{summary} =~ s/<br ?\/?>/\n/g;
126 0 $self->{summary} =~ s/<a href="[^"]+">.*?<\/a>//g;
127 0 $self->{summary} =~ s/^\s*//;
128 0 $self->{summary} =~ s/\s*$//;
129 0 $self->{filled}->{summary} = 1;
130 }
131  
132 0 return $self->{summary};
133 }
134  
135 =head2 genres
136  
137 Returns a list of all the genres that TV.com have categorised this series as.
138  
139 # in scalar context, returns a comma-delimited string
140 my $genres = $series->genres;
141  
142 # in array context, returns an array
143 my @genres = $series->genres;
144  
145 =cut
146  
147 2 ▶sub genres {
Subroutine genres: called CC=3 SCAR=11.0
Pod documented
148 2 my $self = shift;
149  
150 2 ▶ unless (exists $self->{filled}->{genres}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filled"}{"genres"}) 1 1 2
151 1 my ($genres_row) = $self->_html =~ m{
152 <span\sclass="genres">(<a\shref=.*</a>)</span>
153 }x;
154  
155 $self->{genres} =
156 join(
157 ', ',
158 0 map { s/\s*<a href="[^"]+">(.*?)<\/a>\s*/$1/; $_ }
159 split(/,/, $genres_row)
160 );
161  
162 1 my @genres = split(/, /, $self->{genres});
163 1 $self->{genres} = \@genres;
164 1 $self->{filled}->{genres} = 1;
165 }
166  
167 2 ▶ return wantarray ? @{$self->{genres}} : join(', ', @{$self->{genres}});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
168 }
169  
170 =head2 cast
171  
172 Returns a list of the cast members. The order is the same as they
173 appear on TV.com, which is most likely nothing to go by, but
174 in most cases is the main cast order.
175  
176 # in scalar context, returns a comma-delimited string
177 my $cast = $series->cast;
178  
179 # in array context, returns an array
180 my @cast = $series->cast;
181  
182 =cut
183  
184 0 ▶sub cast {
Subroutine cast: not called CC=5 SCAR=34.0
Pod documented
185 0 my $self = shift;
186  
187 0 ▶ unless (exists $self->{filled}->{cast}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filled"}{"cast"}) 0 0 0
188 0 my ($cast_line) = $self->_html =~ m{<ul><li\s* ><div class="wrap"><span class="cast_member">(.*?)</ul>};
189 0 my @cast;
190 0 for my $person (split /<\/li>/, $cast_line) {
191 0 ▶ next unless $person =~ m{<a href="[^"]+">(.*?)</a>};
Branch: 1 branch
BranchTrueFalseTotal
unless $person =~ m[<a href="[^"]+">(.*?)</a>] 0 0 0
192 0 push @cast, $1;
193 }
194 0 $self->{cast} = \@cast;
195 0 $self->{filled}->{cast} = 1;
196 }
197  
198 0 ▶ return wantarray ? @{$self->{cast}} : join(', ', @{$self->{cast}});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
199 }
200  
201 =head2 name
202  
203 Returns a string containing the name of the series.
204  
205 =cut
206  
207 1 ▶sub name {
Subroutine name: called CC=2 SCAR=7.0
Pod documented
208 1 my $self = shift;
209  
210 1 ▶ unless (exists $self->{filled}->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filled"}{"name"}) 1 0 1
211 1 ($self->{name}) = $self->_html =~ m{
212 <div\sclass="content_title".*?>\n\n?
213 <h1>(.*?):\s*<span>Summary</span></h1>\n
214 }x;
215 1 $self->{filled}->{name} = 1;
216 }
217  
218 1 return $self->{name};
219 }
220  
221 =head2 image
222  
223 Returns the url of an image that can be used to identify this series.
224  
225 =cut
226  
227 0 ▶sub image {
Subroutine image: not called CC=2 SCAR=17.9
Pod documented
228 0 my $self = shift;
229  
230 0 ▶ unless (exists $self->{filled}->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filled"}{"image"}) 0 0 0
231 0 ($self->{image}) = $self->_html =~ m{
232 <div\sid="topslot">\s*\n
233 (?:<a\shref="[^"]+">\n)?
234 \s*<img\ssrc="([^"]+)"
235 }x;
236 0 $self->{filled}->{image} = 1;
237 }
238  
239 0 return $self->{image};
240 }
241  
242 =head2 episodes
243  
244 Returns an array of L<WWW::TV::Episode> objects in order.
245  
246 # All episodes
247 my @episodes = $series->episodes;
248  
249 # Episodes for season 2 only
250 my @episodes = $series->episodes( season => 2 );
251  
252 =cut
253  
254 4 ▶sub episodes {
Subroutine episodes: called CC=6 SCAR=19.3
Pod documented
255 4 my $self = shift;
256  
257 4 my %args;
258 4 ▶ if (scalar(@_) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2 == 0) 4 0 4
259 4 %args = @_;
260 }
261  
262 4 ▶ my $season = exists $args{season} ? $args{season} : $self->{_season};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'season'} ? : 3 1 4
263  
264 4 ▶ unless ($self->{filled}->{episodes}->{$season}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"filled"}{"episodes"}{$season}) 3 1 4
265 3 my $ua = LWP::UserAgent->new(agent => $self->agent);
266 3 my $rc = $ua->get($self->episode_url($season));
267 3 ▶ croak sprintf('Unable to fetch episodes for series %d, season %d', $self->id, $season)
Branch: 1 branch
BranchTrueFalseTotal
unless $rc->is_success 0 3 3
268 unless $rc->is_success;
269  
270 3 require WWW::TV::Episode;
271 3 my ($episode_line) = $rc->content =~ m{<th class="ep_title">(.*)\n};
272 my @episodes =
273 0 grep { defined }
274 map {
275 0 my $ep;
276 0 ▶ if (m#<a href=".*/episode/(\d+)/summary\.html[^"]*">(.*?)</a>#) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[<a href=".*/episode/(\d+)/summary\.html[^"]*">(.*?)</a>]) 0 0 0
277 0 $ep = WWW::TV::Episode->new(id => $1, name => $2, agent => $self->agent);
278 }
279 0 $ep;
280 } split /<\/div>/, $episode_line;
281  
282 3 $self->{episodes}->{$season} = \@episodes;
283 3 $self->{filled}->{episodes}->{$season} = 1;
284 }
285  
286 4 return @{$self->{episodes}->{$season}};
287 }
288  
289 2 ▶sub _html {
Subroutine _html: called CC=3 SCAR=11.0
290 2 my $self = shift;
291  
292 2 ▶ unless ($self->{filled}->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"filled"}{"html"}) 1 1 2
293 1 my $ua = LWP::UserAgent->new (agent => $self->agent);
294 1 my $rc = $ua->get($self->url);
295 1 ▶ croak sprintf('Unable to fetch page for series %s', $self->id)
Branch: 1 branch
BranchTrueFalseTotal
unless $rc->is_success 0 1 1
296 unless $rc->is_success;
297  
298 $self->{html} =
299 join(
300 "\n",
301 1 map { s/^\s*//; s/\s*$//; $_ }
302 split /\n/, $rc->content
303 );
304  
305 1 $self->{filled}->{html} = 1;
306 }
307  
308 2 return $self->{html};
309 }
310  
311 =head2 id
312  
313 The ID of this series, according to TV.com
314  
315 =cut
316  
317 5 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
318 5 my $self = shift;
319  
320 5 return $self->{id};
321 }
322  
323 =head2 agent ($value)
324  
325 Returns the current user agent setting, and sets to $value if provided.
326  
327 =cut
328  
329 6 ▶sub agent {
Subroutine agent: called CC=5 SCAR=18.1
Pod documented
330 6 my $self = shift; # may be called as $self or $class
331 6 my $value = shift;
332  
333 6 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 4 2 6
334 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 4 4
335 0 $self->{_agent} = $value;
336 }
337 4 ▶ return ($self->{_agent} || LWP::UserAgent::_agent);
Condition33%1/3
$self->{'_agent'} || LWP::UserAgent::_agent()
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{'_agent'} || LWP::UserAgent::_agent()
$self->{'_agent'} LWP::UserAgent::_agent()
Truth table33%1/3
A || B
A= $self->{'_agent'}B= LWP::UserAgent::_agent()
ABresult
1X1
011
000
338 } else {
339 2 ▶ return ($value || LWP::UserAgent::_agent);
Condition33%1/3
$value || LWP::UserAgent::_agent()
l!l&&r!l&&!r
020
MC/DC0%0/2
$value || LWP::UserAgent::_agent()
$value LWP::UserAgent::_agent()
Truth table33%1/3
A || B
A= $valueB= LWP::UserAgent::_agent()
ABresult
1X1
011
000
340 }
341 }
342  
343 =head2 site ($value)
344  
345 Returns the current mirror site setting, and sets to $value if provided.
346  
347 Default site is "www"; other options include: us, uk, au
348  
349 =cut
350  
351 2 ▶sub site {
Subroutine site: called CC=6 SCAR=28.6
Pod documented
352 2 my $self = shift; # may be called as $self or $class
353 2 my $value = shift;
354  
355 2 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 2 2
356 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
357 0 ▶ if ($value =~ m#^(au|uk|us|www|)$#i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(au|uk|us|www|)$/i) { } 0 0 0
358 0 $self->{_site} = $value;
359 } else {
360 0 warn "Ignoring unknown site value: [$value]\n";
361 }
362 }
363 0 ▶ return ($self->{_site} || 'www');
Condition0%0/2
$self->{'_site'} || 'www'
l!l
00
MC/DC0%0/1
$self->{'_site'} || 'www'
$self->{'_site'}
Truth table0%0/2
A
A= $self->{'_site'}
Aresult
11
00
364 } else {
365 2 ▶ return ($value || 'www');
Condition50%1/2
$value || 'www'
l!l
02
MC/DC0%0/1
$value || 'www'
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
366 }
367 }
368  
369 =head2 url
370  
371 Returns the url that was used to create this object.
372  
373 =cut
374  
375 2 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
376 2 my $self = shift;
377  
378 2 return sprintf('http://%s.tv.com/show/%d/summary.html', $self->{_site}, $self->id);
379 }
380  
381 =head2 episode_url ($season)
382  
383 Returns the url that is used to get the episode listings for this
384 series.
385  
386 $season is optional ; defaults to "all"
387  
388 =cut
389  
390 3 ▶sub episode_url {
Subroutine episode_url: called CC=2 SCAR=6.9
Pod documented
391 3 my $self = shift;
392 3 ▶ my $season = shift || 'All'; # 0 == ALL seasons
Condition100%2/2
shift() || 'All'
l!l
21
MC/DC100%1/1
shift() || 'All'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
393  
394 return sprintf(
395 'http://%s.tv.com/show/%d/episode_listings.html?season=%s',
396 3 $self->{_site}, $self->id, $season
397 );
398 }
399  
400 1 1;
401  
402 __END__
↑ Summary TV/Episode.pm »