diff --git a/vrobbler/apps/music/musicbrainz.py b/vrobbler/apps/music/musicbrainz.py index 845524b..533f84c 100644 --- a/vrobbler/apps/music/musicbrainz.py +++ b/vrobbler/apps/music/musicbrainz.py @@ -1,4 +1,5 @@ import logging +from typing import Iterable import musicbrainzngs from dateutil.parser import parse @@ -74,19 +75,22 @@ def lookup_album_dict_from_mb(release_name: str, artist_name: str) -> dict: } -def lookup_artist_from_mb(artist_name: str) -> str: +def lookup_artist_from_mb(artist_name: str) -> dict: musicbrainzngs.set_useragent("vrobbler", "0.3.0") - top_result = musicbrainzngs.search_artists(artist=artist_name)[ - "artist-list" - ][0] + try: + top_result = musicbrainzngs.search_artists(artist=artist_name)[ + "artist-list" + ][0] + except IndexError: + return {} score = int(top_result.get("ext:score")) if score < 85: logger.debug( "Artist lookup score below 85 threshold", extra={"result": top_result}, ) - return "" + return {} return top_result diff --git a/vrobbler/apps/music/utils.py b/vrobbler/apps/music/utils.py index fdea074..118a0e7 100644 --- a/vrobbler/apps/music/utils.py +++ b/vrobbler/apps/music/utils.py @@ -26,9 +26,10 @@ def get_or_create_artist(name: str, mbid: str = None) -> Artist: name = re.split("&", name, flags=re.IGNORECASE)[0].strip() artist_dict = lookup_artist_from_mb(name) - mbid = mbid or artist_dict["id"] + mbid = mbid or artist_dict.get("id", None) - artist = Artist.objects.filter(musicbrainz_id=mbid).first() + if mbid: + artist = Artist.objects.filter(musicbrainz_id=mbid).first() if not artist: artist = Artist.objects.create(name=name, musicbrainz_id=mbid) artist.fix_metadata()