2012-04-03 19:27:33 +00:00
|
|
|
import re
|
2011-09-29 06:23:50 +00:00
|
|
|
import json
|
|
|
|
|
2019-02-28 21:22:23 +00:00
|
|
|
import requests
|
2013-07-02 15:00:01 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2019-02-28 21:22:23 +00:00
|
|
|
import regluit.core.isbn
|
|
|
|
|
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 = []
|
2019-02-28 21:22:23 +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']
|
2019-02-28 21:22:23 +00:00
|
|
|
r = {'title': v.get('title', ""),
|
2011-11-20 14:20:08 +00:00
|
|
|
'description': v.get('description', ""),
|
|
|
|
'publisher': v.get('publisher', ""),
|
|
|
|
'googlebooks_id': item.get('id')}
|
2019-02-28 21:22:23 +00:00
|
|
|
|
2011-11-20 14:20:08 +00:00
|
|
|
# TODO: allow multiple authors
|
2020-02-12 16:12:56 +00:00
|
|
|
if 'authors' in v and len(v['authors']) == 1:
|
2014-01-18 02:37:19 +00:00
|
|
|
r['author'] = r['authors_short'] = v['authors'][0]
|
2020-02-12 16:12:56 +00:00
|
|
|
elif 'authors' in v and len(v['authors']) > 2:
|
2011-11-20 14:20:08 +00:00
|
|
|
r['author'] = v['authors'][0]
|
2019-02-28 21:22:23 +00:00
|
|
|
r['authors_short'] = '%s et al.' % v['authors'][0]
|
2020-02-12 16:12:56 +00:00
|
|
|
elif 'authors' in v and len(v['authors']) == 2:
|
2014-01-18 02:37:19 +00:00
|
|
|
r['author'] = v['authors'][0]
|
2019-02-28 21:22:23 +00:00
|
|
|
r['authors_short'] = '%s and %s' % (v['authors'][0], v['authors'][1])
|
2011-11-20 14:20:08 +00:00
|
|
|
else:
|
|
|
|
r['author'] = ""
|
|
|
|
r['isbn_13'] = None
|
2019-02-28 21:22:23 +00:00
|
|
|
|
2011-11-20 14:20:08 +00:00
|
|
|
# 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':
|
2019-02-28 21:22:23 +00:00
|
|
|
if not r['isbn_13']:
|
2011-12-20 04:26:55 +00:00
|
|
|
r['isbn_13'] = regluit.core.isbn.convert_10_to_13(i['identifier'])
|
2019-02-28 21:22:23 +00:00
|
|
|
|
2011-11-20 14:20:08 +00:00
|
|
|
# cover image
|
2020-02-12 16:12:56 +00:00
|
|
|
if 'imageLinks' in v:
|
2012-04-03 19:27:33 +00:00
|
|
|
url = v['imageLinks'].get('thumbnail', "")
|
2019-02-28 21:22:23 +00:00
|
|
|
url = re.sub(
|
|
|
|
r'http://(bks[0-9]+\.)?books\.google\.com',
|
|
|
|
'https://encrypted.google.com',
|
|
|
|
url,
|
|
|
|
)
|
2012-04-03 19:27:33 +00:00
|
|
|
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"
|
2019-02-28 21:22:23 +00:00
|
|
|
|
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)
|
2019-02-28 21:22:23 +00:00
|
|
|
return results
|
2011-09-29 06:23:50 +00:00
|
|
|
|
|
|
|
|
2012-02-05 00:06:53 +00:00
|
|
|
def googlebooks_search(q, user_ip, page):
|
2018-02-01 20:18:14 +00:00
|
|
|
if len(q) < 2 or len(q) > 2000:
|
|
|
|
return {}
|
2019-02-28 21:22:23 +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}
|
2019-02-28 21:22:23 +00:00
|
|
|
start = (page - 1) * 10
|
2012-02-05 00:06:53 +00:00
|
|
|
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
|
2019-02-28 21:22:23 +00:00
|
|
|
|
|
|
|
r = requests.get(
|
|
|
|
'https://www.googleapis.com/books/v1/volumes',
|
|
|
|
params=params,
|
|
|
|
headers=headers
|
|
|
|
)
|
2016-12-31 03:26:16 +00:00
|
|
|
# urls like https://www.googleapis.com/books/v1/volumes?q=invisible+engines&startIndex=0&maxResults=10&key=[key]
|
2011-09-29 06:23:50 +00:00
|
|
|
return json.loads(r.content)
|