blib/lib/FilmAffinity/Movie.pm

filter: Statementstmt 98.1% 162 / 165 Branchbran 88.4% 23 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 96.3% 26 / 27 Podpod 80.0% 4 / 5 totaltotal 93.2% 222 / 238 SCAR 30.0
CC
20
Cov
95%
__ANON__
6.0
__ANON__
3.0
__ANON__
3.0
CRAP 20.1
↑ Summary Build »
1 package FilmAffinity::Movie;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Text::Trim;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use LWP::RobotUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use MooseX::Privacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use FilmAffinity::Utils qw/buildRobot/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 FilmAffinity::Movie - Perl interface to FilmAffinity
23  
24 =head1 VERSION
25  
26 Version 0.08
27  
28 =cut
29  
30 4 our $VERSION = 0.08;
31  
32 =head1 SYNOPSIS
33  
34 Retrieve information about a filmaffinity movie
35  
36 use FilmAffinity::Movie;
37  
38 my $movie = FilmAffinity::Movie->new(
39 id => $movieID,
40 delay => $delay || 5,
41 );
42
43 $movie->parse();
44  
45 Via the command-line program filmaffinity-get-movie-info.pl
46  
47 =head1 DESCRIPTION
48  
49 =head2 Overview
50  
51 FilmAffinity::Movie is a Perl interface to FilmAffinity. You can use
52 this module to retrieve information about a movie.
53  
54 =head2 Constructor
55  
56 =over 4
57  
58 =item new()
59  
60 Object's constructor. You should pass as parameter the movieID
61  
62 my $movie = FilmAffinity::Movie->new( id => '932476' );
63  
64 =back
65  
66 =head2 Options
67  
68 =over 4
69  
70 =item delay
71  
72 Set the minimum delay between requests to the server, in seconds.
73  
74 my $parser = FilmAffinity::Movie->new(
75 userID => '932476',
76 delay => 20,
77 );
78  
79 By default, the delay is 5 seconds
80  
81 =back
82  
83 =head1 ACCESSORS
84  
85 =head2 $movie->id
86  
87 get id
88  
89 =head2 $movie->title
90  
91 get title
92  
93 =head2 $movie->originaltitle
94  
95 get original title
96  
97 =head2 $movie->year
98  
99 get year
100  
101 =head2 $movie->duration
102  
103 get running time (in minutes)
104  
105 =head2 $movie->synopsis
106  
107 get synopsis
108  
109 =head2 $movie->website
110  
111 get url of the movie website
112  
113 =head2 $movie->country
114  
115 get country
116  
117 =head2 $movie->cover
118  
119 get url of the cover
120  
121 =head2 $movie->rating
122  
123 get site rating
124  
125 =head2 $movie->votes
126  
127 get number of votes
128  
129 =head2 $movie->genre
130  
131 get genres list
132  
133 =head2 $movie->topic
134  
135 get topics list
136  
137 =head2 $movie->cast
138  
139 get cast list
140  
141 =head2 $movie->director
142  
143 get directors list
144  
145 =head2 $movie->composer
146  
147 get composers list
148  
149 =head2 $movie->screenwriter
150  
151 get screenwriters list
152  
153 =head2 $movie->cinematographer
154  
155 get cinematographers list
156  
157 =head2 $movie->studio
158  
159 get studios list
160  
161 =head2 $movie->producer
162  
163 get producers list
164  
165 =cut
166  
167 4 has id => ( is => 'ro', isa => 'Int', required => 1, );
168 4 has title => ( is => 'rw', isa => 'Str', );
169 4 has originaltitle => ( is => 'rw', isa => 'Str', );
170 4 has year => ( is => 'rw', isa => 'Int', );
171 4 has duration => ( is => 'rw', isa => 'Int', );
172 4 has synopsis => ( is => 'rw', isa => 'Str', );
173 4 has website => ( is => 'rw', isa => 'Str', );
174 4 has country => ( is => 'rw', isa => 'Str', );
175 4 has cover => ( is => 'rw', isa => 'Str', );
176 4 has rating => ( is => 'rw', isa => 'Num', );
177 4 has votes => ( is => 'rw', isa => 'Num', );
178  
179 4 has genre => ( is => 'rw', isa => 'ArrayRef[Str]', );
180 4 has topic => ( is => 'rw', isa => 'ArrayRef[Str]', );
181  
182 4 has cast => ( is => 'rw', isa => 'ArrayRef[Str]', );
183 4 has director => ( is => 'rw', isa => 'ArrayRef[Str]', );
184 4 has composer => ( is => 'rw', isa => 'ArrayRef[Str]', );
185  
186 4 has screenwriter => ( is => 'rw', isa => 'ArrayRef[Str]', );
187 4 has cinematographer => ( is => 'rw', isa => 'ArrayRef[Str]', );
188  
189 4 has studio => ( is => 'rw', isa => 'ArrayRef[Str]', );
190 4 has producer => ( is => 'rw', );
191  
192  
193 4 has tree => (
194 is => 'rw',
195 isa => 'HTML::TreeBuilder',
196 traits => [qw/Private/],
197 );
198  
199 4 has ua => (
200 is => 'rw',
201 isa => 'LWP::RobotUA',
202 traits => [qw/Private/],
203 );
204  
205 4 my $MOVIE_URL = 'http://www.filmaffinity.com/en/film';
206 4 my $XPATH_TITLE = '//h1[@id="main-title"]';
207 4 my $XPATH_RATING = '//div[@id="movie-rat-avg"]';
208 4 my $XPATH_VOTE = '//div[@id="movie-count-rat"]/span';
209 4 my $XPATH_COUNTRY = '//span[@id="country-img"]/img/@title';
210 4 my $XPATH_COVER = '//div[@id="movie-main-image-container"]/a/img/@src';
211  
212 4 my @JSON_FIELD = qw(
213 id title year synopsis website duration cast director composer screenwriter
214 cinematographer genre topic studio producer country cover rating votes
215 originaltitle
216 );
217  
218 4 my $FIELD = [
219 {
220 accessor => 'originaltitle',
221 faTag => 'Original title',
222 },
223 {
224 accessor => 'year',
225 faTag => 'Year',
226 },
227 {
228 accessor => 'synopsis',
229 faTag => 'Synopsis / Plot',
230 },
231 {
232 accessor => 'website',
233 faTag => 'Official Site',
234 },
235 {
236 accessor => 'duration',
237 faTag => 'Running Time',
238 cleanerSub => \&p_cleanDuration,
239 },
240 {
241 accessor => 'cast',
242 faTag => 'Cast',
243 cleanerSub => \&p_cleanPerson,
244 },
245 {
246 accessor => 'director',
247 faTag => 'Director',
248 cleanerSub => \&p_cleanPerson,
249 },
250 {
251 accessor => 'composer',
252 faTag => 'Music',
253 cleanerSub => \&p_cleanPerson,
254 },
255 {
256 accessor => 'screenwriter',
257 faTag => 'Screenwriter',
258 cleanerSub => \&p_cleanPerson,
259 },
260 {
261 accessor => 'cinematographer',
262 faTag => 'Cinematography',
263 cleanerSub => \&p_cleanPerson,
264 },
265 {
266 accessor => 'genre',
267 faTag => 'Genre',
268 cleanerSub => \&p_cleanGenre,
269 },
270 {
271 accessor => 'topic',
272 faTag => 'Genre',
273 cleanerSub => \&p_cleanGenre,
274 },
275 {
276 accessor => 'studio',
277 faTag => 'Production Co.',
278 cleanerSub => \&p_cleanStudio,
279 },
280 {
281 accessor => 'producer' ,
282 faTag => 'Production Co.',
283 cleanerSub => \&p_cleanStudio,
284 },
285 ];
286  
287 4 Readonly my $SECONDS => 5;
288  
289 sub BUILD {
290 9 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=2 SCAR=7.4
Pod undocumented
291  
292 9 $self->tree( HTML::TreeBuilder->new() );
293 9 ▶ $self->ua( buildRobot( $args->{delay} || $SECONDS ) );
Condition33%1/3
$args->{'delay'} || $SECONDS
l!l&&r!l&&!r
090
MC/DC0%0/2
$args->{'delay'} || $SECONDS
$args->{'delay'} $SECONDS
Truth table33%1/3
A || B
A= $args->{'delay'}B= $SECONDS
ABresult
1X1
011
000
294 9 return;
295 }
296  
297 =head1 SUBROUTINES/METHODS
298  
299 =head2 $movie->parse()
300  
301 This method will get the content of the filmaffinity webpage and retrieve
302 all information about the movie
303  
304 =cut
305  
306 sub parse {
307 8 ▶ my $self = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
308  
309 8 my $content = $self->getContent();
310 8 $self->parsePage($content);
311 8 return;
312 }
313  
314  
315 =head2 $movie->getContent()
316  
317 This method will get the content of the filmaffinity webpage
318  
319 =cut
320  
321 sub getContent {
322 8 ▶ my ($self) = @_;
Subroutine getContent: called CC=2 SCAR=7.0
Pod documented
323  
324 8 my $url = $self->p_buildUrlMovie($self->id);
325  
326 8 my $response = $self->ua->get($url);
327 8 ▶ if ($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 8 0 8
328 8 return $response->content();
329 }
330 }
331  
332 =head2 $movie->parsePage($content)
333  
334 This method parses a page of filmaffinity that is available as
335 a single string in memory.
336  
337 =cut
338  
339 sub parsePage {
340 8 ▶ my ($self, $content) = @_;
Subroutine parsePage: called CC=2 SCAR=6.9
Pod documented
341  
342 8 $content = decode('utf-8', $content);
343 8 $self->tree->parse($content);
344  
345 8 foreach my $data (@{$FIELD}){
346 112 $self->p_findField($data);
347 }
348  
349 8 $self->p_findRating();
350 8 $self->p_findVotes();
351 8 $self->country( $self->tree->findvalue( $XPATH_COUNTRY ) );
352 8 $self->cover( $self->tree->findvalue( $XPATH_COVER ) );
353 8 $self->title( $self->tree->findvalue( $XPATH_TITLE ) );
354  
355 8 $self->tree->delete();
356 8 return;
357 }
358  
359  
360  
361 =head2 $movie->toJSON()
362  
363 This method will export all movie informations in JSON format
364  
365 =cut
366  
367 sub toJSON {
368 16 ▶ my $self = shift;
Subroutine toJSON: called CC=3 SCAR=11.0
Pod documented
369  
370 16 my %data;
371 16 foreach my $field (@JSON_FIELD){
372 320 ▶ $data{$field} = $self->$field() if defined $self->$field();
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$field 286 34 320
373 };
374  
375 16 return to_json(\%data, {pretty => 1});
376 }
377  
378 private_method p_findField => sub {
379 112 ▶ my ( $self, $data ) = @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
380  
381 112 my @nodes = $self->tree->findnodes( '//dl[@class="movie-info"]/dt' );
382 112 foreach my $node (@nodes){
383 872 ▶ if ( trim( $node->as_text() ) eq $data->{faTag} ){
Branch: 1 branch
BranchTrueFalseTotal
if (trim($node->as_text) eq $data->{"faTag"}) 106 766 872
384  
385 106 my $searched_node = $node->right();
386  
387 106 my $accessor = $data->{accessor};
388  
389 106 my $value = trim( $searched_node->as_text() );
390  
391 106 ▶ next if $value eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 106 106
392  
393 106 ▶ if (defined $data->{cleanerSub}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"cleanerSub"}) 77 29 106
394 77 $value = $data->{cleanerSub}($value, $accessor);
395 }
396  
397 106 ▶ next if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 9 97 106
398  
399 97 $self->$accessor( $value );
400 97 last;
401 }
402 }
403 4 };
404  
405 private_method p_findRating => sub {
406 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
407  
408 8 my $rating = $self->tree->findvalue( $XPATH_RATING );
409  
410 8 ▶ return if not defined $rating or $rating eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rating or $rating eq "" 1 7 8
Condition66%2/3
not defined $rating or $rating eq ""
l!l&&r!l&&!r
017
MC/DC50%1/2
not defined $rating or $rating eq ""
not defined $rating $rating eq ""
Truth table66%2/3
not(A) or B
A= not defined $ratingB= $rating eq ""
ABresult
1X1
011
000
411 7 $self->rating( trim($rating) );
412 4 };
413  
414 private_method p_findVotes => sub {
415 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
416  
417 8 my $votes = $self->tree->findvalue( $XPATH_VOTE );
418  
419 8 ▶ return if not defined $votes or $votes eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $votes or $votes eq "" 1 7 8
Condition66%2/3
not defined $votes or $votes eq ""
l!l&&r!l&&!r
017
MC/DC50%1/2
not defined $votes or $votes eq ""
not defined $votes $votes eq ""
Truth table66%2/3
not(A) or B
A= not defined $votesB= $votes eq ""
ABresult
1X1
011
000
420 7 $votes =~ s/\D//gixms;
421 7 $self->votes( trim($votes) );
422 4 };
423  
424 private_method p_buildUrlMovie => sub {
425 0 ▶ my ($self, $id) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
426  
427 0 return $MOVIE_URL.$id.'.html';
428 4 };
429  
430 private_method p_removeTextBetweenParenthesis => sub {
431 149 ▶ my $content = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
432  
433 149 $content =~ s/[(].*[)]//gxms;
434 149 return $content;
435 4 };
436  
437 private_method p_cleanDuration => sub {
438 7 ▶ my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
439 7 $value =~ s/min[.]//gixms;
440 7 $value = trim($value);
441  
442 7 ▶ if ( looks_like_number($value) ){
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($value)) 7 0 7
443 7 return $value;
444 }
445 0 return;
446 4 };
447  
448 private_method p_cleanPerson => sub {
449 38 ▶ my $value = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
450  
451 38 my @persons = split m/,/xms, $value;
452 38 @persons = map {trim (p_removeTextBetweenParenthesis($_) )} @persons;
453 38 return \@persons;
454 4 };
455  
456 private_method p_cleanGenre => sub {
457 16 ▶ my ($value, $field) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
458  
459 16 ▶ my $pos = $field eq 'genre' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$field eq 'genre' ? : 8 8 16
460 16 my @list = split m/[|]/xms, $value;
461  
462 16 ▶ if ( defined $list[$pos]){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $list[$pos]) 14 2 16
463 14 my @genres = trim ( split m/[.]/xms, $list[$pos] );
464 14 return \@genres;
465 }
466 2 return;
467 4 };
468  
469 private_method p_cleanStudio => sub {
470 16 ▶ my ($value, $field) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
471  
472 16 ▶ my $pos = $field eq 'studio' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$field eq 'studio' ? : 8 8 16
473 16 my @list = split m/[.]\sProducer: /xms, $value;
474  
475 16 my @studio = ();
476 16 ▶ if (not defined $list[$pos]){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $list[$pos]) { } 7 9 16
477 7 return;
478 } else {
479 9 @studio = trim ( split m{ / }xms, $list[$pos] );
480 9 return \@studio;
481 }
482 4 };
483  
484 =head1 AUTHOR
485  
486 William Belle, C<< <william.belle at gmail.com> >>
487  
488 =head1 BUGS AND LIMITATIONS
489  
490 Please report any bugs or feature requests to C<bug-filmaffinity-userrating at rt.cpan.org>, or through
491 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FilmAffinity-UserRating>. I will be notified, and then you'll
492 automatically be notified of progress on your bug as I make changes.
493  
494 =head1 SUPPORT
495  
496 You can find documentation for this module with the perldoc command.
497  
498 perldoc FilmAffinity::Movie
499  
500 You can also look for information at:
501  
502 =over 4
503  
504 =item * RT: CPAN's request tracker (report bugs here)
505  
506 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FilmAffinity-UserRating>
507  
508 =item * AnnoCPAN: Annotated CPAN documentation
509  
510 L<http://annocpan.org/dist/FilmAffinity-UserRating>
511  
512 =item * CPAN Ratings
513  
514 L<http://cpanratings.perl.org/d/FilmAffinity-UserRating>
515  
516 =item * Search CPAN
517  
518 L<http://search.cpan.org/dist/FilmAffinity-UserRating/>
519  
520 =back
521  
522 =head1 SEE ALSO
523  
524 L<http://www.filmaffinity.com>
525  
526 =head1 LICENSE AND COPYRIGHT
527  
528 Copyright 2013 William Belle.
529  
530 This program is free software; you can redistribute it and/or modify it
531 under the terms of either: the GNU General Public License as published
532 by the Free Software Foundation; or the Artistic License.
533  
534 See http://dev.perl.org/licenses/ for more information.
535  
536 =cut
537  
538 4 1; # End of FilmAffinity::Movie
↑ Summary Build »