blib/lib/FilmAffinity/UserRating.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.7% 18 / 19 Podpod 66.6% 2 / 3 totaltotal 91.8% 113 / 123 SCAR 17.9
CC
6
Cov
94%
parse
3.0
BUILD
2.1
__ANON__
2.0
CRAP 6.0
« Build ↑ Summary blib/lib/FilmAffinity/Utils.pm »
1 package FilmAffinity::UserRating;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
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 Text::Trim;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use LWP::RobotUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use MooseX::Privacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use FilmAffinity::Utils qw/buildRobot demoronize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 FilmAffinity::UserRating - Perl interface to FilmAffinity
22  
23 =head1 VERSION
24  
25 Version 0.08
26  
27 =cut
28  
29 4 our $VERSION = '0.08';
30  
31 =head1 SYNOPSIS
32  
33 Get filmaffinity voted movies from a user
34  
35 use FilmAffinity::UserRating;
36  
37 my $parser = FilmAffinity::UserRating->new( userID => '123456' );
38  
39 my $ref_movies = $parser->parse();
40  
41 Via the command-line program filmaffinity-get-ratings.pl
42  
43 =head1 DESCRIPTION
44  
45 =head2 Overview
46  
47 FilmAffinity::UserRating is a Perl interface to FilmAffinity. You can use
48 this module to retrieve your rated movies.
49  
50 =head2 Constructor
51  
52 =over 4
53  
54 =item new()
55  
56 Object's constructor. You should pass as parameter the userID
57  
58 my $parser = FilmAffinity::UserRating->new( userID => '123456' );
59  
60 =back
61  
62 =head2 Options
63  
64 =over 4
65  
66 =item delay
67  
68 Set the minimum delay between requests to the server, in seconds.
69  
70 my $parser = FilmAffinity::UserRating->new(
71 userID => '123456',
72 delay => 20,
73 );
74  
75 By default, the delay is 5 seconds
76  
77 =back
78  
79 =head1 ACCESSORS
80  
81 =head2 $parser->userID
82  
83 get userID
84  
85 =cut
86  
87 4 has userID => (
88 is => 'ro',
89 isa => 'Int',
90 required => 1,
91 );
92  
93 =head2 $parser->username
94  
95 get username
96  
97 =cut
98  
99 4 has username => (
100 is => 'ro',
101 isa => 'Str',
102 writer => 'p_username',
103 );
104  
105 =head2 $parser->movies
106  
107 get movies
108  
109 =cut
110  
111 has movies => (
112 is => 'rw',
113 isa => 'HashRef[Str]',
114 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 4 );
116  
117 4 has ua => (
118 is => 'rw',
119 isa => 'LWP::RobotUA',
120 traits => [qw/Private/],
121 );
122  
123 4 Readonly my $SECONDS => 5;
124 4 my $RATING_URL = 'http://www.filmaffinity.com/en/userratings.php?orderby=2&';
125 4 my $XPATH_ID = '//table[@class="amovie_info"]/tr/child::*[1]/div/@data-movie-id';
126 4 my $XPATH_TITLE = '//div[@class="mc-title"]/a';
127 4 my $XPATH_RATING = '//table[@class="amovie_info"]/tr/child::*[2]/div/child::*[1]';
128  
129 sub BUILD {
130 3 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
131  
132 3 ▶ $self->ua( buildRobot( $args->{delay} || $SECONDS ) );
Condition33%1/3
$args->{'delay'} || $SECONDS
l!l&&r!l&&!r
030
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
133  
134 3 return;
135 }
136  
137 =head1 SUBROUTINES/METHODS
138  
139 =head2 $parser->parse()
140  
141 This function parses all rating pages of filmaffinity from a user.
142  
143 my $ref_movies = $parser->parse();
144  
145 =cut
146  
147 sub parse {
148 1 ▶ my $self = shift;
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
149  
150 1 my ($next, $page) = (1, 1);
151 1 ▶ while ( $next ){
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 3 1 4
152 3 my $url = $self->p_buildUrl( $page );
153 3 my $response = $self->ua->get($url);
154 3 ▶ if ($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 3 0 3
155 3 my $content = $response->decoded_content();
156 3 $self->parseString($content);
157 3 $next = $self->p_isNextPage($content);
158 3 $page++;
159 } else {
160 0 $next = 0;
161 }
162 }
163 1 return $self->movies;
164 }
165  
166 =head2 $parser->parseString($content)
167  
168 This function parses a page of filmaffinity that is available as
169 a single string in memory.
170  
171 $parser->parseString($content);
172  
173 =cut
174  
175 sub parseString {
176 6 ▶ my ($self, $content) = @_;
Subroutine parseString: called CC=1 SCAR=0.0
Pod documented
177  
178 6 $content = demoronize($content);
179 6 $content = decode('iso8859-1', $content);
180  
181 6 my $tree = HTML::TreeBuilder->new();
182 6 $tree->parse($content);
183 6 $self->p_username($tree->findvalue( '//span[@id="nick"]' ));
184  
185 6 my @ids = $tree->findnodes_as_strings( $XPATH_ID );
186 6 my @titles = $tree->findnodes_as_strings( $XPATH_TITLE );
187 6 my @ratings = $tree->findnodes_as_strings( $XPATH_RATING );
188  
189 6 $self->p_buildMovieInfo(\@ids, \@titles, \@ratings);
190  
191 6 $tree->delete();
192 6 return;
193 }
194  
195 private_method p_buildUrl => sub {
196 0 ▶ my ($self, $page) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
197  
198 0 return $RATING_URL.'p='.$page.'&user_id='.$self->userID;
199 4 };
200  
201 private_method p_buildMovieInfo => sub {
202 6 ▶ my ($self, $ref_ids, $ref_titles, $ref_ratings) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
203  
204 6 for my $i (0..(@{$ref_titles}-1)){
205 147 $self->movies->{${$ref_ids}[$i]} = {
206 147 'title' => demoronize(${$ref_titles}[$i]),
207 147 'rating' => ${$ref_ratings}[$i],
208 }
209 }
210 4 };
211  
212 private_method p_isNextPage => sub {
213 3 ▶ my ($self, $content) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
214  
215 3 ▶ if ($content =~ m/>&gt;&gt;<\/a><\/div><\/div><\/td>/xms){
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[>&gt;&gt;</a></div></div></td>]msx) 2 1 3
216 2 return 1;
217 }
218 1 return 0;
219 4 };
220  
221 =head1 AUTHOR
222  
223 William Belle, C<< <william.belle at gmail.com> >>
224  
225 =head1 BUGS AND LIMITATIONS
226  
227 Please report any bugs or feature requests to C<bug-filmaffinity-userrating at rt.cpan.org>, or through
228 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FilmAffinity-UserRating>. I will be notified, and then you'll
229 automatically be notified of progress on your bug as I make changes.
230  
231 =head1 SUPPORT
232  
233 You can find documentation for this module with the perldoc command.
234  
235 perldoc FilmAffinity::UserRating
236  
237  
238 You can also look for information at:
239  
240 =over 4
241  
242 =item * RT: CPAN's request tracker (report bugs here)
243  
244 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FilmAffinity-UserRating>
245  
246 =item * AnnoCPAN: Annotated CPAN documentation
247  
248 L<http://annocpan.org/dist/FilmAffinity-UserRating>
249  
250 =item * CPAN Ratings
251  
252 L<http://cpanratings.perl.org/d/FilmAffinity-UserRating>
253  
254 =item * Search CPAN
255  
256 L<http://search.cpan.org/dist/FilmAffinity-UserRating/>
257  
258 =back
259  
260 =head1 SEE ALSO
261  
262 L<http://www.filmaffinity.com>
263  
264 =head1 LICENSE AND COPYRIGHT
265  
266 Copyright 2013 William Belle.
267  
268 This program is free software; you can redistribute it and/or modify it
269 under the terms of either: the GNU General Public License as published
270 by the Free Software Foundation; or the Artistic License.
271  
272 See http://dev.perl.org/licenses/ for more information.
273  
274 =cut
275  
276 4 1; # End of FilmAffinity::UserRating
« Build ↑ Summary blib/lib/FilmAffinity/Utils.pm »