#!/usr/bin/python # # cheap_books.py - v0.3 # # Copyright (c) 2007-2008 - Rich Burridge - Sun Microsystems Inc. # All Rights Reserved. # # Usage: python cheap_books.py # # Find out if any of the books you are interested in, are available used # from Amazon, below a given price. If there are, then an email is # automatically sent to me containing the URL's of those books. # # Looks for a file called "books.py" in the same directory as this script, # and uses that as input. This will have been created by the make_book_list.py # script. # # Uses PyAWS, a Python wrapper for the latest Amazon Web Service. # Kun Xi. # [http://pyaws.sourceforge.net/] # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. import ecs import os import smtplib import sys import time # ------ START OF CONFIGURABLE SECTION ---- # Amazon Access License Key. # amazonAccessKey = "XXXXXXXXXXXXXXXXXXXX" # Email address to sent results to. # emailAddr = "someone@somewhere.com" # ------ END OF CONFIGURABLE SECTION ------ # List of books found below the given price. # foundBooks = [] # Email message template. # messageTemplate = """\ From: %s To: %s Subject: %s %s """ scriptDir = os.path.dirname(sys.argv[0]) sys.path.insert(0, scriptDir) try: import books except ImportError: print "Unable to find `books.py`" sys.exit(1) def sendEmail(foundBooks): SERVER = "localhost" FROM = emailAddr TO = [ emailAddr] SUBJECT = "Amazon Used Books Found Below Your Desired Price." TEXT = "" for book in foundBooks: TEXT += "\nTitle: %s\n" % (''.join([ x[0] for x in book.Title ])) TEXT += "Used Price: %s\n" % \ str(book.OfferSummary.LowestUsedPrice.FormattedPrice) TEXT += "Url: http://www.amazon.com/exec/obidos/ASIN/%s/am841-20\n\n" \ % str(book.ASIN) message = messageTemplate % (FROM, ", ".join(TO), SUBJECT, TEXT) server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, message) server.quit() def checkBookList(list): for asin, price, title in list: # Lookup a book on Amazon by ASIN. print "Checking Title: ", title results = ecs.ItemLookup(asin, ResponseGroup='Large') try: usedPrice = results[0].OfferSummary.LowestUsedPrice.FormattedPrice if float(usedPrice[1:]) < float(price[1:]): print "FOUND: Title: ", title foundBooks.append(results[0]) except: pass if foundBooks: sendEmail(foundBooks) if __name__ == "__main__": ecs.setLicenseKey(amazonAccessKey) checkBookList(books.list_of_books) # ChangeLog # 9th April 2008 - 0.3 - richb - version using ecs.py. # 25th February 2008 - 0.2 - richb - Add 1 second sleep to prevent 503 errors. # 3rd September 2007 - 0.1 - richb - Initial version.