Skip to content

Commit

Permalink
Cache google books requests
Browse files Browse the repository at this point in the history
  • Loading branch information
znedw committed Oct 20, 2020
1 parent 91aedde commit a4a2294
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
14 changes: 7 additions & 7 deletions pythonbits/goodreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
API Key"""))


def extract_authors(authors):
def _extract_authors(authors):
if isinstance(authors['author'], OrderedDict):
return [{
'name': authors['author']['name'],
'link': authors['author']['link']
}]
else:
return [extract_author(auth)
return [_extract_author(auth)
for auth in authors['author']]


def extract_author(auth):
def _extract_author(auth):
return {
'name': auth['name'],
'link': auth['link']
}


def process_book(books):
def _process_book(books):
keys_wanted = ['id', 'title', 'isbn', 'isbn13', 'description',
'language_code', 'publication_year', 'publisher',
'image_url', 'url', 'authors', 'average_rating', 'work']
book = {k: v for k, v in books if k in keys_wanted}
book['authors'] = extract_authors(book['authors'])
book['authors'] = _extract_authors(book['authors'])
book['ratings_count'] = int(book['work']['ratings_count']['#text'])
return book

Expand All @@ -50,7 +50,7 @@ def __init__(self, interactive=True):
developer_key=config.get('Goodreads', 'api_key'))

def show_by_isbn(self, isbn):
return process_book(self.goodreads.Book.show_by_isbn(
return _process_book(self.goodreads.Book.show_by_isbn(
isbn).items())

def search(self, path):
Expand Down Expand Up @@ -101,5 +101,5 @@ def search(self, path):
id = result['best_book']['id'].get('#text', '')
log.debug("Selected Goodreads item {}", id)
log.debug("Searching Goodreads by ID {}", id)
return process_book(self.goodreads.Book.show(
return _process_book(self.goodreads.Book.show(
id).items())
36 changes: 32 additions & 4 deletions pythonbits/googlebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,56 @@

API_URL = 'https://www.googleapis.com/books/v1/'

cache = {}


def find_cover(isbn):
if _get_or_set(key=isbn):
return _extract_cover(cache[isbn])

path = 'volumes?q=isbn:{}'.format(isbn)
resp = requests.get(API_URL+path)
log.debug('Fetching alt cover art from {}'.format(resp.url))
if resp.status_code == 200:
content = json.loads(resp.content)
return (content['items'][0]['volumeInfo']
['imageLinks']['thumbnail'] or '')
_get_or_set(key=isbn, value=content)
return _extract_cover(content)
else:
log.warn('Couldn\'t find cover art for ISBN {}'.format(isbn))
return ''


def find_categories(isbn):
if _get_or_set(key=isbn):
return _extract_categories(cache[isbn])

path = 'volumes?q=isbn:{}'.format(isbn)
resp = requests.get(API_URL+path)
log.debug('Fetching categories from {}'.format(resp.url))
if resp.status_code == 200:
content = json.loads(resp.content)
return (content['items'][0]['volumeInfo']
['categories'] or '')
_get_or_set(key=isbn, value=content)
return _extract_categories(content)
else:
log.warn('Couldn\'t find categories for ISBN {}'.format(isbn))
return ''


def _get_or_set(**kwargs):
value = kwargs.get('value', None)
key = kwargs.get('key', None)
if value:
cache[key] = value
return value
elif key in cache:
return cache[key]


def _extract_categories(book):
return (book['items'][0]['volumeInfo']
['categories'] or '')


def _extract_cover(book):
return (book['items'][0]['volumeInfo']
['imageLinks']['thumbnail'] or '')

0 comments on commit a4a2294

Please sign in to comment.