Skip to content

Commit a4a2294

Browse files
committed
Cache google books requests
1 parent 91aedde commit a4a2294

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

pythonbits/goodreads.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,30 @@
1616
API Key"""))
1717

1818

19-
def extract_authors(authors):
19+
def _extract_authors(authors):
2020
if isinstance(authors['author'], OrderedDict):
2121
return [{
2222
'name': authors['author']['name'],
2323
'link': authors['author']['link']
2424
}]
2525
else:
26-
return [extract_author(auth)
26+
return [_extract_author(auth)
2727
for auth in authors['author']]
2828

2929

30-
def extract_author(auth):
30+
def _extract_author(auth):
3131
return {
3232
'name': auth['name'],
3333
'link': auth['link']
3434
}
3535

3636

37-
def process_book(books):
37+
def _process_book(books):
3838
keys_wanted = ['id', 'title', 'isbn', 'isbn13', 'description',
3939
'language_code', 'publication_year', 'publisher',
4040
'image_url', 'url', 'authors', 'average_rating', 'work']
4141
book = {k: v for k, v in books if k in keys_wanted}
42-
book['authors'] = extract_authors(book['authors'])
42+
book['authors'] = _extract_authors(book['authors'])
4343
book['ratings_count'] = int(book['work']['ratings_count']['#text'])
4444
return book
4545

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

5252
def show_by_isbn(self, isbn):
53-
return process_book(self.goodreads.Book.show_by_isbn(
53+
return _process_book(self.goodreads.Book.show_by_isbn(
5454
isbn).items())
5555

5656
def search(self, path):
@@ -101,5 +101,5 @@ def search(self, path):
101101
id = result['best_book']['id'].get('#text', '')
102102
log.debug("Selected Goodreads item {}", id)
103103
log.debug("Searching Goodreads by ID {}", id)
104-
return process_book(self.goodreads.Book.show(
104+
return _process_book(self.goodreads.Book.show(
105105
id).items())

pythonbits/googlebooks.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,56 @@
66

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

9+
cache = {}
10+
911

1012
def find_cover(isbn):
13+
if _get_or_set(key=isbn):
14+
return _extract_cover(cache[isbn])
15+
1116
path = 'volumes?q=isbn:{}'.format(isbn)
1217
resp = requests.get(API_URL+path)
1318
log.debug('Fetching alt cover art from {}'.format(resp.url))
1419
if resp.status_code == 200:
1520
content = json.loads(resp.content)
16-
return (content['items'][0]['volumeInfo']
17-
['imageLinks']['thumbnail'] or '')
21+
_get_or_set(key=isbn, value=content)
22+
return _extract_cover(content)
1823
else:
1924
log.warn('Couldn\'t find cover art for ISBN {}'.format(isbn))
2025
return ''
2126

2227

2328
def find_categories(isbn):
29+
if _get_or_set(key=isbn):
30+
return _extract_categories(cache[isbn])
31+
2432
path = 'volumes?q=isbn:{}'.format(isbn)
2533
resp = requests.get(API_URL+path)
2634
log.debug('Fetching categories from {}'.format(resp.url))
2735
if resp.status_code == 200:
2836
content = json.loads(resp.content)
29-
return (content['items'][0]['volumeInfo']
30-
['categories'] or '')
37+
_get_or_set(key=isbn, value=content)
38+
return _extract_categories(content)
3139
else:
3240
log.warn('Couldn\'t find categories for ISBN {}'.format(isbn))
3341
return ''
42+
43+
44+
def _get_or_set(**kwargs):
45+
value = kwargs.get('value', None)
46+
key = kwargs.get('key', None)
47+
if value:
48+
cache[key] = value
49+
return value
50+
elif key in cache:
51+
return cache[key]
52+
53+
54+
def _extract_categories(book):
55+
return (book['items'][0]['volumeInfo']
56+
['categories'] or '')
57+
58+
59+
def _extract_cover(book):
60+
return (book['items'][0]['volumeInfo']
61+
['imageLinks']['thumbnail'] or '')

0 commit comments

Comments
 (0)