Skip to content

Commit

Permalink
Replace UUID check on MusicBrainz ID (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt authored Jan 21, 2024
1 parent 5726096 commit 9336ff0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions music_assistant/common/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions music_assistant/common/models/media_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +17,7 @@
LinkType,
MediaType,
)
from music_assistant.common.models.errors import InvalidDataError

MetadataTypes = int | bool | str | list[str]

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9336ff0

Please sign in to comment.