From 28f220c4cb52da60cbcce945ded3d1835d0cc3d3 Mon Sep 17 00:00:00 2001 From: Arthur Jamet Date: Sun, 22 Dec 2024 10:28:03 +0000 Subject: [PATCH] Matcher: Musicbrainz: Support more formats for release dates --- matcher/matcher/providers/musicbrainz.py | 14 ++++++++++---- matcher/tests/providers/musicbrainz.py | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/matcher/matcher/providers/musicbrainz.py b/matcher/matcher/providers/musicbrainz.py index eb3ea79a..292db77f 100644 --- a/matcher/matcher/providers/musicbrainz.py +++ b/matcher/matcher/providers/musicbrainz.py @@ -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: diff --git a/matcher/tests/providers/musicbrainz.py b/matcher/tests/providers/musicbrainz.py index d9219ca2..7f7c1f5a 100644 --- a/matcher/tests/providers/musicbrainz.py +++ b/matcher/tests/providers/musicbrainz.py @@ -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