lib/FilmAffinity/UserRating.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 100% 6 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.7% 18 / 19 Podpod 66.6% 2 / 3 totaltotal 94.3% 116 / 123 CC 6 SCAR 4.3
CC
6
Cov
97%
parse
3.0
BUILD
2.0
__ANON__
2.0
CRAP 1.5
« lib/FilmAffinity/Utils.pm ↑ Summary
1 package FilmAffinity::UserRating;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
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 Text::Trim;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::RobotUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use MooseX::Privacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶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 1.01
26  
27 =cut
28  
29 5 our $VERSION = '1.01';
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 5 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 5 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 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 5 );
116  
117 5 has ua => (
118 is => 'rw',
119 isa => 'LWP::RobotUA',
120 traits => [qw/Private/],
121 );
122  
123 5 Readonly my $SECONDS => 5;
124 5 my $RATING_URL = 'http://www.filmaffinity.com/en/userratings.php?orderby=2&';
125 5 my $XPATH_ID = '//div[@class="user-ratings-movie-item"]/div/@data-movie-id';
126 5 my $XPATH_TITLE = '//div[@class="mc-title"]/a';
127 5 my $XPATH_RATING = '//div[@class="ur-mr-rat"]';
128  
129 4 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
130 4 my ($self, $args) = @_;
131  
132 4 ▶ $self->ua( buildRobot( $args->{delay} || $SECONDS ) );
Condition66%2/3
$args->{'delay'} || $SECONDS
l!l&&r!l&&!r
130
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
133  
134 4 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 2 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
148 2 my $self = shift;
149  
150 2 my ($next, $page) = (1, 1);
151 2 ▶ while ( $next ){
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 4 2 6
152 4 my $url = $self->p_buildUrl( $page );
153 4 my $response = $self->ua->get($url);
154 4 ▶ if ($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 3 1 4
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 1 $next = 0;
161 }
162 }
163 2 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 6 ▶sub parseString {
Subroutine parseString: called CC=1 SCAR=0.0
Pod documented
176 6 my ($self, $content) = @_;
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 0 ▶private_method p_buildUrl => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 my ($self, $page) = @_;
197  
198 0 return $RATING_URL.'p='.$page.'&user_id='.$self->userID;
199 5 };
200  
201 6 ▶private_method p_buildMovieInfo => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
202 6 my ($self, $ref_ids, $ref_titles, $ref_ratings) = @_;
203  
204 6 for my $i (0..(@{$ref_titles}-1)){
205 126 $self->movies->{${$ref_ids}[$i]} = {
206 126 'title' => demoronize(${$ref_titles}[$i]),
207 126 'rating' => ${$ref_ratings}[$i],
208 }
209 }
210 5 };
211  
212 3 ▶private_method p_isNextPage => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 3 my ($self, $content) = @_;
214  
215 3 ▶ if ($content =~ m/>&gt;&gt;<\/a><\/div><\/div>/xms){
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[>&gt;&gt;</a></div></div>]msx) 2 1 3
216 2 return 1;
217 }
218 1 return 0;
219 5 };
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 5 1; # End of FilmAffinity::UserRating
« lib/FilmAffinity/Utils.pm ↑ Summary