From a20be0a92c871303df99a71a1ea7bbdc529e6308 Mon Sep 17 00:00:00 2001 From: Kendall Garner <17521368+kgarner7@users.noreply.github.com> Date: Sat, 24 Aug 2024 07:19:02 -0700 Subject: [PATCH] [bugfix]: Do not require Subsonic disc/track number, fix lb-radio artist locally This change has a few fixes: - For subsonic import, allow track and disc number to be None (and default to 1). Subsonic servers are **not** guaranteed to return values here. Tested on my Navidrome instance - Multiple fixes for get_similar_artists: proper post parameter, parse response, query using list of ids (not dicts), and return an empty array for msgs - For `artist.py`, in some cases the `artist_credit` is `None`, also allow for this (via `AttributeError`) --- troi/content_resolver/artist_search.py | 16 +++++++--------- troi/content_resolver/subsonic.py | 5 +++-- troi/patches/lb_radio_classes/artist.py | 3 ++- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/troi/content_resolver/artist_search.py b/troi/content_resolver/artist_search.py index 8109158..02d8aa7 100755 --- a/troi/content_resolver/artist_search.py +++ b/troi/content_resolver/artist_search.py @@ -39,18 +39,15 @@ def get_similar_artists(self, artist_mbid): r = requests.post("https://labs.api.listenbrainz.org/similar-artists/json", json=[{ - 'artist_mbid': - artist_mbid, + 'artist_mbids': + [artist_mbid], 'algorithm': "session_based_days_7500_session_300_contribution_5_threshold_10_limit_100_filter_True_skip_30" }]) if r.status_code != 200: raise RuntimeError(f"Cannot fetch similar artists: {r.status_code} ({r.text})") - try: - artists = r.json()[3]["data"] - except IndexError: - return [] + artists = r.json() # Knock down super hyped artists for artist in artists: @@ -90,8 +87,9 @@ def search(self, mode, artist_mbid, pop_begin, pop_end, max_recordings_per_artis ORDER BY artist_mbid , popularity""" + artist_mbids = [artist["artist_mbid"] for artist in similar_artists] placeholders = ",".join(("?", ) * len(similar_artists)) - cursor = db.execute_sql(query % placeholders, params=tuple(similar_artists)) + cursor = db.execute_sql(query % placeholders, params=artist_mbids) artists = defaultdict(list) for rec in cursor.fetchall(): @@ -104,6 +102,6 @@ def search(self, mode, artist_mbid, pop_begin, pop_end, max_recordings_per_artis }) for artist in artists: - artists[artist] = select_recordings_on_popularity(artists[artist], pop_begin, pop_end, num_recordings) + artists[artist] = select_recordings_on_popularity(artists[artist], pop_begin, pop_end, max_recordings_per_artist) - return artists + return artists, [] diff --git a/troi/content_resolver/subsonic.py b/troi/content_resolver/subsonic.py index aac6903..4f712f9 100755 --- a/troi/content_resolver/subsonic.py +++ b/troi/content_resolver/subsonic.py @@ -136,8 +136,9 @@ def run_sync(self): "release_mbid": album_mbid, "recording_mbid": song["musicBrainzId"], "duration": song["duration"] * 1000, - "track_num": song["track"], - "disc_num": song["discNumber"], + # Neither track number nor disc number are guaranteed for subsonic + "track_num": song.get("track", 1), + "disc_num": song.get("discNumber", 1), "subsonic_id": song["id"], "mtime": datetime.datetime.now() }) diff --git a/troi/patches/lb_radio_classes/artist.py b/troi/patches/lb_radio_classes/artist.py index e283beb..a4dcb0d 100755 --- a/troi/patches/lb_radio_classes/artist.py +++ b/troi/patches/lb_radio_classes/artist.py @@ -52,7 +52,8 @@ def read(self, entities): try: similar_artist_names.append(artist_recordings[mbid][0].artist_credit.name) - except IndexError: + # The item may not exist, or the artist credit may be None + except (AttributeError, IndexError): pass # craft user feedback messages