FilmAffinity-UserRating
view release on metacpan or search on metacpan
lib/FilmAffinity/UserRating.pm view on Meta::CPAN
get username
=cut
has username => (
is => 'ro',
isa => 'Str',
writer => 'p_username',
);
=head2 $parser->movies
get movies
=cut
has movies => (
is => 'rw',
isa => 'HashRef[Str]',
default => sub { {} },
);
has ua => (
is => 'rw',
isa => 'LWP::RobotUA',
traits => [qw/Private/],
);
Readonly my $SECONDS => 5;
my $RATING_URL = 'http://www.filmaffinity.com/en/userratings.php?orderby=2&';
my $XPATH_ID = '//div[@class="user-ratings-movie-item"]/div/@data-movie-id';
my $XPATH_TITLE = '//div[@class="mc-title"]/a';
my $XPATH_RATING = '//div[@class="ur-mr-rat"]';
sub BUILD {
my ($self, $args) = @_;
$self->ua( buildRobot( $args->{delay} || $SECONDS ) );
return;
}
=head1 SUBROUTINES/METHODS
=head2 $parser->parse()
This function parses all rating pages of filmaffinity from a user.
my $ref_movies = $parser->parse();
=cut
sub parse {
my $self = shift;
my ($next, $page) = (1, 1);
while ( $next ){
my $url = $self->p_buildUrl( $page );
my $response = $self->ua->get($url);
if ($response->is_success){
my $content = $response->decoded_content();
$self->parseString($content);
$next = $self->p_isNextPage($content);
$page++;
} else {
$next = 0;
}
}
return $self->movies;
}
=head2 $parser->parseString($content)
This function parses a page of filmaffinity that is available as
a single string in memory.
$parser->parseString($content);
=cut
sub parseString {
my ($self, $content) = @_;
$content = demoronize($content);
$content = decode('iso8859-1', $content);
my $tree = HTML::TreeBuilder->new();
$tree->parse($content);
$self->p_username($tree->findvalue( '//span[@id="nick"]' ));
my @ids = $tree->findnodes_as_strings( $XPATH_ID );
my @titles = $tree->findnodes_as_strings( $XPATH_TITLE );
my @ratings = $tree->findnodes_as_strings( $XPATH_RATING );
$self->p_buildMovieInfo(\@ids, \@titles, \@ratings);
$tree->delete();
return;
}
private_method p_buildUrl => sub {
my ($self, $page) = @_;
return $RATING_URL.'p='.$page.'&user_id='.$self->userID;
};
private_method p_buildMovieInfo => sub {
my ($self, $ref_ids, $ref_titles, $ref_ratings) = @_;
for my $i (0..(@{$ref_titles}-1)){
$self->movies->{${$ref_ids}[$i]} = {
'title' => demoronize(${$ref_titles}[$i]),
'rating' => ${$ref_ratings}[$i],
}
}
};
private_method p_isNextPage => sub {
my ($self, $content) = @_;
if ($content =~ m/>>><\/a><\/div><\/div>/xms){
( run in 1.103 second using v1.01-cache-2.11-cpan-39bf76dae61 )