lib/FilmAffinity/Movie.pm

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