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

Bug fix/118 return precise requested name #123

Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 12 additions & 13 deletions lyricsgenius/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,24 @@ def _result_is_lyrics(self, song_title):
regex = re.compile(expression, re.IGNORECASE)
return not regex.search(self._clean_str(song_title))

def _get_item_from_search_response(self, response, type_):
def _get_item_from_search_response(self, response, search_term, type_, result_type):
""" Returns either a Song or Artist result from search_genius_web """
# Convert list to dictionary
hits = response['sections'][0]['hits']
if hits:
tophit = hits[0]
if tophit['type'] == type_:
return tophit['result']

# Check rest of results if top hit wasn't the search type
sections = sorted(response['sections'],
key=lambda sect: sect['type'] == type_,
reverse=True)
for section in sections:
hits = [hit for hit in section['hits'] if hit['type'] == type_]
if hits:
return hits[0]['result']

hits =[hit for section in sections for hit in section['hits'] if hit['type'] == type_]
for hit in hits:
if hit['result'][result_type] == search_term:
return hit['result']

return hits[0]['result'] if hits else None


def _result_is_match(self, result, title, artist=None):
""" Returns True if search result matches searched song """
Expand All @@ -245,7 +246,7 @@ def search_song(self, title, artist="", get_full_info=True):
response = self.search_genius_web(search_term)

# Otherwise, move forward with processing the search results
result = self._get_item_from_search_response(response, type_="song")
result = self._get_item_from_search_response(response, title, type_="song", result_type = "title")

# Exit search if there were no results returned from API
if not result:
Expand Down Expand Up @@ -301,14 +302,13 @@ def find_artist_id(search_term):
# Perform a Genius API search for the artist
found_artist = None
response = self.search_genius_web(search_term)
found_artist = self._get_item_from_search_response(response, type_="artist")
found_artist = self._get_item_from_search_response(response, search_term, type_="artist", result_type="name")

# Exit the search if we couldn't find an artist by the given name
if not found_artist:
if self.verbose:
print("No results found for '{a}'.".format(a=search_term))
return None

# Assume the top search result is the intended artist
return found_artist['id']

Expand All @@ -326,13 +326,12 @@ def find_artist_id(search_term):

# Create the Artist object
artist = Artist(artist_info)

# Download each song by artist, stored as Song objects in Artist object
page = 1
reached_max_songs = False
while not reached_max_songs:
songs_on_page = self.get_artist_songs(artist_id, sort, per_page, page)

# Loop through each song on page of search results
for song_info in songs_on_page['songs']:
# Check if song is valid (e.g. has title, contains lyrics)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_genius.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_search_song(self):

# Exact match exact search
response = genius.search_song(self.song_title_only)
self.assertTrue(response.title.lower() == drake_song.lower()) # Drake gets returned
self.assertTrue(response.title.lower() == self.song_title_only.lower()) # Drake gets returned

# Song with artist name
response = genius.search_song(self.song_title_only, artist)
Expand Down