Net::Amazon::DVD2IMDB - Use Amazon to convert a DVD title to an IMDB movie id.


Net-Amazon-DVD2IMDB documentation Contained in the Net-Amazon-DVD2IMDB distribution.

Index


Code Index:

NAME

Top

Net-Amazon-DVD2IMDB - Use Amazon to convert a DVD title to an IMDB movie id.

DESCRIPTION

Top

This is a module that uses Net::Amazon and LWP to acquire the associated IMDB id for a DVD. The way this is done is by using the Amazon API to first search for a DVD (by title) and find the most appropriate ASIN. The results from this are plugged into a method which scrapes the associated Amazon product page, looking for a mention of a particular movie ID (one DVD can have multiple movies associated with it). Due to the second part of this script, it's not entirely 'legit' in the eyes of Amazon, but until they make IMDB id available in their API, this is the best that we can do.

SYNOPSIS

Top

    # This script takes a DVD title from the command line, looks it
    # up using this script, and returns the associated IMDB ID(s).

    my $token = 'AMAZONTOKEN';
    my $title = join( ' ', @ARGV );

    use Net::Amazon::DVD2IMDB;

    my $ua = new Net::Amazon::DVD2IMDB( token => $token );

    print map { "$_\n" } @{$ua->convert( $title )};

new
    my $dvd2imdb = new Net::Amazon::DVD2IMDB( token => 'AMAZONTOKEN' );

Instantiates an object with which to perform the search. Requires a valid Amazon developer token.

convert
    $dvd2imdb->convert( 'DVD Title 1', 'DVD Title 2', ... );

Takes in an array of DVD titles and returns an array of IMDB ids (see asin2imdb for more information on the output format).

dvd2asin
    $dvd2imdb->dvd2asin( 'DVD Title 1', 'DVD Title 2', ... );

Takes in an array of DVD titles and returns an array of the most appropriate Amazon Product IDs for each DVD.

asin2imdb
    $dvd2imdb->asin2imdb( 'ASIN or SKU 1', 'ASIN or SKU 2', ... );

Takes in an array of Amazon Product IDs OR SKU product numbers and returns an array of IMDB ID arrays. The structure of the returned data could look something like this:

    (
        [ 'IMDB ID 1' ],
        [ 'IMDB ID 2', 'IMDB ID 3' ]
    );

AUTHOR

Top

<a href="http://ejohn.org/">John Resig</a> <jeresig@gmail.com>

DISCLAIMER

Top

This application utilitizes screen-scraping techniques, which are very fickle and susceptable to changes (on the part of Amazon). If Amazon decides to changet their site, this module may no longer work.

COPYRIGHT

Top


Net-Amazon-DVD2IMDB documentation Contained in the Net-Amazon-DVD2IMDB distribution.

package Net::Amazon::DVD2IMDB;

use LWP::Simple;
use Net::Amazon;
use Net::Amazon::Request::Keyword;

our $VERSION = 0.03;

sub new {
  my $ref = shift;
  my $class = ref( $ref ) || $ref;

  my $self = bless {
    token => undef,
    amazon => undef,
    @_
  }, $class;

  die "Amazon Token Required" unless ( $self->{token} );

  $self->{amazon} = new Net::Amazon( token => $self->{token} );

  return $self;
}

sub convert {
  my ( $self, @titles ) = @_;
  return $self->asin2imdb( $self->dvd2asin( @titles ) );
}

sub dvd2asin {
  my ( $self, @titles ) = @_;

  @titles = map {
    my $asin = undef;
    my $r = Net::Amazon::Request::Keyword->new( keyword => $_, mode => 'dvd' );
    my $resp = $self->{amazon}->request( $r );

    if( $resp->is_success() ) { 
      foreach my $item ( $resp->properties ) {
        $asin ||= $item->Asin();
      }
    }

    $asin;
  } @titles;

  wantarray ? @titles : $titles[0];
}

sub asin2imdb {
  my ( $self, @asin ) = @_;

  @asin = map { 
    my @found;
    my $page = get( "http://www.amazon.com/exec/obidos/ASIN/$_" );

    while ( $page =~ /imdb.*?\?(\d+)/ig ) {
      push( @found, $1 );
    }

    [ @found ];
  } @asin;

  wantarray ? @asin : $asin[0];
}
1;

__END__