#!/usr/bin/env python # # check_books.py - v0.1 # # Copyright (c) 2007 - 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 the pyAmazon Amazon Web Service API wrapper maintained by # Michael Josephson. # [http://www.josephson.org/projects/pyamazon/] # # 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 amazon 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 ------ # Maximum number of results returned per page. # MAX_RESULTS = 10 # 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: TEXT += "Url: %s%s\n" % (libraryURL, str(book.Asin)) TEXT += "ISBN: %s\n" % str(book.Asin) TEXT += "Title: %s\n" % (''.join([ x[0] for x in book.ProductName ])) TEXT += "Author(s): %s\n\n\n" % \ (''.join([ x[0] for x in book.Authors.Author ])) message = messageTemplate % (FROM, ", ".join(TO), SUBJECT, TEXT) server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, message) server.quit() def checkLibrary(book): url = libraryURL + str(book.Asin) bookInfo = urllib2.urlopen(url) contents = bookInfo.read() if contents.find("Sorry, could not find anything matching") == -1: foundBooks.append(book) def processResults(books): for book in books: checkLibrary(book) def checkWishList(amazonWishListID): pageNo = 1 finished = False while not finished: results = amazon.searchByWishlist(amazonWishListID, page=pageNo) if len(results) != 0: processResults(results) if len(results) == MAX_RESULTS: pageNo += 1 else: finished = True if foundBooks: sendEmail(foundBooks) if __name__ == "__main__": amazon.setLicense(amazonAccessKey) checkWishList(amazonWishListID)