Ruby code to quickly convert titles to ISBNs

| book, library, reading, ruby

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!

You can comment with Disqus or you can e-mail me at sacha@sachachua.com.