2012-04-03 19:27:33 +00:00
|
|
|
import re
|
2011-09-29 06:23:50 +00:00
|
|
|
import json
|
|
|
|
import requests
|
2011-12-20 04:26:55 +00:00
|
|
|
import regluit.core.isbn
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2013-07-02 15:00:01 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2012-02-05 00:06:53 +00:00
|
|
|
def gluejar_search(q, user_ip='69.243.24.29', page=1):
|
2011-09-29 06:23:50 +00:00
|
|
|
"""normalizes results from the google books search suitable for gluejar
|
|
|
|
"""
|
|
|
|
results = []
|
2012-02-05 00:06:53 +00:00
|
|
|
search_result=googlebooks_search(q, user_ip, page)
|
2011-11-20 14:20:08 +00:00
|
|
|
if 'items' in search_result.keys():
|
|
|
|
for item in search_result['items']:
|
|
|
|
v = item['volumeInfo']
|
|
|
|
r = {'title': v.get('title', ""),
|
|
|
|
'description': v.get('description', ""),
|
|
|
|
'publisher': v.get('publisher', ""),
|
|
|
|
'googlebooks_id': item.get('id')}
|
|
|
|
|
|
|
|
# TODO: allow multiple authors
|
|
|
|
if v.has_key('authors') and len(v['authors']) > 0:
|
|
|
|
r['author'] = v['authors'][0]
|
|
|
|
else:
|
|
|
|
r['author'] = ""
|
|
|
|
r['isbn_13'] = None
|
|
|
|
|
|
|
|
# pull out isbns
|
|
|
|
for i in v.get('industryIdentifiers', []):
|
|
|
|
if i['type'] == 'ISBN_13':
|
|
|
|
r['isbn_13'] = i['identifier']
|
2011-12-20 04:26:55 +00:00
|
|
|
elif i['type'] == 'ISBN_10':
|
|
|
|
if not r['isbn_13'] :
|
|
|
|
r['isbn_13'] = regluit.core.isbn.convert_10_to_13(i['identifier'])
|
2011-11-20 14:20:08 +00:00
|
|
|
|
|
|
|
# cover image
|
|
|
|
if v.has_key('imageLinks'):
|
2012-04-03 19:27:33 +00:00
|
|
|
url = v['imageLinks'].get('thumbnail', "")
|
|
|
|
url = re.sub(r'http://bks[0-9]+\.books\.google\.com', 'https://encrypted.google.com', url)
|
|
|
|
r['cover_image_thumbnail'] = url
|
2011-11-20 14:20:08 +00:00
|
|
|
else:
|
2011-11-23 17:28:59 +00:00
|
|
|
r['cover_image_thumbnail'] = "/static/images/generic_cover_larger.png"
|
2011-11-20 14:20:08 +00:00
|
|
|
|
|
|
|
access_info = item.get('accessInfo')
|
|
|
|
if access_info:
|
|
|
|
epub = access_info.get('epub')
|
|
|
|
if epub and epub.get('downloadLink'):
|
2011-11-23 17:28:59 +00:00
|
|
|
r['first_epub_url'] = epub['downloadLink']
|
2011-11-20 14:20:08 +00:00
|
|
|
pdf = access_info.get('pdf')
|
|
|
|
if pdf and pdf.get('downloadLink'):
|
2011-11-23 17:28:59 +00:00
|
|
|
r['first_pdf_url'] = pdf['downloadLink']
|
2011-11-20 14:20:08 +00:00
|
|
|
results.append(r)
|
2011-09-29 06:23:50 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2012-02-05 00:06:53 +00:00
|
|
|
def googlebooks_search(q, user_ip, page):
|
2011-09-29 06:23:50 +00:00
|
|
|
# XXX: need to pass IP address of user in from the frontend
|
2011-11-20 14:20:08 +00:00
|
|
|
headers = {'X-Forwarded-For': user_ip}
|
2012-02-05 00:06:53 +00:00
|
|
|
start = (page - 1) * 10
|
|
|
|
params = {'q': q, 'startIndex': start, 'maxResults': 10}
|
2013-07-02 15:00:01 +00:00
|
|
|
if hasattr(settings, 'GOOGLE_BOOKS_API_KEY'):
|
|
|
|
params['key'] = settings.GOOGLE_BOOKS_API_KEY
|
|
|
|
|
2011-09-29 06:23:50 +00:00
|
|
|
r = requests.get('https://www.googleapis.com/books/v1/volumes',
|
2012-02-05 00:06:53 +00:00
|
|
|
params=params, headers=headers)
|
2011-09-29 06:23:50 +00:00
|
|
|
return json.loads(r.content)
|