Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve recommendation_movies #1

Open
brainkiller opened this issue Sep 16, 2012 · 1 comment
Open

Unable to retrieve recommendation_movies #1

brainkiller opened this issue Sep 16, 2012 · 1 comment

Comments

@brainkiller
Copy link

This call always returns a empty result:

$imdbObj->recommendation_movies()

@andy71
Copy link

andy71 commented Aug 13, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants