--- tmdb-old.pl 2010-06-14 20:22:50.000000000 +0200 +++ tmdb.pl 2010-06-14 20:18:14.000000000 +0200 @@ -10,6 +10,8 @@ # # v0.1 # - Initial script +# v0.2 +# - Butchered by Wayne Thomas to work with API 2.1 #TODO: use TMDB IDs and fallback to assuming imdb ID rather than the # current way. @@ -38,8 +40,7 @@ my @countries = qw(USA UK Canada Japan); -my $base_url = "http://api.themoviedb.org/2.0"; -#my $posterimdb_url = "http://api.themoviedb.org/cover.php?imdb="; +my $base_url = "http://api.themoviedb.org/2.1"; # themoviedb.org api key given by Travis Bell for Mythtv my $API_KEY = "c27cb71cff5bd76e1a7a009380562c62"; @@ -84,7 +85,11 @@ my ($paramshashref) = @_; my @ret; while (my ($key, $value) = each(%{$paramshashref})) { - push(@ret, uri_escape($key) . "=" . uri_escape($value)); +# push(@ret, uri_escape($value)); +# uri_escape was causing some funkiness with spaces for some reason. +# Removed it on the assumption that mythvideo will clean up the titles +# before querying tmdb.pl + push(@ret, $value); } return join('&', @ret); @@ -98,9 +103,7 @@ my $request = join('/', $base_url, $relurl); - $queryargs_hashref->{'api_key'} = $API_KEY; - $request = $request . "?" . encode_args($queryargs_hashref); - + $request = $request . "/en/xml/$API_KEY/" . encode_args($queryargs_hashref); if (defined $opt_d) { printf("# request: '%s'\n", $request); } my ($rc, $response) = myth_url_get($request); @@ -131,7 +134,7 @@ if ($xml->{"opensearch:totalResults"} > 0) { # now get the movie data via Movie.getInfo, Movie.imdbLookup does not # provide us all the data - my $tmdbid = $xml->{moviematches}->{movie}->[0]->{id}; + my $tmdbid = $xml->{movies}->{movie}->[0]->{id}; my ($rc, $response) = TMDBAPIRequest('Movie.getInfo', {'id' => $tmdbid}); @@ -142,36 +145,39 @@ $xml = $xs->XMLin($response, ForceArray => ['category', 'production_countries', 'person'], - KeyAttr => ['key', 'id']); + KeyAttr => ['key']); - my $movie = $xml->{moviematches}->{movie}; - my $title = $movie->{title}; - my $releasedate = $movie->{release}; + my $movie = $xml->{movies}->{movie}; + my $title = $movie->{name}; + my $releasedate = $movie->{released}; my $year = substr($releasedate, 0, 4); - my $plot = $movie->{short_overview}; + my $plot = $movie->{overview}; my $userrating = $movie->{rating}; my $runtime = $movie->{runtime}; my $budget = $movie->{budget}; my $revenue = $movie->{revenue}; - my $trailer = $movie->{trailer}->{content}; + my $trailer = $movie->{trailer}; my $homepage = $movie->{homepage}; # Country - my @countrylist = - @{$xml->{moviematches}->{movie}->{production_countries}}; +# Country was causing some unwanted warning messages, as far as I'm +# aware it is superflous have commented out (in case someone wants to +# fix it. +# my @countrylist = +# {$xml->{movies}->{movie}->{countries}}; my $country; - if (@countrylist > 0 && ref $countrylist[0] eq 'HASH' && - exists $countrylist[0]->{name}) { - $country = $countrylist[0]->{name}; - } +# if (@countrylist > 0 && ref $countrylist[0] eq 'HASH' && +# exists $countrylist[0]->{name}) { +# $country = $countrylist[0]->{name}; +# } # Genre my $genres; my @lgenres; - if (exists $xml->{moviematches}->{movie}->{categories}) { + if (exists $xml->{movies}->{movie}->{categories}) { my @catlist; - @catlist = @{$xml->{moviematches}->{movie}->{categories}->{category}} - if ref $xml->{moviematches}->{movie}->{categories}; + @catlist = @{$xml->{movies}->{movie}->{categories}->{category}} + if ref $xml->{movies}->{movie}->{categories}; if (@catlist > 0) { my $j = 0; for (my $i = 0; $i < @catlist; $i++) @@ -189,21 +195,21 @@ my $dc = 0; my $cc = 0; my $wc = 0; - if (exists $xml->{moviematches}->{movie}->{people}) { - my @castlist = @{$xml->{moviematches}->{movie}->{people}->{person}}; + if (exists $xml->{movies}->{movie}->{cast}) { + my @castlist = @{$xml->{movies}->{movie}->{cast}->{person}}; for (my $i = 0; $i < @castlist; $i++) { - if ($castlist[$i]->{job} eq "actor") + if ($castlist[$i]->{job} eq "Actor") { $lcast[$cc++] = $castlist[$i]->{name}; } - if ($castlist[$i]->{job} eq "director") + if ($castlist[$i]->{job} eq "Director") { $ldirector[$dc++] = $castlist[$i]->{name}; } - if ($castlist[$i]->{job} eq "writer") + if ($castlist[$i]->{job} eq "Writer") { $lwriter[$wc++] = $castlist[$i]->{name}; } @@ -255,10 +261,28 @@ KeyAttr => {poster => 'size'}); if ($xml->{"opensearch:totalResults"} > 0) { - if (exists $xml->{moviematches}->{movie}->[0]->{poster}->{mid}) + if (exists $xml->{movies}->{movie}->[0]->{images}) { - print($xml->{moviematches}->{movie}->[0]->{poster}-> - {mid}->{content} . "\n"); + my @posterlist; + @posterlist = @{$xml->{movies}->{movie}->[0]->{images}->{image}} + if ref $xml->{movies}->{movie}->[0]->{images}; + if (@posterlist > 0) { + my @midposters; + my $j = 0; + for (my $i = 0; $i < @posterlist; $i++) + { + if ($posterlist[$i]->{type} eq "poster") { + if ($posterlist[$i]->{size} eq "mid") { + $midposters[$j] = $posterlist[$i]->{url}; + $j++; + } + } + } + if ($j > 0) { + print($midposters[0] . "\n"); + } + print("http://hwcdn.themoviedb.org/images/no-poster.jpg" . "\n") unless ($j > 0); + } } } } @@ -279,34 +303,36 @@ "movie backdrop, stopped"; } - my $xs = new XML::Simple(SuppressEmpty => '', ForceArray => ['movie'], - KeyAttr => []); - my $xml = $xs->XMLin($response); + my $xml = XMLin($response, ForceArray => ['movie', 'poster', 'backdrop'], + KeyAttr => {poster => 'size'}); if ($xml->{"opensearch:totalResults"} > 0) { - # now get the movie data via Movie.getInfo, Movie.imdbLookup does not - # provide us all the data - my $tmdbid = $xml->{moviematches}{movie}[0]{id}; - - my ($rc, $response) = - TMDBAPIRequest('Movie.getInfo', {'id' => $tmdbid}); - - if (!defined $response) { - die "Unable to contact themoviedb.org while retrieving ". - "movie backdrop, stopped"; - } - - $xml = XMLin($response, ForceArray=> ['backdrop'], KeyAttr => ['key', 'id']); - - foreach my $backdrop (@{$xml->{moviematches}->{movie}->{backdrop}}) { - # print "$backdrop->{content}\n"; - - if ($backdrop->{size} eq "original") - { - print "$backdrop->{content}\n"; + if (exists $xml->{movies}->{movie}->[0]->{images}) + { + my @posterlist; + @posterlist = @{$xml->{movies}->{movie}->[0]->{images}->{image}} + if ref $xml->{movies}->{movie}->[0]->{images}; + if (@posterlist > 0) { + my @backdrop; + my $j = 0; + for (my $i = 0; $i < @posterlist; $i++) + { + if ($posterlist[$i]->{type} eq "backdrop") { + if ($posterlist[$i]->{size} eq "original") { + $backdrop[$j] = $posterlist[$i]->{url}; + $j++; + } + } + } + if ($j > 0) { + print($backdrop[0] . "\n"); + } + print("\n") unless ($j > 0); } } } + + } # dump Movie list: 1 entry per line, each line as 'movieid:Movie Title' @@ -337,14 +363,14 @@ if ($xml->{"opensearch:totalResults"} > 0) { my @movies; - foreach my $movie (@{$xml->{moviematches}->{movie}}) { + foreach my $movie (@{$xml->{movies}->{movie}}) { if ($movie->{type} ne 'movie') { next; } - my $movienum = $movie->{imdb}; - my $moviename = $movie->{title}; - my $release = $movie->{release}; + my $movienum = $movie->{imdb_id}; + my $moviename = $movie->{name}; + my $release = $movie->{released}; my $movieyear = 0; if ($release) { @@ -361,7 +387,7 @@ } } - for my $movie (@movies) { print "$movie\n"; } + for my $movie (@movies) { print "$movie\n"; } } }