Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated examples to work with current version of API.

  • Loading branch information...
commit dcd51935b832a009d1c7f82d7edb05b9dbe772ce 1 parent cbb9e00
@redtoad authored
View
51 examples/all-galileo-titles.py
@@ -3,39 +3,34 @@
Get all books published by "Galileo Press".
"""
-from config import AWS_KEY, SECRET_KEY
from amazonproduct.api import API
+import lxml
if __name__ == '__main__':
- api = API(AWS_KEY, SECRET_KEY, 'de')
-
- for root in api.item_search('Books', Publisher='Galileo Press',
- ResponseGroup='Large'):
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = API(locale='de')
+
+ result = api.item_search('Books', Publisher='Galileo Press',
+ ResponseGroup='Large')
+
+ # extract paging information
+ total_results = result.results
+ total_pages = len(result) # or result.pages
- # extract paging information
- total_results = root.Items.TotalResults.pyval
- total_pages = root.Items.TotalPages.pyval
- try:
- current_page = root.Items.Request.ItemSearchRequest.ItemPage.pyval
- except AttributeError:
- current_page = 1
-
- print 'page %d of %d' % (current_page, total_pages)
+ for book in result:
+
+ print 'page %d of %d' % (result.current, total_pages)
#~ from lxml import etree
- #~ print etree.tostring(root, pretty_print=True)
-
- nspace = root.nsmap.get(None, '')
- books = root.xpath('//aws:Items/aws:Item',
- namespaces={'aws' : nspace})
+ #~ print etree.tostring(book, pretty_print=True)
- for book in books:
- print book.ASIN,
- print unicode(book.ItemAttributes.Author), ':',
- print unicode(book.ItemAttributes.Title),
- if hasattr(book.ItemAttributes, 'ListPrice'):
- print unicode(book.ItemAttributes.ListPrice.FormattedPrice)
- elif hasattr(book.OfferSummary, 'LowestUsedPrice'):
- print u'(used from %s)' % book.OfferSummary.LowestUsedPrice.FormattedPrice
-
+ print book.ASIN,
+ print unicode(book.ItemAttributes.Author), ':',
+ print unicode(book.ItemAttributes.Title),
+ if hasattr(book.ItemAttributes, 'ListPrice'):
+ print unicode(book.ItemAttributes.ListPrice.FormattedPrice)
+ elif hasattr(book.OfferSummary, 'LowestUsedPrice'):
+ print u'(used from %s)' % book.OfferSummary.LowestUsedPrice.FormattedPrice
+
View
11 examples/get-cover.py
@@ -10,8 +10,7 @@
import sys
import urllib2
-from config import AWS_KEY, SECRET_KEY
-from amazonproduct.api import API
+from amazonproduct.api import API, HOSTS
ASIN = 'ASIN'
EAN = 'EAN'
@@ -38,6 +37,8 @@ def fetch_image(url, dest_path):
help='ID is an Universal Product Code (UPC).')
parser.add_option('--sku', action='store_const', dest='id_type', const=SKU,
help='ID is an Stock Keeping Unit (SKU).')
+ parser.add_option('--locale', choices=HOSTS.keys(), dest='locale',
+ help='Amazon locale to use [default: %default].', default='de')
parser.add_option('-q', '--quiet', action='store_false', dest='verbose',
help='Suppress output.')
@@ -45,8 +46,10 @@ def fetch_image(url, dest_path):
if len(ids) == 0:
parser.error('No IDs specified!')
-
- api = API(AWS_KEY, SECRET_KEY, 'de')
+
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = API(locale=options.locale)
params = {
'ResponseGroup' : 'Images',
View
3  examples/node-browser.py
@@ -6,7 +6,6 @@
"""
import gtk
-from config import AWS_KEY, SECRET_KEY
from amazonproduct.api import API
#: a list of root nodes retrieved from
@@ -208,7 +207,7 @@ def __init__(self, locale='de'):
self.connect("delete_event", self.on_delete)
self.locale = locale
- self.api = API(AWS_KEY, SECRET_KEY, self.locale)
+ self.api = API(locale=self.locale)
# create a TreeStore with one string column to use as the model
self.treestore = gtk.TreeStore(int, str)
View
5 examples/operations-in-batch-mode.py
@@ -1,5 +1,4 @@
-from config import AWS_KEY, SECRET_KEY
from amazonproduct.api import API
from amazonproduct.errors import AWSError
@@ -68,7 +67,9 @@ def collect_batch_results(self):
if __name__ == '__main__':
- api = BatchModeAPI(AWS_KEY, SECRET_KEY, 'us')
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = BatchModeAPI(locale='us')
# batch operation: up to 2 operations with one request
api.start_batch_request()
View
12 examples/reviews.py
@@ -5,9 +5,7 @@
"""
import sys
-from textwrap import fill
-from config import AWS_KEY, SECRET_KEY
from amazonproduct.api import API
if __name__ == '__main__':
@@ -20,14 +18,16 @@
for isbn in sys.argv[1:]:
isbn = isbn.replace('-', '')
-
- api = API(AWS_KEY, SECRET_KEY, 'us')
+
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = API(locale='us')
for root in api.item_lookup(isbn, IdType='ISBN',
SearchIndex='Books', ResponseGroup='EditorialReview'):
nspace = root.nsmap.get(None, '')
reviews = root.xpath('//aws:EditorialReview',
namespaces={'aws' : nspace})
for review in reviews:
- print str(review.Source)
+ print unicode(review.Source)
print '-' * 40
- print str(review.Content)
+ print unicode(review.Content)
View
5 examples/similar.py
@@ -5,11 +5,12 @@
"""
from amazonproduct.api import API
-from config import AWS_KEY, SECRET_KEY
if __name__ == '__main__':
- api = API(AWS_KEY, SECRET_KEY, 'us')
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = API(locale='us')
root = api.similarity_lookup('0451462009')
#~ from lxml import etree
View
44 examples/using-beautifulsoup.py
@@ -0,0 +1,44 @@
+
+"""
+Find similar items to "Small Favor: A Novel of the Dresden Files"
+(ASIN 0451462009).
+"""
+
+from amazonproduct.api import API
+from amazonproduct.errors import AWSError
+from amazonproduct.processors import BaseProcessor
+
+import BeautifulSoup
+
+
+class SoupProcessor (BaseProcessor):
+
+ """
+ Custom response parser using BeautifulSoup to parse the returned XML.
+ """
+
+ def parse(self, fp):
+
+ soup = BeautifulSoup.BeautifulSoup(fp.read())
+
+ # parse errors
+ for error in soup.findAll('error'):
+ code = error.find('code').text
+ msg = error.find('message').text
+ raise AWSError(code, msg)
+
+ return soup
+
+if __name__ == '__main__':
+
+ # Don't forget to create file ~/.amazon-product-api
+ # with your credentials (see docs for details)
+ api = API(locale='us', processor=SoupProcessor())
+ result = api.item_lookup('0718155157')
+
+ print result
+
+ # ...
+ # now do something with it!
+
+
View
40 examples/using-xml-minidom.py
@@ -1,40 +0,0 @@
-
-"""
-Find similar items to "Small Favor: A Novel of the Dresden Files"
-(ASIN 0451462009).
-"""
-
-from config import AWS_KEY, SECRET_KEY
-from amazonproduct.api import API
-from amazonproduct.errors import AWSError
-
-from xml.dom.minidom import parse
-
-
-def minidom_response_parser(fp):
- """
- Custom response parser using xml.dom.minidom.parse
- instead of lxml.objectify.
- """
- root = parse(fp)
-
- # parse errors
- for error in root.getElementsByTagName('Error'):
- code = error.getElementsByTagName('Code')[0].firstChild.nodeValue
- msg = error.getElementsByTagName('Message')[0].firstChild.nodeValue
- raise AWSError(code, msg)
-
- return root
-
-if __name__ == '__main__':
-
- api = API(AWS_KEY, SECRET_KEY, 'us',
- processor=minidom_response_parser)
- root = api.item_lookup('0718155157')
-
- print root.toprettyxml()
-
- # ...
- # now do something with it!
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.