Skip to content

Instantly share code, notes, and snippets.

@HarlanH
Created January 27, 2012 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarlanH/1689086 to your computer and use it in GitHub Desktop.
Save HarlanH/1689086 to your computer and use it in GitHub Desktop.
R wrapper around the ISBNdb web service
library(XML)
library(RCurl)
# USAGE: ldply(c('9780387962406', '9780387961406', '0387981403'), function(x) ISBNdb(x, 'apikey'))
ISBNdb <- function(isbn, access_key,
isbn.api='http://isbndb.com/api/books.xml?access_key=%s&index1=isbn&value1=%s') {
isbn <- as.character(isbn)
stopifnot(length(isbn)==1, (nchar(isbn)==10 || nchar(isbn)==13))
stopifnot(length(access_key)==1)
query <- sprintf(isbn.api, access_key, isbn)
isbn.xml <- xmlParse(getURL(query))
bookdata <- getNodeSet(isbn.xml, '//BookData')
if (length(bookdata) == 0) {
warning(paste('No results found for ISBN', isbn))
return(NULL)
}
cbind(xmlToDataFrame(bookdata),
as.data.frame(t(xmlAttrs(bookdata[[1]]))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment