Created
January 27, 2012 14:39
-
-
Save HarlanH/1689086 to your computer and use it in GitHub Desktop.
R wrapper around the ISBNdb web service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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