Amazon sales stats grabber

Thursday 8 September 2005This is 19 years old. Be careful.

Let’s say your wife has just published a book. Let us further suppose that you are a statistics-obsessed geek (not that there’s anything wrong with that!) You’re probably going to want a Python script that uses the Amazon web services to check the book’s sale ranking. I just happen to have one:

# Amazon stats grabber
# Ned Batchelder, http://nedbatchelder.com, September 2005.

# A list of Amazon ASIN's to get stats for:
asins = [
    '1590302443',   # Making Peace with Autism
    '1932565167',   # Thorn in My Pocket
    '0385504209',   # DaVinci Code, why not aim high? :-)
    ]

# Put your own Amazon Web Services subscription id here:
subid = '1AY..............S82'

# The Amazon response groups you want to retrieve for the items.
respgroups = 'SalesRank,ItemAttributes,Reviews'

# How you want the results formatted.
format = "%(SalesRank)7s [ %(TotalReviews)4s %(AverageRating)3s ]  %(Title)s"

# The rest of the file doesn't need to be customized.

import urllib
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
from xml import xpath

urlfmt = 'http://webservices.amazon.com/onca/xml?' \
    'Service=AWSECommerceService&SubscriptionId=%(subid)s&' \
    'Operation=ItemLookup&ItemId=%(asin)s&' \
    'ResponseGroup=%(respgroups)s'

def load_stats(asins):
    """ Returns a dictionary of stats. """
    asin = ",".join(asins)
    slots = dict(globals())
    slots.update(locals())
    url = urlfmt % slots
    f = urllib.urlopen(url)
    content = f.read()
    f.close()
    alldata = PyExpat.Reader().fromString(content)
    # To get a look at the raw data, uncomment this:
    #PrettyPrint(alldata)
    return alldata

def get_stats(alldata, asin):
    itemdata = xpath.Evaluate('.//Item[ASIN/text()="%s"]' % asin, alldata)[0]
    stats = {}
    for tag in ['SalesRank', 'Title', 'AverageRating', 'TotalReviews']:
        elts = xpath.Evaluate(".//%s/text()" % tag, itemdata)
        val = '---'
        if elts:
            val = elts[0].data
            if ':' in val:
                val = val.split(':')[0]
            val = val.strip()
        stats[tag] = val
    return stats

alldata = load_stats(asins)

for asin in asins:
    stats = get_stats(alldata, asin)
    print format % stats

When run, it will write a simple report to standard out.

Comments

[gravatar]
Then all you have to do is integrate RRDTool (http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/)
and you can have a live dashboard with eye candy.
[gravatar]
That was clever. Re: 'you seem to have come from a search engine. Your search words (amazon sales stats) are highlighted on this page...however I already had that covered by visiting via googlecache. Nice, though. Kudos.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.