Skip to content

Commit

Permalink
beets: add support for albumtypes list, but constrain beets dependenc…
Browse files Browse the repository at this point in the history
…y <= 1.6.0
  • Loading branch information
snejus committed Apr 28, 2024
1 parent 17dcd81 commit 38b12e0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
24 changes: 15 additions & 9 deletions beetsplug/bandcamp/metaguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
from beets import __version__ as beets_version
from beets import config as beets_config
from beets.autotag.hooks import AlbumInfo, TrackInfo
from packaging import version
from pycountry import countries, subdivisions

from .album import AlbumName
from .helpers import PATTERNS, Helpers, MediaInfo
from .track import Track
from .tracks import Tracks

NEW_BEETS = int(beets_version.split(".")[1]) > 4
BEETS_VERSION = version.parse(beets_version)
EXTENDED_FIELDS_SUPPORT = version.Version("1.5.0") <= BEETS_VERSION
ALBUMTYPES_LIST_SUPPORT = version.Version("1.6.0") < BEETS_VERSION

JSONDict = Dict[str, Any]

Expand Down Expand Up @@ -92,9 +95,10 @@ def comments(self) -> Optional[str]:

@cached_property
def all_media_comments(self) -> str:
return "\n".join(
[*[m.description for m in self.media_formats], self.comments or ""]
)
return "\n".join([
*[m.description for m in self.media_formats],
self.comments or "",
])

@cached_property
def label(self) -> str:
Expand Down Expand Up @@ -347,7 +351,7 @@ def albumtype(self) -> str:
return "album"

@cached_property
def albumtypes(self) -> str:
def albumtypes(self) -> List[str]:
albumtypes = {self.albumtype}
if self.is_comp:
if self.albumtype == "ep":
Expand All @@ -364,7 +368,7 @@ def albumtypes(self) -> str:
if len(self.tracks.remixers) == len(self.tracks):
albumtypes.add("remix")

return "; ".join(sorted(albumtypes))
return sorted(albumtypes)

@cached_property
def va(self) -> bool:
Expand Down Expand Up @@ -419,9 +423,11 @@ def get_fields(self, fields: Iterable[str], src: object = None) -> JSONDict:
def _common_album(self) -> JSONDict:
common_data: JSONDict = {"album": self.album_name}
fields = ["label", "catalognum", "albumtype", "country"]
if NEW_BEETS:
if EXTENDED_FIELDS_SUPPORT:
fields.extend(["genre", "style", "comments", "albumtypes"])
common_data.update(self.get_fields(fields))
if EXTENDED_FIELDS_SUPPORT and not ALBUMTYPES_LIST_SUPPORT:
common_data["albumtypes"] = "; ".join(common_data["albumtypes"])
reldate = self.release_date
if reldate:
common_data.update(self.get_fields(["year", "month", "day"], reldate))
Expand All @@ -437,7 +443,7 @@ def _trackinfo(self, track: Track, **kwargs: Any) -> TrackInfo:
data.pop("catalognum", None)
if not data["lyrics"]:
data.pop("lyrics", None)
if not NEW_BEETS:
if not EXTENDED_FIELDS_SUPPORT:
data.pop("catalognum", None)
data.pop("lyrics", None)
for field in set(data.keys()) & self.excluded_fields:
Expand All @@ -450,7 +456,7 @@ def singleton(self) -> TrackInfo:
self._singleton = True
self.media = self.media_formats[0]
track = self._trackinfo(self.tracks.first)
if NEW_BEETS:
if EXTENDED_FIELDS_SUPPORT:
track.update(self._common_album)
track.pop("album", None)
track.track_id = track.data_url
Expand Down
3 changes: 2 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ python = ">=3.8, <4"

requests = ">=2.27"
pycountry = ">=20.7.3"
beets = ">=1.4"
beets = ">=1.4,<=1.6.0"
ordered-set = ">=4.0"
packaging = ">=24.0"

[tool.poetry.dev-dependencies]
flake8 = ">=3.8.4"
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest
from beets.autotag.hooks import AlbumInfo, TrackInfo
from beetsplug.bandcamp import DEFAULT_CONFIG
from beetsplug.bandcamp.metaguru import ALBUMTYPES_LIST_SUPPORT
from rich.console import Console


Expand Down Expand Up @@ -131,6 +132,9 @@ def release(request):
expected_output = json.load(out_f)
if isinstance(expected_output, dict):
expected_output = [expected_output]
if ALBUMTYPES_LIST_SUPPORT:
for release in expected_output:
release["albumtypes"] = release["albumtypes"].split("; ")

return input_json, expected_output

Expand Down
5 changes: 3 additions & 2 deletions tests/test_jsons.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Tests that compare beetcamp outputs against expected JSON outputs."""

from operator import itemgetter

import pytest
from beetsplug.bandcamp.metaguru import NEW_BEETS, Metaguru
from beetsplug.bandcamp.metaguru import EXTENDED_FIELDS_SUPPORT, Metaguru

pytestmark = pytest.mark.jsons


def check(actual, expected) -> None:
if NEW_BEETS:
if EXTENDED_FIELDS_SUPPORT:
assert dict(actual) == expected
else:
actual = vars(actual)
Expand Down

0 comments on commit 38b12e0

Please sign in to comment.