From f24c026ae42aa941baa7650ea6b3c9f090835e9e Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Tue, 21 Apr 2020 18:56:46 +0530 Subject: [PATCH] Improve Genius lyrics stability Sometimes the first result in the Genius API response may not contain the path to lyrics. In such cases, keep iterating on the next result until the path is found. --- spotdl/lyrics/providers/genius.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spotdl/lyrics/providers/genius.py b/spotdl/lyrics/providers/genius.py index 9048453..10c579e 100644 --- a/spotdl/lyrics/providers/genius.py +++ b/spotdl/lyrics/providers/genius.py @@ -79,7 +79,22 @@ class Genius(LyricBase): encoded_query = urllib.request.quote(query.replace(" ", "+")) search_url = self.base_search_url + encoded_query metadata = self._fetch_search_page(search_url) - lyric_url = metadata["response"]["sections"][0]["hits"][0]["result"]["path"] + + lyric_url = None + for section in metadata["response"]["sections"]: + result = section["hits"][0]["result"] + try: + lyric_url = result["path"] + break + except KeyError: + pass + + if lyric_url is None: + raise LyricsNotFoundError( + "Could not find any valid lyric paths in the " + "API response for the query {}".format(query) + ) + return self.base_url + lyric_url def from_query(self, query, linesep="\n", timeout=None):