Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: iterate over results pages when searching for artist #278

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lyricsgenius/api/public_methods/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def search(self, search_term, per_page=None, page=None, type_=''):
Args:
search_term (:obj:`str`): A term to search on Genius.
per_page (:obj:`int`, optional): Number of results to
return per request. It can't be more than 50.
return per request. It can't be more than 5.
page (:obj:`int`, optional): Paginated offset (number of the page).
text_format (:obj:`str`, optional): Text format of the results
('dom', 'html', 'markdown' or 'plain').
Expand Down
21 changes: 16 additions & 5 deletions lyricsgenius/genius.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self, access_token=None,
skip_non_songs=True, excluded_terms=None,
replace_default_terms=False,
retries=0,
per_page=5,
):
# Genius Client Constructor
super().__init__(
Expand All @@ -84,6 +85,7 @@ def __init__(self, access_token=None,
self.verbose = verbose
self.remove_section_headers = remove_section_headers
self.skip_non_songs = skip_non_songs
self.per_page = per_page

excluded_terms = excluded_terms if excluded_terms is not None else []
if replace_default_terms:
Expand Down Expand Up @@ -501,11 +503,20 @@ def find_artist_id(search_term):

# Perform a Genius API search for the artist
found_artist = None
response = self.search_all(search_term)
found_artist = self._get_item_from_search_response(response,
search_term,
type_="artist",
result_type="name")
page = 0
while not found_artist:
response = self.search_all(search_term, per_page=self.per_page, page=page)
result_count_on_page = max(map(lambda section: len(section["hits"]), response["sections"]))
if result_count_on_page == 0:
break
has_more_pages = result_count_on_page == self.per_page
found_artist = self._get_item_from_search_response(response,
search_term,
type_="artist",
result_type="name")
page += 1
if not has_more_pages:
break

# Exit the search if we couldn't find an artist by the given name
if not found_artist:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def test_zero_songs(self):
result = genius.search_artist(name, max_songs=0).songs
self.assertEqual(0, len(result), msg)

def test_search_artist_not_on_the_first_results_page(self):
msg = "Different artist was returned."
name = "Norther"
result = genius.search_artist(name, max_songs=self.max_songs)
self.assertEqual(name, result.name, msg)

def test_name(self):
msg = "The artist object name does not match the requested artist name."
self.assertEqual(self.artist.name, self.artist_name, msg)
Expand Down