Commit ad60a422 authored by Daniel W Bond's avatar Daniel W Bond

ISNBMetadata utility revised to use Internet Archive's OpenLibrary, rather...

ISNBMetadata utility revised to use Internet Archive's OpenLibrary, rather than WorldCat, which is now a paid service
parent cff995c1
# standard libary imports
from datetime import datetime
# third party imports
import requests import requests
# imports from your apps
from .models import Flag, Rating, BidFlag from .models import Flag, Rating, BidFlag
# pulls worldcat metadata from ISBNs # previously pulled worldcat metadata from ISBNs, worldcat is now a paid service
# thankfully, the internet archive has stepped in with a free api replacement
def ISBNMetadata(standardISBN): def ISBNMetadata(standardISBN):
# passing in numbers starting with 0 throws "SyntaxError: invalid token" # supports both ISBN 10 and ISBN 13
url = "http://xisbn.worldcat.org/webservices/xid/isbn/" +\ key_format = 'ISBN:%s' % str(standardISBN)
str(standardISBN) +\ url = "https://openlibrary.org/api/books?bibkeys=" +\
"?method=getMetadata&format=json&fl=title,year,author,ed" key_format +\
"&format=json&jscmd=data"
# In case the API fails to return, simply return None. # In case the API fails to return, simply return None.
try: try:
...@@ -16,10 +22,28 @@ def ISBNMetadata(standardISBN): ...@@ -16,10 +22,28 @@ def ISBNMetadata(standardISBN):
return None return None
# format into a dictionary # format into a dictionary
dejson = metadata.json() json_response = metadata.json()
isbn_data = json_response.get(key_format)
try: try:
metadataDict = dejson.get('list') title = isbn_data.get('title', '')
return metadataDict[0] subtitle = isbn_data.get('subtitle', '')
if subtitle:
full_title = '%s: %s' % (title, subtitle)
else:
full_title = title
metadataDict = {'title': full_title}
date = isbn_data.get('publish_date' ,'')
# unfortunately, dates are formatted in a variety of different ways
# but year should be the final four digits regardless of, say, month formatting
metadataDict['year'] = date[-4:]
authors = isbn_data.get('authors', 'wat')
metadataDict['authors'] = ' and '.join([author.get('name', '')
for author in authors])
# unlike worldcat, openlibrary does not provide edition information
return metadataDict
except: except:
return None return None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment