#!/usr/bin/env python # # check_books.py - v0.2 # # Copyright (c) 2007-2008 - Rich Burridge - Sun Microsystems Inc. # All Rights Reserved. # # Script to check my Amazon wish list to see if any of the books are # now available at my local library. If they are, then an email is # automatically sent to me containing the URL's of those books in the # libraries online web catalog. # # 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 smtplib import sys import urllib2 # ------ START OF CONFIGURABLE SECTION ---- # Amazon Access License Key. # amazonAccessKey = "XXXXXXXXXXXXXXXXXXXX" # Amazon Wishlist ID. # amazonWishListID = "YYYYYYYYYYYYY" # Email address to sent results to. # emailAddr = "somebody@somewhere.com" # Url to access my local library system. The book ISBN is appended to it. # libraryURL = "http://146.74.92.11/ipac20/ipac.jsp?index=ISBNEX&term=" # ------ END OF CONFIGURABLE SECTION ------ # List of books found at the library. # foundBooks = [] # Email message template. # messageTemplate = """\ From: %s To: %s Subject: %s %s """ def sendEmail(foundBooks): SERVER = "localhost" FROM = emailAddr TO = [ emailAddr] SUBJECT = "New Books at Library." TEXT = "" for book in foundBooks: results = ecs.ItemLookup(book.Item.ASIN, ResponseGroup='Request,Small') TEXT += "Url: %s%s\n" % (libraryURL, str(results[0].ASIN)) TEXT += "ISBN: %s\n" % str(results[0].ASIN) TEXT += "Title: %s\n" % \ (''.join([ x[0] for x in results[0].Title ])) try: TEXT += "Author(s): %s\n\n\n" % \ (''.join([ x[0] for x in results[0].Author ])) except AttributeError: pass TEXT += "\n\n" message = messageTemplate % (FROM, ", ".join(TO), SUBJECT, TEXT) server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, message) server.quit() def checkLibrary(book): print "Checking Title: ", book.Item.Title url = libraryURL + str(book.Item.ASIN) bookInfo = urllib2.urlopen(url) contents = bookInfo.read() if contents.find("Sorry, could not find anything matching") == -1: print "FOUND: ", book.Item.Title foundBooks.append(book) def checkWishList(amazonWishListID): results = ecs.ListLookup('WishList', amazonWishListID, ResponseGroup='ListItems') for book in results: checkLibrary(book) if foundBooks: sendEmail(foundBooks) if __name__ == "__main__": ecs.setLicenseKey(amazonAccessKey) checkWishList(amazonWishListID) # ChangeLog # 8th April 2008 - 0.2 - richb - version using ecs.py. # (Thanks to Gene Kim and Kun Xi for their help) # 2nd April 2007 - 0.1 - richb - Initial version (using pyamazon).