The Code
The code in amazon_wrap.py sets the
License argument with your developer's token, and
calls the searchByKeyword function using the
argument supplied on the
command line.
#!/usr/bin/python
# amazon_wrap.py
# A typical Amazon Web API Python script using Mark Pilgrim's
# pyAmazon Amazon Web Service API wrapper
# [http://diveintomark.org/projects/#pyamazon]
# Usage: python amazon_wrap.py <keyword>
import sys
# Use the pyAmazon Functions
import amazon
# Set your dev token
amazon.setLicense(' insert developer token here ')
# Get the Keyword from input
if sys.argv [1:]:
actor = sys.argv [1 ]
else:
sys.exit('Usage: python amazon_wrap.py <keyword>')
# Make the Request
pythonBooks = amazon.searchByKeyword(actor)
# Print the Results
for book in pythonBooks:
print book.ProductName
print "by",
authorList = book.Authors.Author
if len(authorList) < 5:
for author in authorList:
print author + ", ",
else:
print book.Authors.Author,
print book.OurPrice + "\n"
The searchByKeyword function is shorthand that the
program uses to make the Amazon request. There are several other
shorthand functions, including searchByASIN,
searchByUPC, searchByAuthor,
and searchByArtist. Check the source of
amazon.py for a full list.
The for statement at the end of the code loops
through the results. The variable names mirror Amazon's results. By
changing book.OurPrice to
book.ListPrice or book.Asin,
you can change what the script returns.