From 49dc7093c893749544246ae76d05406b64e3f991 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 22 May 2024 15:18:43 +0200 Subject: [PATCH] fix: genius fallback --- music_kraken/pages/genius.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/music_kraken/pages/genius.py b/music_kraken/pages/genius.py index 0664ac4..1719025 100644 --- a/music_kraken/pages/genius.py +++ b/music_kraken/pages/genius.py @@ -79,6 +79,8 @@ class Genius(Page): artwork.append(url=url) def parse_api_object(self, data: dict) -> Optional[DatabaseObject]: + if data is None: + return None object_type = data.get("_type") artwork = Artwork() @@ -93,7 +95,7 @@ class Genius(Page): }) notes = FormattedText() - description = data.get("description", {}) + description = data.get("description") or {} if "html" in description: notes.html = description["html"] elif "markdown" in description: @@ -113,7 +115,7 @@ class Genius(Page): additional_sources.append(Source(ALL_SOURCE_TYPES.TWITTER, f"https://x.com/{data['twitter_name']}/")) return Artist( - name=data.get("name").strip(), + name=data["name"].strip() if data.get("name") is not None else None, source_list=[source], artwork=artwork, notes=notes, @@ -147,7 +149,7 @@ class Genius(Page): if primary_artist is not None: _artist_name = primary_artist.name main_artist_list.append(primary_artist) - for feature_artist in (*data.get("featured_artists", []), *data.get("producer_artists", []), *data.get("writer_artists", [])): + for feature_artist in (*(data.get("featured_artists") or []), *(data.get("producer_artists") or []), *(data.get("writer_artists") or [])): artist = self.parse_api_object(feature_artist) if artist is not None: featured_artist_list.append(artist) @@ -201,16 +203,16 @@ class Genius(Page): dump_to_file("genius_itemprop_artist.json", content, is_json=True, exit_after_dump=False) data = json.loads(content) - artist = self.parse_api_object(data.get("artist", {})) + artist = self.parse_api_object(data.get("artist")) - for e in data.get("artist_albums", []): + for e in (data.get("artist_albums") or []): r = self.parse_api_object(e) if not isinstance(r, Album): continue artist.album_collection.append(r) - for e in data.get("artist_songs", []): + for e in (data.get("artist_songs") or []): r = self.parse_api_object(e) if not isinstance(r, Song): continue @@ -243,7 +245,7 @@ class Genius(Page): dump_to_file("genius_itemprop_album.json", content, is_json=True, exit_after_dump=False) data = json.loads(content) - album = self.parse_api_object(data.get("album", {})) + album = self.parse_api_object(data.get("album")) for e in data.get("album_appearances", []): r = self.parse_api_object(e.get("song"))