Skip to content

Commit

Permalink
Properly get_full_info for songs and albums (#181)
Browse files Browse the repository at this point in the history
* 🔧 Properly get full song info

Previously, it was fetched only when the song didn't have an id (?)

* 🔧 Add fields in song for the new fields

* 🔧 Keep the statement as it is, but move it before song_id is overwritten in both search_song and search_album

* Revert "🔧 Add fields in song for the new fields"

This reverts commit bb948b1

* 🔧 Fix get_full_info logic

* 🐛 Set `search_term` for both types of album queries, preventing a NameError

* 🐛 Set `search_term` for both types of song queries, preventing a NameError

* ✏ Rename result to song_info to avoid defining song_info later

* 🤦 Oops. Now album_info was always None.

Please squash this commit! :)

* 🧹 Reindent due to variable rename

* 🔧 Fix get_full_info logic for song searches

* 🐛 Remove quotes from the search_term in album

* 🐛 Remove quotes from the search_term in song

* 🔧 Change .get("album") to ["album"]
  • Loading branch information
Steffo99 authored Oct 27, 2024
1 parent b33deaa commit f896087
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions lyricsgenius/genius.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def search_album(self, name=None, artist="",
print('Searching for "{s}"...'.format(s=name))

if album_id:
album_info = self.album(album_id, text_format).get('album')
album_info = self.album(album_id, text_format)['album']
else:
search_term = "{s} {a}".format(s=name, a=artist).strip()
response = self.search_all(search_term)
Expand All @@ -322,6 +322,11 @@ def search_album(self, name=None, artist="",
print("No results found for: '{s}'".format(s=search_term))
return None

# If the album was searched, query the API using the album id so the full info can be retrieved
if album_id is None and get_full_info:
album_info.update(self.album(album_info['id'], text_format)['album'])

# Set the album id to the value retrieved from the API
album_id = album_info['id']

tracks = []
Expand Down Expand Up @@ -349,10 +354,6 @@ def search_album(self, name=None, artist="",

next_page = tracks_list['next_page']

if album_id is None and get_full_info is True:
new_info = self.album(album_id, text_format=text_format)['album']
album_info.update(new_info)

return Album(self, album_info, tracks)

def search_song(self, title=None, artist="", song_id=None,
Expand Down Expand Up @@ -395,25 +396,25 @@ def search_song(self, title=None, artist="", song_id=None,
print('Searching for "{s}"...'.format(s=title))

if song_id:
result = self.song(song_id)['song']
song_info = self.song(song_id)['song']
else:
search_term = "{s} {a}".format(s=title, a=artist).strip()
search_response = self.search_all(search_term)
result = self._get_item_from_search_response(search_response,
title,
type_="song",
result_type="title")
song_info = self._get_item_from_search_response(search_response,
title,
type_="song",
result_type="title")

# Exit search if there were no results returned from API
# Otherwise, move forward with processing the search results
if result is None:
if song_info is None:
if self.verbose and title:
print("No results found for: '{s}'".format(s=search_term))
return None

# Reject non-songs (Liner notes, track lists, etc.)
# or songs with uncomplete lyrics (e.g. unreleased songs, instrumentals)
if self.skip_non_songs and not self._result_is_lyrics(result):
if self.skip_non_songs and not self._result_is_lyrics(song_info):
valid = False
else:
valid = True
Expand All @@ -423,13 +424,9 @@ def search_song(self, title=None, artist="", song_id=None,
print('Specified song does not contain lyrics. Rejecting.')
return None

song_id = result['id']

# Download full song info (an API call) unless told not to by user
song_info = result
if song_id is None and get_full_info is True:
new_info = self.song(song_id)['song']
song_info.update(new_info)
if song_id is None and get_full_info:
song_info.update(self.song(song_info['id'])['song'])

if (song_info['lyrics_state'] == 'complete'
and not song_info.get('instrumental')):
Expand Down

0 comments on commit f896087

Please sign in to comment.