2011-09-29 06:23:50 +00:00
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
|
|
|
|
def gluejar_search(q):
|
|
|
|
"""normalizes results from the google books search suitable for gluejar
|
|
|
|
"""
|
|
|
|
results = []
|
|
|
|
|
|
|
|
for item in googlebooks_search(q)['items']:
|
|
|
|
v = item['volumeInfo']
|
|
|
|
r = {'title': v.get('title', ""),
|
|
|
|
'description': v.get('description', ""),
|
|
|
|
'publisher': v.get('publisher', ""),
|
2011-10-10 16:57:10 +00:00
|
|
|
'googlebooks_id': item.get('id')}
|
2011-09-29 06:23:50 +00:00
|
|
|
|
|
|
|
# TODO: allow multiple authors
|
|
|
|
if v.has_key('authors') and len(v['authors']) > 0:
|
|
|
|
r['author'] = v['authors'][0]
|
|
|
|
else:
|
|
|
|
r['author'] = ""
|
2011-09-29 19:07:16 +00:00
|
|
|
r['isbn_10'] = None
|
|
|
|
r['isbn_13'] = None
|
2011-09-29 06:23:50 +00:00
|
|
|
|
|
|
|
# pull out isbns
|
|
|
|
for i in v.get('industryIdentifiers', []):
|
|
|
|
if i['type'] == 'ISBN_13':
|
|
|
|
r['isbn_13'] = i['identifier']
|
|
|
|
if i['type'] == 'ISBN_10':
|
|
|
|
r['isbn_10'] = i['identifier']
|
|
|
|
|
|
|
|
# cover image
|
|
|
|
if v.has_key('imageLinks'):
|
2011-11-07 16:04:09 +00:00
|
|
|
r['image'] = v['imageLinks'].get('thumbnail', "")
|
2011-09-29 06:23:50 +00:00
|
|
|
else:
|
|
|
|
r['image'] = ""
|
|
|
|
|
2011-11-07 20:39:02 +00:00
|
|
|
access_info = item.get('accessInfo')
|
|
|
|
if access_info:
|
|
|
|
epub = access_info.get('epub')
|
|
|
|
if epub and epub.get('downloadLink'):
|
|
|
|
r['epub'] = epub['downloadLink']
|
|
|
|
pdf = access_info.get('pdf')
|
|
|
|
if pdf and pdf.get('downloadLink'):
|
|
|
|
r['pdf'] = pdf['downloadLink']
|
2011-09-29 06:23:50 +00:00
|
|
|
results.append(r)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def googlebooks_search(q):
|
|
|
|
# XXX: need to pass IP address of user in from the frontend
|
|
|
|
headers = {'X-Forwarded-For': '69.243.24.29'}
|
|
|
|
r = requests.get('https://www.googleapis.com/books/v1/volumes',
|
|
|
|
params={'q': q}, headers=headers)
|
|
|
|
return json.loads(r.content)
|