Jelajahi Sumber

Add looking tracks without MB IDs by looking them up

Colin Powell 2 tahun lalu
induk
melakukan
f6b9245b8b
2 mengubah file dengan 13 tambahan dan 6 penghapusan
  1. 6 1
      vrobbler/apps/music/utils.py
  2. 7 5
      vrobbler/apps/scrobbles/musicbrainz.py

+ 6 - 1
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,

+ 7 - 5
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 ""