diff --git a/.gitignore b/.gitignore index 59382eb..e00abf0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ build/ docs/site/* dist/ goodreads.egg-info/ -.coverage \ No newline at end of file +.coverage +examples/*.json +*.json \ No newline at end of file diff --git a/examples/search_all_pages.py b/examples/search_all_pages.py new file mode 100644 index 0000000..3d57f7b --- /dev/null +++ b/examples/search_all_pages.py @@ -0,0 +1,29 @@ +from goodreads import client +from goodreads import apikey +import json + +def writeToJSONFile(path, fileName, data): + filePathNameWExt = './' + path + '/' + fileName + '.json' + with open(filePathNameWExt, 'w') as fp: + json.dump(data, fp) + +#authetification +gc = client.GoodreadsClient(apikey.key, apikey.secret) + +#get all books by query +books = gc.search_books_all_pages(q='Gerri Hill', search_field='author') + +print(books) +#to json +data = [] +for book in books: + i = {} + i['isbn'] = book.isbn + i['isbn13'] = book.isbn13 + i['title'] = book.title + i['author'] = book.authors[0].name + i['link'] = book.link + i['popular_shelves'] = [shelve.name for shelve in book.popular_shelves] + data.append(i) + +writeToJSONFile('','search_all_pages',data) diff --git a/examples/search_one_page.py b/examples/search_one_page.py new file mode 100644 index 0000000..75c589e --- /dev/null +++ b/examples/search_one_page.py @@ -0,0 +1,28 @@ +from goodreads import client +from goodreads import apikey +import json + +def writeToJSONFile(path, fileName, data): + filePathNameWExt = './' + path + '/' + fileName + '.json' + with open(filePathNameWExt, 'w') as fp: + json.dump(data, fp) + +#authetification +gc = client.GoodreadsClient(apikey.key, apikey.secret) + +#get all books by query +books = gc.search_books(q='Gerri Hill', search_field='author') + +#to json +data = [] +for book in books: + i = {} + i['isbn'] = book.isbn + i['isbn13'] = book.isbn13 + i['title'] = book.title + i['author'] = book.authors[0].name + i['link'] = book.link + i['popular_shelves'] = [shelve.name for shelve in book.popular_shelves] + data.append(i) + +writeToJSONFile('','search_one_page',data) diff --git a/goodreads/client.py b/goodreads/client.py index a6a35a1..1187594 100644 --- a/goodreads/client.py +++ b/goodreads/client.py @@ -113,6 +113,40 @@ def search_books(self, q, page=1, search_field='all'): works = [works] return [self.book(work['best_book']['id']['#text']) for work in works] + def search_books_total_pages(self, q, page=1, search_field='all'): + """Get the total of pages from the search. This will search all + books in the title/author/ISBN fields and show matches, sorted by + popularity on Goodreads. + :param q: query text + :param page: which page to return (default 1) + :param search_fields: field to search, one of 'title', 'author' or + 'genre' (default is 'all') + """ + resp = self.request("search/index.xml", + {'q': q, 'page': page, 'search[field]': search_field}) + total = resp['search']['total-results'] + books_per_page = 20 + + #convert to integer plus 1 in case that the result it's a float + pages = int(int(total)/books_per_page) + 2 + return pages + + def search_books_all_pages(self, q, page=1, search_field='all'): + """Get all books for the given query. This will search all + books in the title/author/ISBN fields and show matches, sorted by + popularity on Goodreads. + :param q: query text + :param page: which page to return (default 1) + :param search_fields: field to search, one of 'title', 'author' or + 'genre' (default is 'all') + """ + pages = self.search_books_total_pages(q=q, search_field=search_field) + books = [self.search_books(q, p, search_field) for p in list(range(1,pages))] + outputlist = [] + for i in books: + outputlist.extend(i) + return outputlist + def group(self, group_id): """Get info about a group""" resp = self.request("group/show", {'id': group_id}) diff --git a/tests/client_test.py b/tests/client_test.py index 31e118c..ce82d76 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -40,8 +40,8 @@ def test_book_by_id(self): eq_(book.gid,book_id) def test_search_books(self): - books = self.client.search_books("The selfish gene") - assert len(books) > 0 + books = self.client.search_books(q='Gerri Hill', search_field='author') + assert len(books) == 20 assert all(isinstance(book, GoodreadsBook) for book in books) def test_search_books_with_one_book(self): @@ -60,4 +60,12 @@ def test_find_groups(self): def test_list_events(self): events = self.client.list_events(55408) - assert len(events) > 0 \ No newline at end of file + assert len(events) > 0 + + def test_search_books_total_pages(self): + num_pages = self.client.search_books_total_pages(q='Gerri Hill', search_field='author') + eq_(num_pages, 2) + + def test_search_books_all_pages(self): + books = self.client.search_books_all_pages(q='Gerri Hill', search_field='author') + assert len(books) > 20 diff --git a/tests/group_test.py b/tests/group_test.py index 620bfc1..1b4218b 100644 --- a/tests/group_test.py +++ b/tests/group_test.py @@ -33,9 +33,6 @@ def test_rules(self): def test_image_url(self): eq_(self.group.image_url,'https://images.gr-assets.com/groups/1182455834p2/1.jpg') - def test_last_activity_at(self): - eq_(self.group.last_activity_at,'Sun Jul 02 12:08:59 -0700 2017') - def test_access(self): eq_(self.group.access,'public')