From 9336ff0f476284a16e7ae1b5dd012ad39162fd52 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Sun, 21 Jan 2024 23:30:46 +0100 Subject: [PATCH] Replace UUID check on MusicBrainz ID (#1015) --- music_assistant/common/helpers/util.py | 10 ++++++++++ music_assistant/common/models/media_items.py | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/music_assistant/common/helpers/util.py b/music_assistant/common/helpers/util.py index 1a73f44ca..d5ff70e64 100755 --- a/music_assistant/common/helpers/util.py +++ b/music_assistant/common/helpers/util.py @@ -6,6 +6,7 @@ import socket from collections.abc import Callable from typing import Any, TypeVar +from uuid import UUID # pylint: disable=invalid-name T = TypeVar("T") @@ -277,3 +278,12 @@ def empty_queue(q: asyncio.Queue) -> None: q.task_done() except (asyncio.QueueEmpty, ValueError): pass + + +def is_valid_uuid(uuid_to_test: str, version: int = 4) -> bool: + """Check if uuid string is a valid UUID.""" + try: + uuid_obj = UUID(uuid_to_test, version=version) + except ValueError: + return False + return str(uuid_obj) == uuid_to_test diff --git a/music_assistant/common/models/media_items.py b/music_assistant/common/models/media_items.py index 85c2ec6fb..aace45cb1 100755 --- a/music_assistant/common/models/media_items.py +++ b/music_assistant/common/models/media_items.py @@ -8,7 +8,7 @@ from mashumaro import DataClassDictMixin from music_assistant.common.helpers.uri import create_uri -from music_assistant.common.helpers.util import create_sort_name, merge_lists +from music_assistant.common.helpers.util import create_sort_name, is_valid_uuid, merge_lists from music_assistant.common.models.enums import ( AlbumType, ContentType, @@ -17,6 +17,7 @@ LinkType, MediaType, ) +from music_assistant.common.models.errors import InvalidDataError MetadataTypes = int | bool | str | list[str] @@ -222,8 +223,8 @@ def mbid(self, value: str) -> None: """Set MusicBrainz External ID.""" if not value: return - if len(value.split("-")) != 5: - raise RuntimeError("Invalid MusicBrainz identifier") + if not is_valid_uuid(value): + raise InvalidDataError(f"Invalid MusicBrainz identifier: {value}") if existing := next((x for x in self.external_ids if x[0] == ExternalID.MUSICBRAINZ), None): # Musicbrainz ID is unique so remove existing entry self.external_ids.remove(existing)