Skip to content

Commit

Permalink
Matcher: Musicbrainz: Support more formats for release dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthi-chaud committed Dec 22, 2024
1 parent 3f200dc commit 28f220c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions matcher/matcher/providers/musicbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,16 @@ def _get_album(self, album_id: str) -> Any | None:
)

def _get_album_release_date(self, album: Any) -> date | None:
try:
return datetime.strptime(album["first-release-date"], "%Y-%m-%d").date()
except Exception:
pass
str_release_date = album.get("first-release-date")
if not str_release_date:
return None
for format in ["%Y-%m-%d", "%Y-%m", "%Y"]:
try:
parsed = datetime.strptime(str_release_date, format)
if parsed:
return parsed.date()
except Exception:
continue

def _get_album_genres(self, album: Any) -> List[str] | None:
try:
Expand Down
9 changes: 9 additions & 0 deletions matcher/tests/providers/musicbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def test_get_album_release_date_and_genres_and_type(self):
type = provider.get_album_type(album)
self.assertIs(AlbumType.STUDIO, type)

def test_get_album_release_date_month_only(self):
provider: MusicBrainzProvider = (
Context().get().get_provider(MusicBrainzProvider)
) # pyright: ignore
album = provider.get_album("88f4aea6-617a-305b-ab3d-9433dc2d5c6f")
self.assertIsNotNone(album)
release_date = provider.get_album_release_date(album)
self.assertEqual(release_date, datetime.date(1994, 11, 1))

def test_get_album_type(self):
scenarios: List[Tuple[str, AlbumType]] = [
# Massive Attack - No Protection
Expand Down

0 comments on commit 28f220c

Please sign in to comment.