Skip to content

Commit

Permalink
Merge pull request #123 from danielcliu/bug-fix/118-return-precise-re…
Browse files Browse the repository at this point in the history
…quested-name

Bug fix/118 return precise requested name
  • Loading branch information
johnwmillr committed Dec 11, 2019
2 parents 85de300 + 8fef8f2 commit a65c0fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lyricsgenius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
__url__ = 'https://github.com/johnwmillr/LyricsGenius'
__description__ = 'A Python wrapper around the Genius API'
__license__ = 'MIT'
__version__ = '1.8.1'
__version__ = '1.8.2'
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 @@ -35,7 +35,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

0 comments on commit a65c0fb

Please sign in to comment.