2011-09-07 09:34:03 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2011-10-13 01:59:46 +00:00
|
|
|
from xml.etree import ElementTree
|
2011-09-07 09:34:03 +00:00
|
|
|
|
|
|
|
import requests
|
2011-09-09 05:38:28 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from regluit.core import models
|
|
|
|
|
2011-09-10 11:36:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-09-07 09:34:03 +00:00
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
def add_by_isbn(isbn, work=None):
|
2011-10-10 16:57:10 +00:00
|
|
|
url = "https://www.googleapis.com/books/v1/volumes"
|
|
|
|
results = _get_json(url, {"q": "isbn:%s" % isbn})
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-10-10 21:26:38 +00:00
|
|
|
if not results.has_key('items') or len(results['items']) == 0:
|
2011-10-10 16:57:10 +00:00
|
|
|
logger.warn("no google hits for %s" % isbn)
|
|
|
|
return None
|
2011-09-07 09:34:03 +00:00
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
return add_by_googlebooks_id(results['items'][0]['id'], work)
|
2011-09-07 09:34:03 +00:00
|
|
|
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
def add_by_googlebooks_id(googlebooks_id, work=None):
|
|
|
|
# don't ping google again if we already know about the edition
|
2011-10-10 16:57:10 +00:00
|
|
|
e, created = models.Edition.objects.get_or_create(googlebooks_id=googlebooks_id)
|
|
|
|
if not created:
|
|
|
|
return e
|
2011-09-07 09:34:03 +00:00
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
url = "https://www.googleapis.com/books/v1/volumes/%s" % googlebooks_id
|
|
|
|
d = _get_json(url)['volumeInfo']
|
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
e.title = d.get('title')
|
|
|
|
e.description = d.get('description')
|
|
|
|
e.publisher = d.get('publisher')
|
|
|
|
e.publication_date = d.get('publishedDate')
|
2011-10-14 04:02:19 +00:00
|
|
|
e.language = d.get('language')
|
2011-09-07 09:34:03 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
for i in d.get('industryIdentifiers', []):
|
|
|
|
if i['type'] == 'ISBN_10':
|
|
|
|
e.isbn_10 = i['identifier']
|
|
|
|
elif i['type'] == 'ISBN_13':
|
|
|
|
e.isbn_13 = i['identifier']
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
for a in d.get('authors', []):
|
|
|
|
a, created = models.Author.objects.get_or_create(name=a)
|
|
|
|
a.editions.add(e)
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
for s in d.get('categories', []):
|
|
|
|
s, created = models.Subject.objects.get_or_create(name=s)
|
|
|
|
s.editions.add(e)
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
# if we know what work to add the edition to do it
|
|
|
|
if work:
|
|
|
|
work.editions.add(e)
|
|
|
|
|
|
|
|
# otherwise we need to create a stub work
|
|
|
|
else:
|
2011-10-10 16:57:10 +00:00
|
|
|
w = models.Work.objects.create(title=e.title)
|
|
|
|
w.editions.add(e)
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-10-10 16:57:10 +00:00
|
|
|
return e
|
2011-09-09 05:38:28 +00:00
|
|
|
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
def add_related(isbn, work=None):
|
|
|
|
# make sure the seed edition is there
|
|
|
|
edition = add_by_isbn(isbn)
|
|
|
|
|
|
|
|
# this is the work everything will hang off
|
|
|
|
work = edition.work
|
|
|
|
|
|
|
|
for other_isbn in thingisbn(isbn):
|
|
|
|
# TODO: if the other book is there already we have some surgery
|
|
|
|
# to do on the works, and the wishlists
|
|
|
|
add_by_isbn(other_isbn, work)
|
|
|
|
|
2011-10-13 01:59:46 +00:00
|
|
|
def thingisbn(isbn):
|
|
|
|
url = "http://www.librarything.com/api/thingISBN/%s" % isbn
|
|
|
|
xml = requests.get(url, headers={"User-Agent": settings.USER_AGENT}).content
|
|
|
|
doc = ElementTree.fromstring(xml)
|
|
|
|
return [e.text for e in doc.findall('isbn')]
|
|
|
|
|
2011-09-10 11:36:38 +00:00
|
|
|
def _get_json(url, params={}):
|
2011-10-10 19:57:12 +00:00
|
|
|
# TODO: should X-Forwarded-For change based on the request from client?
|
|
|
|
headers = {'User-Agent': settings.USER_AGENT,
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-Forwarded-For': '69.174.114.214'}
|
2011-10-10 16:57:10 +00:00
|
|
|
params['key'] = settings.GOOGLE_BOOKS_API_KEY
|
2011-09-09 05:38:28 +00:00
|
|
|
response = requests.get(url, params=params, headers=headers)
|
2011-09-07 09:34:03 +00:00
|
|
|
if response.status_code == 200:
|
|
|
|
return json.loads(response.content)
|
|
|
|
else:
|
2011-09-10 11:36:38 +00:00
|
|
|
logger.error("unexpected HTTP response: %s" % response)
|
2011-09-07 09:34:03 +00:00
|
|
|
raise LookupFailure("GET failed: url=%s and params=%s" % (url, params))
|
|
|
|
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-09-07 09:34:03 +00:00
|
|
|
class LookupFailure(Exception):
|
|
|
|
pass
|