LXF 151 Perl project

Comments, suggestions and questions about Linux Format magazine and the coverdiscs

Moderators: ChrisThornett, LXF moderators

LXF 151 Perl project

Postby systemaddict » Tue Oct 25, 2011 10:09 pm

I've followed the steps in the tutorial as far as trying out the book add command but all I get is:

robert@milo:~/programming/perl$ ./book add 0330258648
Error: Forbidden

I take this to mean that Amazon has refused the request. Has anyone got this to work?
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby systemaddict » Wed Oct 26, 2011 11:09 am

I've had a look at the Net::Amazon documentation at Sourceforge and added some debugging. book seems to be doing what it should:

robert@milo:~/programming/perl$ ./book add 9781847394651
2011/10/26 11:39:40 request: params = $VAR1 = {
'ItemId' => '9781847394651',
'ResponseGroup' => 'Large',
'locale' => 'uk',
'Sort' => 'salesrank',
'ItemPage' => 1,
'Operation' => 'ItemLookup'
};

2011/10/26 11:39:40 urlstr=http://webservices.amazon.co.uk/onca/xml?AWSAccessKeyId=%2Fhome%2FrobertDesktopDownloadsrsa-APKAJKF7VYMOH27RN43Q.pem&ItemId=9781847394651&ItemPage=1&Operation=ItemLookup&ResponseGroup=Large&Service=AWSECommerceService&Sort=salesrank&Timestamp=2011-10-26T10%3A39%3A40Z&Version=2009-07-01&locale=uk&Signature=230gYjcrrPmY7FC4G3OzWnXRSRaDdD2g4iG2HPiDqNQ%3D
2011/10/26 11:39:40 Fetching http://webservices.amazon.co.uk/onca/xm ... HPiDqNQ%3D
Error: Forbidden

The key is the one I downloaded from Amazon Web Services, is it, prthaps the wrong one?
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby systemaddict » Thu Oct 27, 2011 1:16 pm

I've done some digging around the Amazon Product Advertising API website and have discovered that I was sending the wrong identification.

The AMAZON_KEY variable needs to be your Access Key ID, in my case AKIAJEW2TL6RBGT65SXA, not the filename of your public key but we then hit another snag:

robert@milo:~/programming/perl$ ./book add 0750632445
Error:Your request is missing required parameters. Required parameters include AssociateTag.

There is no mention of an Associate tag in the article or in the code on the DVD, I suspect that Amazon have changed the rules since the article was written.
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby systemaddict » Tue Nov 01, 2011 12:08 am

I've just updated my system and It looks like Net::Amazon has been updated to include the associate tag feature:

robert@milo:~/programming/perl$ ./book add 0330258648
Mandatory paramter 'associate_tag' not defined at /usr/local/share/perl/5.10.1/Net/Amazon.pm line 67.

Didn't get that error last week.
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby davorg » Wed Nov 09, 2011 2:57 pm

Hi,

I saw this error when I returned to the project to start work on the next article in the series last week.

Amazon have changed the way that the API authentication works. Now you need to include the associate tag in your requests. Net::Amazon will do that for you if you add it to your call to 'new'.

Code: Select all
my $az = Net::Amazon->new(
  token         => $ENV{AMAZON_KEY},
  secret_key    => $ENV{AMAZON_SECRET},
  associate_tag => $ENV{AMAZON_ASST_TAG},
  locale        => 'uk',
);


It's unfortunate that the changed it in the middle of this series. The really annoying thing is that the second article in the series has already been submitted with the same out of date information. It'll only be in the third article (which won't be published until next year) that I'll be able to include a correction.

Sorry about that,

Dave...
davorg
 
Posts: 2
Joined: Wed May 25, 2005 12:12 pm

Postby systemaddict » Tue Nov 15, 2011 7:35 pm

Got this working at last, got my Associate Tag (even though my web site only has an 'under construction' page), worked out that the AMAZON_SECRET variable needs to be the string which appears when you click the link next to your Access Key ID at https://aws-portal.amazon.com/gp/aws/de ... access-key.

The only issue I've now got is over ISBN numbers:

robert@milo:~/programming/perl$ ./book list
Reading:


To Read:

* Learning Perl (Randal L. Schwartz)
* Heartstone (Matthew Shardlake 5) (C. J. Sansom)

Read:

Learning Perl went in just like that, using the 10-digit ISBN number from the book itself (entered as a single 10-digit string, no dashes) but the 13-digit ISBN from Heartstone wasn't recognised and I had to get the 10-digit ISBN from Amazon's website in order to get it added.
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby davorg » Tue Nov 15, 2011 8:04 pm

Yeah, it's not really the ISBN. It's actually the Amazon ASIN. That's the ten-character version of the ISBN for books.
davorg
 
Posts: 2
Joined: Wed May 25, 2005 12:12 pm

Postby systemaddict » Tue Nov 15, 2011 11:17 pm

I've been looking at the Wikipedia page on ISBN's which says that 13 digit ISBN numbers are made up of a 3-digit country prefix (more or less), a 9-digit reference number and a check digit; it gives the formula for calculating the check digit so I'll nock up a script to convert a 13-digit ISBN to a 10-digit ISBN in the next couple of days.
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex

Postby systemaddict » Wed Nov 16, 2011 8:02 pm

Here's my script:


#!/usr/bin/perl -w
#
# isbn_convertor - a tool to derive a pre-2007 10-digit ISBN
# number from a post-2007 13-digit ISBN number.
#
# The first three digits of the ISBN-13 number can be discarded
# but the last digit is a check digit computed from the
# rest of the number and this has to be re-calculated . . .
#
print "Give us the 13 digits, no spaces or dashes: ";
$isbn13 = <STDIN>;
chomp($isbn13);
#
# create a 13-element array from the input string:
#
@isbn13 = split(//, $isbn13);
#
# calculation of the check digit is explained at
# http://en.wikipedia.org/wiki/Internatio ... ook_Number
#
$multiplier = 10;
for ($i = 3; $i <12; $i++){
$what = $isbn13[$i];
$product = $what * $multiplier;
$sum += $product;
--$multiplier;
}
$mod = $sum % 11;
$check = 11 - $mod;
if ($check =~ 10){
$check = 'X';
}
@isbn10 = @isbn13[3..11];
push(@isbn10, $check);
$asin = join("",@isbn10);
print "Your ASIN is $asin\n";


I've tested it with 14 books, I got one anomaly where I mistyped the ISBN for The Suspicions of Mr Whicher and got the ASIN for a different book although other mistypes returned errors from Amazon, I guess the check digit isn't infallible.
systemaddict
 
Posts: 9
Joined: Tue Oct 25, 2011 9:58 pm
Location: Edgware, Middlesex


Return to Magazine and coverdiscs

Who is online

Users browsing this forum: Google [Bot] and 5 guests