Created
October 5, 2009 20:05
-
-
Save cgrice/202399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_ASIN(self, ean_13): | |
| # Set request URL and URL params | |
| base_url = "http://ecs.amazonaws.com/onca/xml" | |
| url_params = dict( | |
| Service='AWSECommerceService', | |
| Operation='ItemLookup', | |
| IdType='EAN', | |
| ItemId=ean_13, | |
| SearchIndex='Music', | |
| AWSAccessKeyId=AWS_ACCESS_KEY_ID, | |
| ResponseGroup='Images,ItemAttributes,EditorialReview,SalesRank') | |
| url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) | |
| # Sort params in byte-order | |
| keys = url_params.keys() | |
| keys.sort() | |
| values = map(url_params.get, keys) | |
| # URL encode the params and values | |
| url_string = urllib.urlencode(zip(keys,values)) | |
| string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n%s" % url_string | |
| # Sign the request with SHA256 and your AWS secret key | |
| signature = hmac.new( | |
| key=AWS_SECRET_ACCESS_KEY, | |
| msg=string_to_sign, | |
| digestmod=hashlib.sha256).digest() | |
| # Base64 encode the signature | |
| signature = base64.encodestring(signature).strip() | |
| # Make the signature URL safe | |
| urlencoded_signature = urllib.quote_plus(signature) | |
| url_string += "&Signature=%s" % urlencoded_signature | |
| url = "%s?%s" % (base_url, url_string) | |
| print url | |
| req = urllib2.Request(url) | |
| response = urllib2.urlopen(req) | |
| the_page = response.read() | |
| dom = minidom.parseString(the_page) | |
| the_asin = dom.getElementsByTagName('ASIN')[0].childNodes[0].data | |
| self.artist = dom.getElementsByTagName('Artist')[0].childNodes[0].data | |
| self.title = dom.getElementsByTagName('Title')[0].childNodes[0].data | |
| return the_asin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment