Persons.pm

filter: Statementstmt 20.0% 34 / 170 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 40.7% 11 / 27 Podpod 80.0% 8 / 10 totaltotal 15.7% 53 / 337 CC 53 SCAR 43.0
CC
53
Cov
12%
date_of_birth
156.0
filmography
156.0
name
110.0
CRAP 73.6
« Film.pm ↑ Summary BaseClass.pm »
1 =head1 NAME
2  
3 IMDB::Persons - Perl extension for retrieving movies persons
4 from IMDB.com
5  
6 =head1 SYNOPSIS
7  
8 use IMDB::Persons;
9  
10 #
11 # Retrieve a person information by IMDB code
12 #
13 my $person = new IMDB::Persons(crit => '0000129');
14  
15 or
16  
17 #
18 # Retrieve a person information by name
19 #
20 my $person = new IMDB::Persons(crit => 'Tom Cruise');
21  
22 or
23  
24 #
25 # Process already stored HTML page from IMDB
26 #
27 my $person = new IMDB::Persons(file => 'imdb.html');
28  
29 if($person->status) {
30 print "Name: ".$person->name."\n";
31 print "Birth Date: ".$person->date_of_birth."\n";
32 } else {
33 print "Something wrong: ".$person->error."!\n";
34 }
35  
36 =head1 DESCRIPTION
37  
38 IMDB::Persons allows to retrieve an information about
39 IMDB persons (actors, actresses, directors etc): full name,
40 photo, date and place of birth, mini bio and filmography.
41  
42 =cut
43  
44 package IMDB::Persons;
45  
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use base qw(IMDB::BaseClass);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 ▶use fields qw( _name
Subroutine BEGIN: called CC=1 SCAR=0.0
56 _date_of_birth
57 _place_of_birth
58 _photo
59 _mini_bio
60 _filmography_types
61 _filmography
62 _genres
63 _plot_keywords
64 1 );
65  
66 1 ▶use vars qw($VERSION %FIELDS);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 ▶use constant FORCED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant CLASS_NAME => 'IMDB::Persons';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use constant MAIN_TAG => 'h4';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 $VERSION = '0.53';
74 }
75  
76 {
77 1 my %_defaults = (
78 cache => 0,
79 debug => 0,
80 error => [],
81 matched => [],
82 cache_exp => '1 h',
83 host => 'www.imdb.com',
84 query => 'name/nm',
85 search => 'find?nm=on&mx=20&q=',
86 status => 0,
87 timeout => 10,
88 user_agent => 'Mozilla/5.0',
89 );
90  
91 0 ▶ sub _get_default_attrs { keys %_defaults }
Subroutine _get_default_attrs: not called CC=1 SCAR=6.9
92 0 ▶ sub _get_default_value {
Subroutine _get_default_value: not called CC=1 SCAR=6.9
93 0 my($self, $attr) = @_;
94 0 $_defaults{$attr};
95 }
96 }
97  
98 =head1 Object Private Methods
99  
100 =over 4
101  
102 =item _init()
103  
104 Initialize a new object.
105  
106 =cut
107  
108 1 ▶sub _init {
Subroutine _init: not called CC=5 SCAR=32.6
109 0 my CLASS_NAME $self = shift;
110 0 my %args = @_;
111  
112 0 ▶ croak "Person IMDB ID or Name should be defined!" if !$args{crit} && !$args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"crit"} or $args{"file"} 0 0 0
Condition0%0/3
$args{"crit"} or $args{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"crit"} or $args{"file"}
$args{"crit"} $args{"file"}
Truth table0%0/3
A or B
A= $args{"crit"}B= $args{"file"}
ABresult
1X1
011
000
113  
114 0 $self->SUPER::_init(%args);
115 0 my $name = $self->name();
116
117 0 ▶ for my $prop (grep { /^_/ && !/^_name$/ } sort keys %FIELDS) {
Condition0%0/3
/^_/ && !/^_name$/
!ll&&!rl&&r
000
MC/DC0%0/2
/^_/ && !/^_name$/
/^_/ !/^_name$/
Truth table0%0/3
A && not(B)
A= /^_/B= !/^_name$/
ABresult
0X0
100
111
118 0 ($prop) = $prop =~ /^_(.*)/;
119 0 $self->$prop();
120 }
121 }
122  
123 =item _search_person()
124  
125 Implements a logic to search IMDB persons by their names.
126  
127 =cut
128  
129 0 ▶sub _search_person {
Subroutine _search_person: not called CC=1 SCAR=6.9
130 0 my CLASS_NAME $self = shift;
131  
132 0 return $self->SUPER::_search_results('\/name\/nm(\d+)', '/a');
133 }
134  
135 0 ▶sub fields {
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my CLASS_NAME $self = shift;
137 0 return \%FIELDS;
138 }
139  
140  
141 =back
142  
143 =head1 Object Public Methods
144  
145 =over 4
146  
147 =item name()
148  
149 Retrieve a person full name
150  
151 my $person_name = $person->name();
152  
153 =cut
154  
155 0 ▶sub name {
Subroutine name: not called CC=10 SCAR=47.0
Pod documented
156 0 my CLASS_NAME $self = shift;
157 0 ▶ if(!defined $self->{'_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_name"}) 0 0 0
158 0 my $parser = $self->_parser(FORCED);
159  
160 0 $parser->get_tag('title');
161 0 my $title = $parser->get_text();
162 0 $title =~ s#\s*\-\s*IMDB##i;
163  
164 0 $self->_show_message("Title=$title", 'DEBUG');
165
166 # Check if we have some search results
167 0 my $no_matches = 1;
168 0 ▶ while(my $tag = $parser->get_tag('td')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("td")) 0 0 0
169 0 ▶ if($tag->[1]->{class} && $tag->[1]->{class} eq 'media_strip_header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->[1]{"class"} and $tag->[1]{"class"} eq "media_strip_header") 0 0 0
Condition0%0/3
$tag->[1]{"class"} and $tag->[1]{"class"} eq "media_strip_header"
!ll&&!rl&&r
000
MC/DC0%0/2
$tag->[1]{"class"} and $tag->[1]{"class"} eq "media_strip_header"
$tag->[1]{"class"} $tag->[1]{"class"} eq "media_strip_header"
Truth table0%0/3
A and B
A= $tag->[1]{"class"}B= $tag->[1]{"class"} eq "media_strip_header"
ABresult
0X0
100
111
170 0 $no_matches = 0;
171 0 last;
172 }
173 }
174  
175 0 ▶ if($title =~ /imdb\s+name\s+search/i && !$no_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /imdb\s+name\s+search/i and not $no_matches) 0 0 0
Condition0%0/3
$title =~ /imdb\s+name\s+search/i and not $no_matches
!ll&&!rl&&r
000
MC/DC0%0/2
$title =~ /imdb\s+name\s+search/i and not $no_matches
$title =~ /imdb\s+name\s+search/i not $no_matches
Truth table0%0/3
A and not(B)
A= $title =~ /imdb\s+name\s+search/iB= not $no_matches
ABresult
0X0
100
111
176 0 $self->_show_message("Go to search page ...", 'DEBUG');
177 0 $title = $self->_search_person();
178 }
179
180 0 ▶ $title = '' if $title =~ /IMDb Name Search/i;
Branch: 1 branch
BranchTrueFalseTotal
if $title =~ /IMDb Name Search/i 0 0 0
181 0 ▶ if($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) { } 0 0 0
182 0 $self->status(1);
183 0 ▶ $self->retrieve_code($parser, 'http://www.imdb.com/name/nm(\d+)') unless $self->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->code 0 0 0
184 } else {
185 0 $self->status(0);
186 0 $self->error('Not Found');
187 }
188  
189 0 $title =~ s/^imdb\s+\-\s+//i;
190 0 $self->{'_name'} = $title;
191 }
192  
193 0 return $self->{'_name'};
194 }
195  
196 =item mini_bio()
197  
198 Returns a mini bio for specified IMDB person
199  
200 my $mini_bio = $person->mini_bio();
201  
202 =cut
203  
204 0 ▶sub mini_bio {
Subroutine mini_bio: not called CC=5 SCAR=34.0
Pod documented
205 0 my CLASS_NAME $self = shift;
206 0 ▶ if(!defined $self->{_mini_bio}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_mini_bio"}) 0 0 0
207 0 my $parser = $self->_parser(FORCED);
208 0 ▶ while(my $tag = $parser->get_tag('div') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("div")) 0 0 0
209 0 ▶ last if $tag->[1]->{class} && $tag->[1]->{class} eq 'infobar';
Branch: 1 branch
BranchTrueFalseTotal
if $tag->[1]{"class"} and $tag->[1]{"class"} eq "infobar" 0 0 0
Condition0%0/3
$tag->[1]{"class"} and $tag->[1]{"class"} eq "infobar"
!ll&&!rl&&r
000
MC/DC0%0/2
$tag->[1]{"class"} and $tag->[1]{"class"} eq "infobar"
$tag->[1]{"class"} $tag->[1]{"class"} eq "infobar"
Truth table0%0/3
A and B
A= $tag->[1]{"class"}B= $tag->[1]{"class"} eq "infobar"
ABresult
0X0
100
111
210 }
211
212 0 my $tag = $parser->get_tag('p');
213 0 $self->{'_mini_bio'} = $parser->get_trimmed_text('a');
214 }
215 0 return $self->{'_mini_bio'};
216 }
217  
218 =item date_of_birth()
219  
220 Returns a date of birth of IMDB person in format 'day' 'month caption' 'year':
221  
222 my $d_birth = $person->date_of_birth();
223  
224 =cut
225  
226 #TODO: add date convertion in different formats.
227 0 ▶sub date_of_birth {
Subroutine date_of_birth: not called CC=12 SCAR=50.5
Pod documented
228 0 my CLASS_NAME $self = shift;
229 0 ▶ if(!defined $self->{'_date_of_birth'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_date_of_birth"}) 0 0 0
230 0 my $parser = $self->_parser(FORCED);
231 0 ▶ while(my $tag = $parser->get_tag(MAIN_TAG)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("h4")) 0 0 0
232 0 my $text = $parser->get_text;
233 0 ▶ last if $text =~ /^Born/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^Born/i 0 0 0
234 }
235  
236 0 my $date = '';
237 0 my $year = '';
238 0 my $place = '';
239 0 ▶ while(my $tag = $parser->get_tag()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag) 0 0 0
240 0 ▶ last if $tag->[0] eq '/td';
Branch: 1 branch
BranchTrueFalseTotal
if $tag->[0] eq "/td" 0 0 0
241
242 0 ▶ if($tag->[0] eq 'a') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->[0] eq "a") 0 0 0
243 0 my $text = $parser->get_text();
244 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
245  
246 0 SWITCH: for($tag->[1]->{href}) {
247 0 ▶ /birth_monthday/i && do { $date = $text; $date =~ s#(\w+)\s(\d+)#$2 $1#; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
/birth_monthday/i and do { $date = $text; $date =~ s/(\w+)\s(\d+)/$2 $1/; last SWITCH } 0 0 0
248 0 ▶ /birth_year/i && do { $year = $text; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
/birth_year/i and do { $year = $text; last SWITCH } 0 0 0
249 0 ▶ /birth_place/i && do { $place = $text; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
/birth_place/i and do { $place = $text; last SWITCH } 0 0 0
250 }
251 }
252 }
253  
254 0 $self->{'_date_of_birth'} = {date => "$date $year", place => $place};
255 }
256  
257 0 return $self->{'_date_of_birth'}{'date'};
258 }
259  
260 =item place_of_birth()
261  
262 Returns a name of place of the birth
263  
264 my $place = $person->place_of_birth();
265  
266 =cut
267  
268 0 ▶sub place_of_birth {
Subroutine place_of_birth: not called CC=1 SCAR=6.9
Pod documented
269 0 my CLASS_NAME $self = shift;
270 0 return $self->{'_date_of_birth'}{'place'};
271 }
272  
273 =item photo()
274  
275 Return a path to the person's photo
276  
277 my $photo = $person->photo();
278  
279 =cut
280  
281 0 ▶sub photo {
Subroutine photo: not called CC=6 SCAR=37.4
Pod documented
282 0 my CLASS_NAME $self = shift;
283 0 ▶ if(!defined $self->{'_photo'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_photo"}) 0 0 0
284 0 my $tag;
285 0 my $parser = $self->_parser(FORCED);
286 0 ▶ while($tag = $parser->get_tag('img')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag = $parser->get_tag("img")) 0 0 0
287 0 ▶ if($tag->[1]->{alt} && $tag->[1]->{alt} eq $self->name . ' Picture') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->[1]{"alt"} and $tag->[1]{"alt"} eq $self->name . ' Picture') 0 0 0
Condition0%0/3
$tag->[1]{"alt"} and $tag->[1]{"alt"} eq $self->name . ' Picture'
!ll&&!rl&&r
000
MC/DC0%0/2
$tag->[1]{"alt"} and $tag->[1]{"alt"} eq $self->name . ' Picture'
$tag->[1]{"alt"} $tag->[1]{"alt"} eq $self->name . ' Picture'
Truth table0%0/3
A and B
A= $tag->[1]{"alt"}B= $tag->[1]{"alt"} eq $self->name . ' Picture'
ABresult
0X0
100
111
288 0 $self->{'_photo'} = $tag->[1]{src};
289 0 last;
290 }
291 }
292  
293 0 ▶ $self->{'_photo'} = 'No Photo' unless $self->{'_photo'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_photo"} 0 0 0
294 }
295  
296 0 return $self->{'_photo'};
297 }
298  
299 =item filmography()
300  
301 Returns a person's filmography as a hash of arrays with following structure:
302  
303 my $fg = $person->filmography();
304  
305 __DATA__
306 $fg = {
307 'Section' => [
308 { title => 'movie title',
309 role => 'person role',
310 year => 'year of movie production',
311 code => 'IMDB code of movie',
312 }
313 ];
314 }
315  
316 The section can be In Development, Actor, Self, Thanks, Archive Footage, Producer etc.
317  
318 =cut
319  
320 0 ▶sub filmography {
Subroutine filmography: not called CC=12 SCAR=50.5
Pod documented
321 0 my CLASS_NAME $self = shift;
322
323 0 my $films;
324 0 ▶ if(!$self->{'_filmography'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'_filmography'}) { } 0 0 0
325 0 my $parser = $self->_parser(FORCED);
326 0 ▶ while(my $tag = $parser->get_tag('h2')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("h2")) 0 0 0
327  
328 0 my $text = $parser->get_text;
329 0 ▶ last if $text && $text =~ /filmography/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text and $text =~ /filmography/i 0 0 0
Condition0%0/3
$text and $text =~ /filmography/i
!ll&&!rl&&r
000
MC/DC0%0/2
$text and $text =~ /filmography/i
$text $text =~ /filmography/i
Truth table0%0/3
A and B
A= $textB= $text =~ /filmography/i
ABresult
0X0
100
111
330 }
331
332 0 my $key = 'Unknown';
333 0 ▶ while(my $tag = $parser->get_tag()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag) 0 0 0
334
335 0 ▶ last if $tag->[0] eq 'script'; # Netx section after filmography
Branch: 1 branch
BranchTrueFalseTotal
if $tag->[0] eq "script" 0 0 0
336
337 0 ▶ if($tag->[0] eq 'h5') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->[0] eq "h5") 0 0 0
338 0 my $caption = $parser->get_trimmed_text('h5', '/a');
339
340 0 ▶ $key = $caption if $caption;
Branch: 1 branch
BranchTrueFalseTotal
if $caption 0 0 0
341 0 $key =~ s/://;
342  
343 0 $self->_show_message("FILMOGRAPHY: key=$key; caption=$caption; trimmed=".$parser->get_trimmed_text('h5', '/a'), 'DEBUG');
344 }
345
346 0 ▶ if($tag->[0] eq 'a' && $tag->[1]->{href} && $tag->[1]{href} =~ m!title\/tt(\d+)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->[0] eq "a" and $tag->[1]{"href"} and $tag->[1]{"href"} =~ m[title\/tt(\d+)]) 0 0 0
Condition0%0/6
$tag->[0] eq "a" and $tag->[1]{"href"} and $tag->[1]{"href"} =~ m[title\/tt(\d+)]
!ll&&!rl&&r
000
$tag->[0] eq "a" and $tag->[1]{"href"}
!ll&&!rl&&r
000
MC/DC0%0/3
$tag->[0] eq "a" and $tag->[1]{"href"} and $tag->[1]{"href"} =~ m[title\/tt(\d+)]
$tag->[0] eq "a" $tag->[1]{"href"} $tag->[1]{"href"} =~ m[title\/tt(\d+)]
Truth table0%0/4
A and B and C
A= $tag->[0] eq "a"B= $tag->[1]{"href"}C= $tag->[1]{"href"} =~ m[title\/tt(\d+)]
ABCresult
0XX0
10X0
1100
1111
347 0 my $title = $parser->get_text();
348 0 my $text = $parser->get_trimmed_text('br', '/li');
349
350 0 $self->_show_message("link: $title --> $text", 'DEBUG');
351  
352 0 my $code = $1;
353 0 my($year, $role) = $text =~ m!\((\d+)\)\s.+\.+\s(.+)!;
354 0 push @{$films->{$key}}, { title => $title,
355 code => $code,
356 year => $year,
357 role => $role,
358 };
359 }
360 }
361  
362 0 $self->{'_filmography'} = $films;
363  
364 } else {
365 0 $self->_show_message("filmography defined!", 'DEBUG');
366 }
367
368 0 return $self->{'_filmography'};
369 }
370  
371 =item genres()
372  
373 Retrieve a list of movie genres for specified person:
374  
375 my $genres = $persons->genres;
376  
377 =cut
378  
379 0 ▶sub genres {
Subroutine genres: not called CC=2 SCAR=17.9
Pod documented
380 0 my CLASS_NAME $self = shift;
381  
382 0 ▶ unless($self->{_genres}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_genres"}) 0 0 0
383 0 my @genres = $self->_get_common_array_propery('genres');
384 0 $self->{_genres} = \@genres;
385 }
386  
387 0 $self->{_genres};
388 }
389  
390 =item plot_keywords()
391  
392 Retrieve a list of keywords for movies where specified person plays:
393  
394 my $keywords = $persons->plot_keywords;
395  
396 =cut
397  
398 0 ▶sub plot_keywords {
Subroutine plot_keywords: not called CC=2 SCAR=17.9
Pod documented
399 0 my CLASS_NAME $self = shift;
400  
401 0 ▶ unless($self->{_plot_keywords}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_plot_keywords"}) 0 0 0
402 0 my @keywords = $self->_get_common_array_propery('plot keywords');
403 0 $self->{_plot_keywords} = \@keywords;
404 }
405  
406 0 $self->{_plot_keywords};
407 }
408  
409 0 ▶sub _get_common_array_propery {
Subroutine _get_common_array_propery: not called CC=7 SCAR=40.3
410 0 my CLASS_NAME $self = shift;
411 0 ▶ my $target = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
412  
413 0 my $parser = $self->_parser(FORCED);
414 0 ▶ while(my $tag = $parser->get_tag(MAIN_TAG)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("h4")) 0 0 0
415 0 my $text = $parser->get_text();
416 0 ▶ last if $text =~ /$target/i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$target/i 0 0 0
417 }
418
419 0 my @res = ();
420 0 ▶ while(my $tag = $parser->get_tag('a')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $parser->get_tag("a")) 0 0 0
421 0 ▶ last if $tag->[1]->{class} && $tag->[1]->{class} =~ /tn15more/i;
Branch: 1 branch
BranchTrueFalseTotal
if $tag->[1]{"class"} and $tag->[1]{"class"} =~ /tn15more/i 0 0 0
Condition0%0/3
$tag->[1]{"class"} and $tag->[1]{"class"} =~ /tn15more/i
!ll&&!rl&&r
000
MC/DC0%0/2
$tag->[1]{"class"} and $tag->[1]{"class"} =~ /tn15more/i
$tag->[1]{"class"} $tag->[1]{"class"} =~ /tn15more/i
Truth table0%0/3
A and B
A= $tag->[1]{"class"}B= $tag->[1]{"class"} =~ /tn15more/i
ABresult
0X0
100
111
422 0 push @res, $parser->get_text;
423 }
424
425 0 return @res;
426 }
427  
428 0 ▶sub filmography_types {
Subroutine filmography_types: not called CC=1 SCAR=6.9
Pod undocumented
429 0 my CLASS_NAME $self = shift;
430 }
431  
432 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
433 0 my $self = shift;
434 }
435  
436 1 1;
437  
438 __END__
« Film.pm ↑ Summary BaseClass.pm »