From f6b9245b8b88414c143c9ea2aadf5855e668a0d5 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 23 Feb 2023 10:07:29 -0500 Subject: [PATCH] Add looking tracks without MB IDs by looking them up --- vrobbler/apps/music/utils.py | 7 ++++++- vrobbler/apps/scrobbles/musicbrainz.py | 12 +++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/vrobbler/apps/music/utils.py b/vrobbler/apps/music/utils.py index e1a06cd..4e97e77 100644 --- a/vrobbler/apps/music/utils.py +++ b/vrobbler/apps/music/utils.py @@ -4,6 +4,7 @@ import re from scrobbles.musicbrainz import ( lookup_album_dict_from_mb, lookup_artist_from_mb, + lookup_track_from_mb, ) logger = logging.getLogger(__name__) @@ -93,7 +94,11 @@ def get_or_create_track( title=title, artist=artist, album=album ).first() - # TODO Can we look up mbid for tracks? + if not mbid: + mbid = lookup_track_from_mb( + title, artist.musicbrainz_id, album.musicbrainz_id + )['id'] + if not track: track = Track.objects.create( title=title, diff --git a/vrobbler/apps/scrobbles/musicbrainz.py b/vrobbler/apps/scrobbles/musicbrainz.py index f1ebc81..9cb9a86 100644 --- a/vrobbler/apps/scrobbles/musicbrainz.py +++ b/vrobbler/apps/scrobbles/musicbrainz.py @@ -90,16 +90,18 @@ def lookup_artist_from_mb(artist_name: str) -> str: return top_result -def lookup_track_from_mb(artist_name: str) -> str: +def lookup_track_from_mb( + track_name: str, artist_mbid: str, album_mbid: str +) -> str: musicbrainzngs.set_useragent('vrobbler', '0.3.0') - top_result = musicbrainzngs.search_recordings(artist=artist_name)[ - 'artist-list' - ][0] + top_result = musicbrainzngs.search_recordings( + query=track_name, artist=artist_mbid, release=album_mbid + )['recording-list'][0] score = int(top_result.get('ext:score')) if score < 85: logger.debug( - "Artist lookup score below 85 threshold", + "Track lookup score below 85 threshold", extra={"result": top_result}, ) return ""