#!/usr/bin/python -tt # amazon-salesrank v1.0 # Copyright (c) 2008, John Morrissey # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. AMAZON_WS_KEY = '' from getopt import gnu_getopt, GetoptError from os.path import basename import sys if AMAZON_WS_KEY: import locale import urllib from xml.dom import minidom else: import mechanize import re from urllib2 import Request from BeautifulSoup import BeautifulSoup def usage(): print 'amazon-salesrank v1.0' print 'Usage: ' + basename(sys.argv[0]) + ' [-h|--help] [ISBN]...' print '' print ' -h, --help display this help and exit' try: options = gnu_getopt(sys.argv[1:], 'h', ['help']) except GetoptError, e: print basename(sys.argv[0]) + ': ' + str(e) usage() sys.exit(2) for option in options[0]: if option[0] == '-h' or option[0] == '--help': usage() sys.exit(2) if len(options[1]) == 0: usage() sys.exit(2) for isbn in options[1]: if AMAZON_WS_KEY: locale.setlocale(locale.LC_ALL, 'en_US') url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=%s&Operation=ItemLookup&Idtype=ISBN&ItemId=%s&ResponseGroup=SalesRank,ItemAttributes' % \ (AMAZON_WS_KEY, isbn) dom = minidom.parse(urllib.urlopen(url)) for item in dom.getElementsByTagName('Item'): attrs = item.getElementsByTagName('ItemAttributes')[0] title = attrs.getElementsByTagName('Title')[0].firstChild.data rank = locale.format('%d', int(item.getElementsByTagName('SalesRank')[0].firstChild.data), grouping=True) else: req = Request('http://www.amazon.com/s/ref=nb_ss_b/102-8927736-4556930?url=search-alias%%3Dstripbooks&field-keywords=%s&x=0&y=0' % isbn) req.add_header('User-Agent', 'amazon-salesrank 1.0') data = mechanize.urlopen(req) soup = BeautifulSoup(''.join(data.fp.readlines())) title = soup.find(attrs={'id': 'btAsinTitle'}).string rank = soup.find(id='SalesRank').\ find(text=re.compile(r'#\d+,\d+')) rank = re.sub(r'.*#(\d+,\d+).*', r'\1', rank) print '%s: #%s' % (title, rank)