#!/usr/bin/env python # Print out the contents of my Amazon wishlist with prices, so I can look at # buying anything that's cheap. # Adam Sampson import re, os, sys, amazonxml, optparse from amazonxml import get_text, get_wishlist_items parser = optparse.OptionParser() parser.add_option("-a", dest="amazon_only", action="store_true", default=False, help="sort by Amazon's price") parser.add_option("-f", dest="one_page", action="store_true", default=False, help="only make one Amazon request") (opts, args) = parser.parse_args() wishlist_items = get_wishlist_items("QH665BMD07GI").keys() items = [] n = 1 while wishlist_items != []: these_items, wishlist_items = wishlist_items[:10], wishlist_items[10:] print "Making request %d..." % n data = amazonxml.call({ "Operation": "ItemLookup", "ItemId": ",".join(these_items), "ResponseGroup": "Small,Offers", "MerchantId": "Amazon", # doesn't affect summary "Condition": "All", "ProductPage": str(n), }) if data.getElementsByTagName("Errors") or (opts.one_page and n > 1): break for item in data.getElementsByTagName("Item"): asin = get_text(item.getElementsByTagName("ASIN")[0]) url = "http://www.amazon.co.uk/gp/offer-listing/%s/" % asin title = get_text(item.getElementsByTagName("Title")[0]) product_group = get_text(item.getElementsByTagName("ProductGroup")[0]) if product_group == "Music": postage = 1.21 else: postage = 2.75 def get_price(name): price = None els = item.getElementsByTagName(name) if els != []: amount = get_text(els[0].getElementsByTagName("Amount")[0]) currency = get_text(els[0].getElementsByTagName("CurrencyCode")[0]) if currency == "GBP": price = (float(amount) / 100.0) + postage return price # These will come from OfferSummary, covering all merchants new = get_price("LowestNewPrice") used = get_price("LowestUsedPrice") coll = get_price("LowestCollectiblePrice") # This will come from Offers, which is only Amazon amazon = get_price("Price") if amazon is not None: # I can normally swing free shipping from Amazon... amazon -= postage prices = [amazon] if not opts.amazon_only: prices += [new, used, coll] prices = [p for p in prices if p is not None] if prices == []: minprice = None else: minprice = min(prices) items.append((minprice, title, url, new, used, coll, amazon)) n += 1 items.sort() format = "%-40s %6s %6s %6s %6s" print format % ("Title", "New", "Used", "Coll", "Amazon") for minprice, title, url, new, used, coll, amazon in items: if minprice is None: continue def fp(price): if price is None: return "-" else: return "%6.2f" % price title = title.encode("UTF-8") print format % (title[:40], fp(new), fp(used), fp(coll), fp(amazon)) print " %s" % url