Skip to content

Commit

Permalink
Add support for beets==2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Sep 21, 2024
1 parent 9455f8c commit 9d188b8
Show file tree
Hide file tree
Showing 29 changed files with 2,433 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]
beets: ["1.5.0", "1.6.0"]
beets: ["1.5.0", "1.6.0", "2.0.0"]
steps:
- uses: actions/checkout@v4

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Drop support for `beets<1.5`.

### Added

- Add artist list fields support for `beets==2.0.0`.

### Fixed

- `album`:
Expand Down
Empty file added beetsplug/bandcamp/art.py
Empty file.
24 changes: 24 additions & 0 deletions beetsplug/bandcamp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@
NamedTuple,
Pattern,
Tuple,
TypeVar,
Union,
)

from beets import __version__ as beets_version
from beets.autotag.hooks import AlbumInfo, TrackInfo
from ordered_set import OrderedSet as ordset # noqa: N813
from packaging.version import Version

from .genres_lookup import GENRES

BEETS_VERSION = Version(beets_version)
ALBUMTYPES_LIST_SUPPORT = BEETS_VERSION >= Version("1.6.0")
ARTIST_LIST_FIELDS_SUPPORT = BEETS_VERSION >= Version("2.0.0")

JSONDict = Dict[str, Any]
DIGI_MEDIA = "Digital Media"
USB_TYPE_ID = 5
Expand All @@ -36,6 +43,8 @@
"USB Flash Drive": DIGI_MEDIA,
}

T = TypeVar("T", bound=JSONDict)


class MediaInfo(NamedTuple):
album_id: str
Expand Down Expand Up @@ -341,6 +350,21 @@ def get_medium_total(medium: int) -> int:
medium_index += 1
return album

@staticmethod
def check_list_fields(data: T) -> T:
if "albumtypes" in data and not ALBUMTYPES_LIST_SUPPORT:
data["albumtypes"] = "; ".join(data["albumtypes"])

if not ARTIST_LIST_FIELDS_SUPPORT:
fields = ["artists", "artists_ids", "artists_credit", "artists_sort"]
for f in fields:
data.pop(f)

if "tracks" in data:
data["tracks"] = [Helpers.check_list_fields(t) for t in data["tracks"]]

return data


@singledispatch
def to_dict(info: Any) -> Any:
Expand Down
24 changes: 11 additions & 13 deletions beetsplug/bandcamp/metaguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
from typing import Any, Dict, Iterable, List, Optional, Set
from unicodedata import normalize

from beets import __version__ as beets_version
from beets import config as beets_config
from beets.autotag.hooks import AlbumInfo, TrackInfo
from packaging.version import Version
from pycountry import countries, subdivisions

from .album_name import AlbumName
Expand All @@ -22,9 +20,6 @@
from .track import Track
from .tracks import Tracks

BEETS_VERSION = Version(beets_version)
ALBUMTYPES_LIST_SUPPORT = BEETS_VERSION >= Version("1.6.0")

JSONDict = Dict[str, Any]

COUNTRY_OVERRIDES = {
Expand Down Expand Up @@ -408,10 +403,14 @@ def genre(self) -> Optional[str]:
@property
def _common(self) -> JSONDict:
return {
"album": self.album_name,
"artist_id": self.artist_id,
"artists_credit": [], # TODO: implement
"artists_ids": [self.artist_id],
"artists_sort": [], # TODO: implement
"data_source": DATA_SOURCE,
"media": self.media.name,
"data_url": self.album_id,
"artist_id": self.artist_id,
"media": self.media.name,
}

def get_fields(self, fields: Iterable[str], src: object = None) -> JSONDict:
Expand All @@ -424,7 +423,7 @@ def get_fields(self, fields: Iterable[str], src: object = None) -> JSONDict:

@property
def _common_album(self) -> JSONDict:
common_data: JSONDict = {"album": self.album_name}
common_data: JSONDict = dict.fromkeys(["barcode", "release_group_title"])
fields = [
"albumtype",
"albumtypes",
Expand All @@ -436,11 +435,10 @@ def _common_album(self) -> JSONDict:
"style",
]
common_data.update(self.get_fields(fields))
if 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))
common_data["artists"] = self.albumartist.split(", ")

return common_data

Expand All @@ -464,9 +462,9 @@ def singleton(self) -> TrackInfo:
self.media = self.media_formats[0]
track = self._trackinfo(self.tracks.first)
track.update(self._common_album)
track.pop("album", None)
track.album = None
track.track_id = track.data_url
return track
return self.check_list_fields(track)

def get_media_album(self, media: MediaInfo) -> AlbumInfo:
"""Return album for the appropriate release format."""
Expand Down Expand Up @@ -497,7 +495,7 @@ def get_media_album(self, media: MediaInfo) -> AlbumInfo:
album_info.album_id = self.media.album_id
if self.media.name == "Vinyl":
album_info = self.add_track_alts(album_info, self.comments or "")
return album_info
return self.check_list_fields(album_info)

@cached_property
def albums(self) -> List[AlbumInfo]:
Expand Down
7 changes: 7 additions & 0 deletions beetsplug/bandcamp/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ def artists(self) -> List[str]:

@property
def info(self) -> JSONDict:
artists = self.artists
if self.ft_artist:
artists.append(self.ft_artist)
# if self.remix:
# artists.append(self.remix.remixer)

return {
"index": self.index,
"medium_index": self.index,
Expand All @@ -234,6 +240,7 @@ def info(self) -> JSONDict:
if self.ft_artist not in self.artist + self.title
else self.artist
),
"artists": artists,
"title": self.title,
"length": self.duration,
"track_alt": self.track_alt,
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Changelog = "https://github.com/snejus/beetcamp/blob/master/CHANGELOG.md"
python = ">=3.8, <4"

pycountry = ">=20.7.3"
beets = ">=1.5,<=2"
beets = ">=1.5"
httpx = ">=0.27.0"
ordered-set = ">=4.0"
packaging = ">=24.0"
Expand Down
12 changes: 3 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from rich_tables.utils import make_console, pretty_diff

from beetsplug.bandcamp import DEFAULT_CONFIG
from beetsplug.bandcamp.metaguru import ALBUMTYPES_LIST_SUPPORT
from beetsplug.bandcamp.helpers import Helpers

if TYPE_CHECKING:
from _pytest.config import Config
Expand Down Expand Up @@ -194,15 +194,9 @@ def expected_release(bandcamp_data_path: Path) -> list[AlbumInfo] | TrackInfo |
release_data = json.loads(release_datastr)

if isinstance(release_data, dict):
if ALBUMTYPES_LIST_SUPPORT:
release_data["albumtypes"] = release_data["albumtypes"].split("; ")
return TrackInfo(**release_data)
return Helpers.check_list_fields(TrackInfo(**release_data))

if ALBUMTYPES_LIST_SUPPORT:
for release in release_data:
release["albumtypes"] = release["albumtypes"].split("; ")

return [AlbumInfo(**r) for r in release_data]
return [Helpers.check_list_fields(AlbumInfo(**r)) for r in release_data]


@pytest.fixture
Expand Down
Loading

0 comments on commit 9d188b8

Please sign in to comment.