Hack 2 Find a CD's ASIN with the UPC

figs/beginner.giffigs/hack02.gif

Instead of searching by Title or Artist, you can find an ASIN for a CD by using the UPC.

Almost every product has a UPC (Universal Price Code). Amazon doesn't offer product searches by UPC through their main web site, but they do offer it for music products through their Web Services.

Following is a simple bit of JavaScript code that brings up a CD's product detail page based on a UPC.

To run this script you'll just need a developer's token Section 6.4 and Internet Explorer for Windows.

2.1 The Code

Save this piece of HTML as a text file on your computer. Name it something appropriate (cd_asin.html will do nicely).

<html>

<head>

<title>Find a CD's ASIN</title>

<script language="JavaScript">

function getDetailPage(upc) {

    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    xmlhttp.Open("GET", "http://xml.amazon.com/onca/xml2?t=webservices-[RETURN]
20&dev-t=insert developer'stoken&UpcSearch="+upc+"&mode=music&type=lite[RETURN]
&f=xml", false);
    xmlhttp.Send(  );
    var response = xmlhttp.responseXML;
    if (response.selectSingleNode("/ProductInfo/ErrorMsg")) {
        alert(response.selectSingleNode("/ProductInfo/ErrorMsg").text);
    } else {
        var asin = response.selectSingleNode("/ProductInfo/Details/Asin")[RETURN]
.text;
        document.location = "http://amazon.com/o/ASIN/" + asin;
    }
}

</script>

</head>

<body>

<form>
    <input name="upc" type="text" size="25">
    <input type="button" value="Go" 
    onClick="getDetailPage(document.forms[0].upc.value);">
</form>

</body>

</html>

2.2 Running the Hack

Point your browser at the HTML page, cd_asin.html, enter a UPC code into the form, and click the Go button. If a match is found, you'll be taken to that CD's product detail page where you can jot down the ASIN.