Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created September 3, 2015 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/7e09329bebd75c3175e1 to your computer and use it in GitHub Desktop.
Save briandfoy/7e09329bebd75c3175e1 to your computer and use it in GitHub Desktop.
Look up an item based on Amazon ID
#!/Users/brian/bin/perls/perl5.22.0
use v5.22;
use experimental qw(postderef);
no warnings qw(experimental::postderef);
use strict;
use warnings;
use URI::Amazon::APA;
use Mojo::UserAgent;
use XML::Simple;
use Data::Dumper;
use URI::Amazon::APA;
my $u = URI::Amazon::APA->new('http://webservices.amazon.com/onca/xml');
$u->query_form(
Service => 'AWSECommerceService',
Operation => 'ItemLookup',
ItemId => $ARGV[0],
AssociateTag => $ENV{AMAZON_ASSOCIATES_TAG},
ResponseGroup => 'Large',
);
$u->sign(
key => $ENV{AWS_ACCESS_KEY},
secret => $ENV{AWS_SECRET_KEY},
);
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get($u->as_string);
if ( $tx->success ) {
my $xml = $tx->res->body;
my $perl = XMLin( $xml );
process_response( $perl );
}
else {
say "ERROR ----------\n", $tx->res->to_string;
}
sub process_response {
my( $hash ) = @_;
my $a = $hash->{Items}{Item}{ItemAttributes};
my $asin = $hash->{Items}{Item}{ASIN};
say join "\t",$asin, $a->@{qw(ISBN EAN Title PublicationDate Publisher NumberOfPages ) };
}
__END__
$VAR1 = {
'Author' => [
'Peter Roop',
'Connie Roop'
],
'PackageDimensions' => {
'Height' => {
'content' => '20',
'Units' => 'hundredths-inches'
},
'Length' => {
'content' => '880',
'Units' => 'hundredths-inches'
},
'Weight' => {
'content' => '10',
'Units' => 'hundredths-pounds'
},
'Width' => {
'Units' => 'hundredths-inches',
'content' => '600'
}
},
'ISBN' => '0439206359',
'PublicationDate' => '2001-07-01',
'Brand' => 'Cartwheel Books',
'ProductGroup' => 'Book',
'ListPrice' => {
'FormattedPrice' => '$3.99',
'Amount' => '399',
'CurrencyCode' => 'USD'
},
'ProductTypeName' => 'ABIS_BOOK',
'ItemDimensions' => {
'Width' => {
'content' => '596',
'Units' => 'hundredths-inches'
},
'Height' => {
'content' => '14',
'Units' => 'hundredths-inches'
},
'Length' => {
'Units' => 'hundredths-inches',
'content' => '896'
},
'Weight' => {
'content' => '10',
'Units' => 'hundredths-pounds'
}
},
'EANList' => {
'EANListElement' => '9780439206358'
},
'Label' => 'Cartwheel Books',
'Manufacturer' => 'Cartwheel Books',
'Publisher' => 'Cartwheel Books',
'Feature' => 'Great product!',
'Binding' => 'Paperback',
'Title' => 'Octopus Under The Sea (Hello Reader)',
'NumberOfItems' => '1',
'Languages' => {
'Language' => [
{
'Name' => 'English',
'Type' => 'Published'
},
{
'Name' => 'English',
'Type' => 'Original Language'
},
{
'Name' => 'English',
'Type' => 'Unknown'
}
]
},
'Studio' => 'Cartwheel Books',
'EAN' => '9780439206358',
'NumberOfPages' => '32'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment