Ruby code to quickly convert titles to ISBNs

I love the Toronto Public Library system. I can’t say that enough. I particularly love how I can go on a reading spree, place holds on a gazillion books, and have them delivered to the library branch that’s about three blocks away from the house.

Ideally, of course, these books would arrive suitably spaced apart so that a new batch arrives just as I’ve finished another. This happens when I request popular books. Most of the time, though, the books that I want to read fall in the Long Tail–obscure titles, books that have fallen off the New York Times bestseller lists, and the occasional random find.

All of these books tend to descend on the unsuspecting library branch at the same time.

There were 27 books waiting for me earlier. The librarian thanked me for clearing the shelf. J- greatly enjoyed piling them into the shopping cart we had the foresight to bring. Yes, I’ve got presentations to prepare and things to do–but reading is fun, and I’m somehow going to find time to read all those books before my three-week loan period is up. I’ll probably be able to renew them, but hey, might as well try.

So I decided I might as well try tracking them on LibraryThing. Instead of typing in all the details manually, I grabbed the list of titles from my account on LibraryElf (good reminder system for books), used ISBNdb to convert the titles into ISBNs (best guess), and imported the list of ISBNs into LibraryThing. Now my profile lists 163 books–a small fraction of the books that have passed through my hands, but it’s better than nothing. Someday I might even get myself a barcode scanner so that I can just pick up the ISBNs from the book jackets.

Anyway, I promised the Ruby code I’d quickly written to convert the titles to ISBNs:

require 'net/http'
require 'CGI'
require 'open-uri'
require 'rexml/document' 

access_key = 'YOURACCESSKEYHERE'
while (s = gets)
  s.chomp!
  url = "http://isbndb.com/api/books.xml?access_key=" + access_key + "&index1=title&value1=" + CGI::escape(s)
  xml = REXML::Document.new(open(url).read)
  if (xml.elements["ISBNdb/BookList/BookData"])
    puts xml.elements["ISBNdb/BookList/BookData"].attributes["isbn"]
  end
end

Takes titles as standard input, prints out ISBNs. Enjoy!

4 responses to “Ruby code to quickly convert titles to ISBNs”

  1. Luddite Geek says:

    Laughing… couldn’t you have make that shorter?

    I’m kidding. It amazes me how great these new languages are at doing stuff.

    It reminds me of when I started to teach myself Perl. I wrote a random quote “generator” in about four lines of code. It just read a text file of quotes into a hash, generated a random number to index to the hash, and print it.

    Sorry about all the comments — lately I’ve been too lazy to blog, so I just go around and comment a lot.

    Oh wait, I meant to ask if you have a web browser tool that you use to search for book titles in your library system? I’m thinking of the FireFox search technique of highlighting text, right-clicking, and then searching Amazon, only instead of Amazon, you search the library. That would be neat.

  2. Sacha Chua says:

    Shorter? Probably, but that would’ve required more thinking. ;)

    Browsing tool – I used to have a Greasemonkey script (I modified Chris Nolan’s TPL Linky) that automatically looked up whatever book I was browsing on Amazon.com, but the recent library “upgrade” removed search by ISBN, and I haven’t gotten around to hacking together a title + author search yet. You can probably find a similar script for Amazon + your Library. It’d be automatic and totally awesome.

  3. Vincent says:

    How do I use this code to generate ISBNs from titles?

  4. Adam says:

    You probably already know: you can de-activate your TPL holds without losing your place in the queue. When your de-activated hold comes to the top of the list, you don’t get the next available book, the person after you does, and your hold stays at the top, still asleep. When you’re ready, re-activate the hold, and the next free copy goes to you.

    I have regularly bumped up against the 50-book hold limit, and if I didn’t control my holds with judiciously-placed suspends, unpredictable forces would be determining what I read next. Chaos bad.

    PS – I have an inelegant Chickenfoot script which injects (1) a link to a book’s Amazon reviews, and (2) a draggable browser bookmark link. Works with the new Dec 2008 TPL web catalog. Needs Firefox, of course. Probably convertible to GreaseMonkey with a little grief. Advise by e-mail if you want it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>