Home

Tags

Получение информации о товаре через Amazon API

2011-09-29 python amazon

Пример получения информации о товаре через Amazon API

Нужно зарегистрироваться на амазоне взять ключи со страницы Security Credentials, подставить их в скрипт

# coding:utf8

url = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl'
access_key = 'Your access key'
secret_key = 'Your secret key'

def get_headers(method):
    from suds.sax.element import Element
    from datetime import datetime
    import base64
    import hmac
    import hashlib

    dtime = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
    digest = hmac.new(secret_key, method+dtime, hashlib.sha256).digest()
    signature = base64.encodestring(digest).rstrip()

    res = []
    ssnns = ('ns2', 'http://security.amazonaws.com/doc/2007-01-01/')
    res.append( Element('AWSAccessKeyId', ns=ssnns).setText(access_key) )
    res.append( Element('Timestamp', ns=ssnns).setText(dtime) )
    res.append( Element('Signature', ns=ssnns).setText(signature) )
    #print res
    return res

def get_item(ItemId):
    from suds.client import Client
    client = Client(url)
    myheaders = get_headers('ItemLookup')
    client.set_options(soapheaders=myheaders)
    #print client

    d = {}
    d['AssociateTag'] = 'ASSOCIATE TAG'
    d['AWSAccessKeyId'] = access_key
    d['Request'] = { 'ItemId':ItemId, 'ResponseGroup':'Medium' }
    result = client.service['AWSECommerceServicePortUS'].ItemLookup(**d)
    #print result
    return result

def main():
    result = get_item('B00428ZRFE')
    print result
    print result.Items[0].Item[0].SalesRank

main()

comments powered by Disqus