Code

Changes between Version 2 and Version 3 of WebServices


Ignore:
Timestamp:
06/01/07 18:42:10 (7 years ago)
Author:
Simon G. <dev@…>
Comment:

vandalism revert

Legend:

Unmodified
Added
Removed
Modified
  • WebServices

    v2 v3  
    1 == '''''1923turk grup deuksX>>''''' == 
     1Here's a quick example of accessing [http://www.amazon.com/gp/browse.html/103-7129557-6640636?%5Fencoding=UTF8&node=3435361 Amazon Web Services] using Python.  You must first download and install [http://uche.ogbuji.net/tech/4suite/amara/ Amara] and obtain a Amazon Web Services access key.  
     2{{{  
     3from httplib import HTTPConnection  
     4from urlparse import urlparse, urlunparse  
     5import amara  
     6  
     7def paramunparse(params):  
     8    result = ""  
     9    for k, v in params.iteritems():  
     10        result += k + "=" + v + "&"  
     11    return result.rstrip('&')  
     12  
     13AWSAccessKeyId = [Your Access Key ID Here]  
     14isbn = '0-3951-7711-1'  
     15# ISBN's are sometimes formatted with hyphens  
     16# The ASIN for books is equal to the ISBN with no hyphens  
     17asin = isbn.replace('-','')   
     18host = 'webservices.amazon.com'  
     19path = '/onca/xml'  
     20params = { 'Service': 'AWSECommerceService',   
     21           'AWSAccessKeyId': AWSAccessKeyId,   
     22           'Operation': 'ItemLookup',   
     23           'MerchantId': 'Featured',  
     24           'IdType': 'ASIN',  
     25           'ResponseGroup': 'Medium',  
     26           'ItemId': asin}  
     27  
     28query = urlunparse(('','',path, '', paramunparse(params), ''))  
     29print 'http://'+host+query  
     30conn = HTTPConnection(host)  
     31conn.request('GET',query)  
     32xmlresponse = conn.getresponse().read()  
     33doc = amara.parse(xmlresponse)  
     34print "Found", len(doc.ItemLookupResponse.Items.Item), "items."  
     35print "Looking at first item"  
     36item = doc.ItemLookupResponse.Items.Item[0]  
     37print "Title:", item.ItemAttributes.Title  
     38print "Authors:",   
     39for author in item.ItemAttributes.Author:  
     40    print author.__str__() + ";",  
     41  
     42print   
     43price = item.OfferSummary.LowestNewPrice.Amount #unformatted  
     44print "List Price:", item.ItemAttributes.ListPrice.FormattedPrice  
     45print "Amazon Price:", item.OfferSummary.LowestNewPrice.FormattedPrice  
     46}}}  
     47  
     48Although this article is not directly related to Django, it will undoubtly be useful for many Django newbies.