Skip to content

Commit

Permalink
add examples and Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
tatianass committed Jul 2, 2017
1 parent 64a9da8 commit c08cd72
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ build/
docs/site/*
dist/
goodreads.egg-info/
.coverage
.coverage
examples/*.json
*.json
29 changes: 29 additions & 0 deletions examples/search_all_pages.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions examples/search_one_page.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions goodreads/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
14 changes: 11 additions & 3 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -60,4 +60,12 @@ def test_find_groups(self):

def test_list_events(self):
events = self.client.list_events(55408)
assert len(events) > 0
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
3 changes: 0 additions & 3 deletions tests/group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit c08cd72

Please sign in to comment.