Skip to content

Commit

Permalink
[bugfix]: Do not require Subsonic disc/track number, fix lb-radio art…
Browse files Browse the repository at this point in the history
…ist 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`)
  • Loading branch information
kgarner7 committed Aug 24, 2024
1 parent 8da768d commit a20be0a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 7 additions & 9 deletions troi/content_resolver/artist_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand All @@ -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, []
5 changes: 3 additions & 2 deletions troi/content_resolver/subsonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
3 changes: 2 additions & 1 deletion troi/patches/lb_radio_classes/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a20be0a

Please sign in to comment.