Skip to content

Commit

Permalink
Add second-pass search that truncates all text in song title after a …
Browse files Browse the repository at this point in the history
…hyphen
  • Loading branch information
robert-alfaro committed Sep 16, 2024
1 parent 296483b commit 842e3dc
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions custom_components/genius_lyrics/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,30 @@ def _fetch_lyrics(self) -> bool:
self._media_title = cleaned_title

_LOGGER.info(
"Searching lyrics for artist='%s' and title='%s'",
self._media_artist,
self._media_title,
f"Searching lyrics for artist='{self._media_artist}' and title='{self._media_title}'"
)

self._attr_extra_state_attributes[ATTR_MEDIA_ARTIST] = self._media_artist
self._attr_extra_state_attributes[ATTR_MEDIA_TITLE] = self._media_title

# perform search
song = self._genius.search_song(
self._media_title, self._media_artist, get_full_info=False
)

# second search needed?
if not song and " - " in self._media_title:
# aggressively truncate title from the first hyphen
self._media_title = self._media_title.split(" - ", 1)[0]
_LOGGER.info(
f"Second attempt, aggressively cleaned title='{self._media_title}'"
)

# perform search
song = self._genius.search_song(
self._media_title, self._media_artist, get_full_info=False
)

self._attr_extra_state_attributes[ATTR_MEDIA_ARTIST] = self._media_artist
self._attr_extra_state_attributes[ATTR_MEDIA_TITLE] = self._media_title

if song:
_LOGGER.debug(
"Found song: artist = %s, title = %s", song.artist, song.title
Expand Down

0 comments on commit 842e3dc

Please sign in to comment.