stepanov / IMDB-Film

IMDB::Film module
http://search.cpan.org/~stepanov/
Other
3 stars 4 forks source link

Unable to retrieve recommendation_movies #1

Open brainkiller opened 12 years ago

brainkiller commented 12 years ago

This call always returns a empty result:

$imdbObj->recommendation_movies()

andy71 commented 6 years ago

I know it's quite old, but maybe someone needs it.

Just replace the whole sub "recommendation_movies" within Film.pm (worked for me):

sub recommendation_movies {
    my CLASS_NAME $self = shift;
    my $forced = shift || 0;

    if($forced) {
        my $parser = $self->_parser(FORCED);

        while (my $token = $parser->get_token) {
            if ($token->[0] eq 'S' and $token->[1] eq 'div') {
                if (exists $token->[2]{id} and $token->[2]{id} eq 'titleRecs') {
                    $self->_show_message("Jumped to DIV " . $token->[2]{id}, 'DEBUG');
                    last;
                }
            }
        }

        my %result = ();
        my $end_tag_counter = 1;
        while(my $tag = $parser->get_tag()) {
            $end_tag_counter++ if $tag->[0] eq 'div';
            $end_tag_counter-- if $tag->[0] eq '/div';

            my $text = $parser->get_text();
            if($tag->[0] eq 'a' && $text && $tag->[1]{href} && $tag->[1]{href} =~ /tt(\d+)/) {
                $result{$1} = $text;
            }
            last if $end_tag_counter le '0';
        }

        $self->{_recommendation_movies} = \%result;
    }

    return $self->{_recommendation_movies};
}

The first while loop moves forward within the IMDb page until the div tag with id 'titleRecs'. Then we need a counter for nested div tags to jump to last if it reaches 0 again. And i removed an 'Use of uninitialized value in pattern match' warning by adding another check if $tag->[1]{href} is initialized before using it in the pattern match.