#!/usr/bin/python
# matt.joyce@gmail.com
# April 2007
#
# based on work by William K Turkel http://digitalhistoryhacks.blogspot.com

import urllib
import xml.dom.minidom


# extract the data from the xml

def getelement(xmlstring,elementname):
    r=""
    doc = xml.dom.minidom.parseString(xmlstring)
    for item in doc.getElementsByTagName(elementname):
	r=item.firstChild.nodeValue
	break
    return r


# wrapper for amazon call, can do SIMILAR or LOOKUP
def AmazonAPI(asin,op="LOOKUP"):
    r=False
    #returns xml
    baseurl = 'http://webservices.amazon.com/onca/xml?'
    service = 'Service=AWSECommerceService'
    amazonid = '1G18PRQGGYW2MMNPWWR2'
    access = '&AWSAccessKeyId=' + amazonid
    if op=="SIMILAR":
        operation = '&Operation=SimilarityLookup&ItemId=' + asin
    else:
        operation = '&ResponseGroup=Medium&Operation=ItemLookup&ItemId=' +asin

    apicall=baseurl + service + access + operation
    #print apicall
    r = urllib.urlopen(apicall).read()
    return r

