Friday Feb 22, 2008

Another Python Library Script

Something I've been meaning to do for a while.

When I go to the library, I'll first look in the "new books" section. If there is nothing there that I'm interested in, I'll look for books off one or more lists that I have. One of those lists is for the books on my Amazon Wish Lists.

My library is part of the Santa Clara County library system. Several branches work together. If the book is in the county library system, but not available at my local branch, I can put in a request, and they'll ship a copy to me as soon as one is available.

It's also possible that my local branch has a copy and it's out. What I really want to know is which books I'm interested in are available now in my local branch, so I can grab them if I immediately visit the library.

This script helps me do that. Here's how it works. For each of the books on each of the given Amazon Wish List ID's, it'll extract the ISBN and use that to query my libraries online catalog.

It'll first check the HTML reply, looking for the string "Sorry, could not find anything matching". If it finds, that, it'll go onto the next book. If it doesn't find it, then the county library has at least one copy of the book. It then looks for the string "Los Altos Library". If it finds that, it'll then grab the reply from that point upto the sub-string "Add Copy to MyList" and divide it into tokens, using "<" as a separator. It'll then look for tokens that start with "a class" and grab the sub-string from the ">" character to the end of the string. When that's complete for all the tokens, it'll check to see if the last one is "In". If it is, we've found a book that's in at my local library, and the book results are written to standard out. The script also writes a few messages to stderr that give information on the books that are in the county library system and/or are available from my local branch but are not currently in.

Here's a partial listing of what the program output to stderr looks like as it's running:

$ python ./check_los_altos.py >booklist.txt
...
Found in County Library: Watercolor: Painting Smart
Found in County Library: Chaos and Fractals: New Frontiers of Science
Los Altos Library has a copy
Found in County Library: Ships-In-Bottles: A Step-By-Step Guide to a Venerable Nautical Craft
Los Altos Library has a copy
Currently IN
Found in County Library: Complete Stories of Robert Bloch: Final Reckonings (Complete Stories of Robert Bloch)
Los Altos Library has a copy
Currently IN
Found in County Library: Looking for Jake: Stories
Los Altos Library has a copy
Found in County Library: 123 Robotics Experiments for the Evil Genius (TAB Robotics)
Los Altos Library has a copy
Found in County Library: The Art and Craft of Paper Sculpture: A Step-By-Step Guide to Creating 20 Outstanding and Original Paper Projects
Found in County Library: Bad Science: The Short Life and Weird Times of Cold Fusion
...
$

The booklist.txt entries for those two books above that are found in my local library look like:

Ships-In-Bottles: A Step-By-Step Guide to a Venerable Nautical Craft  Nonfiction Section  745.5928 HUBBARD  In
Complete Stories of Robert Bloch: Final Reckonings (Complete Stories of Robert Bloch)  Science Fiction Section  SF BLOCH ROBERT  In

As you can see, the output even tells me which section of the library to look in for each book.

For anybody who wants to use the script as the basis for doing something similar for their library, you are going to have to make small changes in four areas. Three are trivial. The fourth will take a little Python programming.

  • You'll need to replace the "KKKKKKKKKKKKKKKKKKKK" on the line:
    amazonAccessKey = "KKKKKKKKKKKKKKKKKKKK"
    with your own Amazon Access key. If you don't already have one, then click the "Sign up now" link on the right side of their Amazon Web Services web page.

  • You'll need to replace the "XXXXXXXXXXXXX", "YYYYYYYYYYYYY" on the line:
    amazonWishListIDs = [ "XXXXXXXXXXXXX", "YYYYYYYYYYYYY" ]
    with the ID's of the Amazon wish lists you are interested in. One way to find out the Amazon wish list ID, is to view the wish list, then click on the "Edit list information" link near the top left of the web page. You'll be taken to another web page with a URL of the form:

    https://www.amazon.com/gp/registry/wishlist/XXXXXXXXXXXXX/settings.html?ie=UTF8&ref%5F=cm%5Fwl%5Fedit&in-sign-in=1&page=settings.m
          

    The "XXXXXXXXXXXXX" part is the wish list ID.

  • The URL of your libraries online catalog. This old post of mine and Jon Udell's LibraryLookup Bookmarklet Generator should help with that.

  • And the last part is where you'll need to cut some Python code. The checkLibrary() routine will need to be rewritten to search for the appropriate strings on the HTML web page that your library catalog generates.

For the Python naming pedants, I've found that I simply like CamelCase variable names better that the current Python naming "standard", so you'll going to have to just deal with it. Other constructive Python criticisms are appreciated.

As I was writing this, I realized I didn't take into consideration the case where my local library might have multiple copies of the same book, and one or more of them might be in, even though the first one wasn't.

But that'll be a fix for the next version.

[]

[]

[]

About

user12607856

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today