From 4ae454be85b6333d7ddcbc851ed22179c1978805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Sun, 17 Jan 2021 21:21:53 +0000 Subject: [PATCH] Hardened release_date search pattern --- CHANGELOG.md | 8 ++++++++ beetsplug/bandcamp/_metaguru.py | 9 +++++++-- pyproject.toml | 2 +- tests/test_parsing.py | 11 +++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19c8a90..afd9612 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# [0.4.4] 2021-01-17 + +### Fixed +- `release_date` search pattern now looks for a specific date format, guarding + it against similar matches that could be found in the description, thanks + @noahsager. + + # [0.4.3] 2021-01-17 ### Fixed diff --git a/beetsplug/bandcamp/_metaguru.py b/beetsplug/bandcamp/_metaguru.py index 14e9a25..762eac1 100644 --- a/beetsplug/bandcamp/_metaguru.py +++ b/beetsplug/bandcamp/_metaguru.py @@ -45,7 +45,7 @@ "country": re.compile(r'location\ssecondaryText">(?:[\w\s]*, )?([\w\s,]+){1,4}'), "label": re.compile(r'og:site_name".*content="([^"]*)"'), "lyrics": re.compile(r'"lyrics":({[^}]*})'), - "release_date": re.compile(r" released (.*)"), + "release_date": re.compile(r"released ([\d]{2} [A-Z][a-z]+ [\d]{4})"), "track_name": re.compile( r""" ((?P[ABCDEFGH]{1,3}\d\d?)[^\w]*)? @@ -99,6 +99,11 @@ def parse_catalognum(album: str, disctitle: str) -> str: return "" + @staticmethod + def parse_release_date(string: str) -> str: + match = re.search(PATTERNS["release_date"], string) + return match.groups()[0] if match else "" + class Metaguru(Helpers): html: str @@ -152,7 +157,7 @@ def lyrics(self) -> Optional[str]: @property def release_date(self) -> date: - datestr = self._search(PATTERNS["release_date"]) + datestr = self.parse_release_date(self.html) return datetime.strptime(datestr, DATE_FORMAT).date() @property diff --git a/pyproject.toml b/pyproject.toml index 7b40ee4..aef7340 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "beetcamp" -version = "0.4.3" +version = "0.4.4" description = "Bandcamp autotagger source for beets (http://beets.io)." authors = ["Šarūnas Nejus "] readme = "README.md" diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 4bee8de..e8f5204 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -31,6 +31,17 @@ def test_convert_title(title, expected): assert urlify(title) == expected +@pytest.mark.parametrize( + ("string", "expected"), + [ + ("released 06 November 2019", "06 November 2019"), + ("released on Some Records", ""), + ], +) +def test_parse_release_date(string, expected): + assert Metaguru.parse_release_date(string) == expected + + @pytest.mark.parametrize( ("name", "expected"), [