Getting a list of books for a speaker from the Open Library API in Python

As part of a project I’ve been working on, I’m exploring using the Open Library API to get books someone has written.

Their API gives you ISBNs (and many other identifiers), which you presumably can use against the Amazon Product API. It is a bit sensitive to extra tokens (e.g. titles and name prefixes), so you should update the regex to match what your data has in it.

I save the results, for later processing – if the response is 66 bytes, it means nothing was found at all.

author = author.split('(')[0]
filename = 'authors/' + author + '.txt'

if (not os.path.isfile(filename)):
  author = re.sub(r'\b(Dr|Professor|Alderman)\b', r'', author.strip())
     
url = 'http://openlibrary.org/search.json?author=' + quote(author)
httpresponse = urlopen(url).read().decode('utf8')
if (len(httpresponse) > 66):
  f = open(filename, 'w')      
  f.write(httpresponse)
  f.close()

From manually inspecting a few of these, they seem to return everything (not paged), so this is a pretty simple API to use.

Leave a Reply

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