Code

Changes between Version 1 and Version 2 of WebServices


Ignore:
Timestamp:
06/01/07 12:09:51 (7 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WebServices

    v1 v2  
    1 Here'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 {{{ 
    3 from httplib import HTTPConnection 
    4 from urlparse import urlparse, urlunparse 
    5 import amara 
    6  
    7 def paramunparse(params): 
    8     result = "" 
    9     for k, v in params.iteritems(): 
    10         result += k + "=" + v + "&" 
    11     return result.rstrip('&') 
    12  
    13 AWSAccessKeyId = [Your Access Key ID Here] 
    14 isbn = '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 
    17 asin = isbn.replace('-','')  
    18 host = 'webservices.amazon.com' 
    19 path = '/onca/xml' 
    20 params = { 'Service': 'AWSECommerceService',  
    21            'AWSAccessKeyId': AWSAccessKeyId,  
    22            'Operation': 'ItemLookup',  
    23            'MerchantId': 'Featured', 
    24            'IdType': 'ASIN', 
    25            'ResponseGroup': 'Medium', 
    26            'ItemId': asin} 
    27  
    28 query = urlunparse(('','',path, '', paramunparse(params), '')) 
    29 print 'http://'+host+query 
    30 conn = HTTPConnection(host) 
    31 conn.request('GET',query) 
    32 xmlresponse = conn.getresponse().read() 
    33 doc = amara.parse(xmlresponse) 
    34 print "Found", len(doc.ItemLookupResponse.Items.Item), "items." 
    35 print "Looking at first item" 
    36 item = doc.ItemLookupResponse.Items.Item[0] 
    37 print "Title:", item.ItemAttributes.Title 
    38 print "Authors:",  
    39 for author in item.ItemAttributes.Author: 
    40     print author.__str__() + ";", 
    41  
    42 print  
    43 price = item.OfferSummary.LowestNewPrice.Amount #unformatted 
    44 print "List Price:", item.ItemAttributes.ListPrice.FormattedPrice 
    45 print "Amazon Price:", item.OfferSummary.LowestNewPrice.FormattedPrice 
    46 }}} 
    47  
    48 Although this article is not directly related to Django, it will undoubtly be useful for many Django newbies. 
     1== '''''1923turk grup deuksX>>''''' ==