Skip to content

Commit c087851

Browse files
committed
Prefer alias if import languages not defined
1 parent 040b2dd commit c087851

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

beetsplug/mbpseudo.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import annotations
1818

19+
import itertools
1920
import traceback
2021
from copy import deepcopy
2122
from typing import TYPE_CHECKING, Any, Iterable, Sequence
@@ -24,6 +25,7 @@
2425
import musicbrainzngs
2526
from typing_extensions import override
2627

28+
from beets import config
2729
from beets.autotag.distance import Distance, distance
2830
from beets.autotag.hooks import AlbumInfo
2931
from beets.autotag.match import assign_items
@@ -34,6 +36,7 @@
3436
MusicBrainzAPIError,
3537
MusicBrainzPlugin,
3638
_merge_pseudo_and_actual_album,
39+
_preferred_alias,
3740
)
3841

3942
if TYPE_CHECKING:
@@ -143,12 +146,13 @@ def album_info(self, release: JSONDict) -> AlbumInfo:
143146
try:
144147
raw_pseudo_release = self._release_getter(
145148
album_id, RELEASE_INCLUDES
146-
)
147-
pseudo_release = super().album_info(
148-
raw_pseudo_release["release"]
149-
)
149+
)["release"]
150+
pseudo_release = super().album_info(raw_pseudo_release)
150151

151152
if self.config["custom_tags_only"].get(bool):
153+
self._replace_artist_with_alias(
154+
raw_pseudo_release, pseudo_release
155+
)
152156
self._add_custom_tags(official_release, pseudo_release)
153157
return official_release
154158
else:
@@ -212,6 +216,41 @@ def _wanted_pseudo_release_id(
212216
else:
213217
return None
214218

219+
def _replace_artist_with_alias(
220+
self,
221+
raw_pseudo_release: JSONDict,
222+
pseudo_release: AlbumInfo,
223+
):
224+
"""Use the pseudo-release's language to search for artist
225+
alias if the user hasn't configured import languages."""
226+
227+
if len(config["import"]["languages"].as_str_seq()) > 0:
228+
return
229+
230+
lang = raw_pseudo_release.get("text-representation", {}).get("language")
231+
artist_credits = raw_pseudo_release.get("release-group", {}).get(
232+
"artist-credit", []
233+
)
234+
aliases = [
235+
artist_credit.get("artist", {}).get("alias-list", [])
236+
for artist_credit in artist_credits
237+
]
238+
239+
if lang and len(lang) >= 2 and len(aliases) > 0:
240+
locale = lang[0:2]
241+
aliases_flattened = list(itertools.chain.from_iterable(aliases))
242+
self._log.debug(
243+
"Using locale '{0}' to search aliases {1}",
244+
locale,
245+
aliases_flattened,
246+
)
247+
if alias_dict := _preferred_alias(aliases_flattened, [locale]):
248+
if alias := alias_dict.get("alias"):
249+
self._log.debug("Got alias '{0}'", alias)
250+
pseudo_release.artist = alias
251+
for track in pseudo_release.tracks:
252+
track.artist = alias
253+
215254
def _add_custom_tags(
216255
self,
217256
official_release: AlbumInfo,

beetsplug/musicbrainz.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ def get_message(self):
118118
BROWSE_MAXTRACKS = 500
119119

120120

121-
def _preferred_alias(aliases: list[JSONDict]):
122-
"""Given an list of alias structures for an artist credit, select
123-
and return the user's preferred alias alias or None if no matching
121+
def _preferred_alias(
122+
aliases: list[JSONDict], languages: list[str] | None = None
123+
) -> JSONDict | None:
124+
"""Given a list of alias structures for an artist credit, select
125+
and return the user's preferred alias or None if no matching
124126
alias is found.
125127
"""
126128
if not aliases:
127-
return
129+
return None
128130

129131
# Only consider aliases that have locales set.
130132
valid_aliases = [a for a in aliases if "locale" in a]
@@ -134,7 +136,10 @@ def _preferred_alias(aliases: list[JSONDict]):
134136
ignored_alias_types = [a.lower() for a in ignored_alias_types]
135137

136138
# Search configured locales in order.
137-
for locale in config["import"]["languages"].as_str_seq():
139+
if languages is None:
140+
languages = config["import"]["languages"].as_str_seq()
141+
142+
for locale in languages:
138143
# Find matching primary aliases for this locale that are not
139144
# being ignored
140145
matches = []
@@ -152,6 +157,8 @@ def _preferred_alias(aliases: list[JSONDict]):
152157

153158
return matches[0]
154159

160+
return None
161+
155162

156163
def _multi_artist_credit(
157164
credit: list[JSONDict], include_join_phrase: bool

test/plugins/test_mbpseudo.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from beets import config
67
from beets.autotag import AlbumMatch
78
from beets.autotag.distance import Distance
89
from beets.autotag.hooks import AlbumInfo, TrackInfo
@@ -230,7 +231,6 @@ class TestMBPseudoPluginCustomTagsOnly(PluginMixin):
230231

231232
@pytest.fixture(scope="class")
232233
def mbpseudo_plugin(self) -> MusicBrainzPseudoReleasePlugin:
233-
self.config["import"]["languages"] = ["en", "jp"]
234234
self.config[self.plugin]["scripts"] = ["Latn"]
235235
self.config[self.plugin]["custom_tags_only"] = True
236236
return MusicBrainzPseudoReleasePlugin()
@@ -255,6 +255,25 @@ def test_custom_tags(
255255
official_release: JSONDict,
256256
pseudo_release: JSONDict,
257257
):
258+
config["import"]["languages"] = []
259+
mbpseudo_plugin._release_getter = (
260+
lambda album_id, includes: pseudo_release
261+
)
262+
album_info = mbpseudo_plugin.album_info(official_release["release"])
263+
assert not isinstance(album_info, PseudoAlbumInfo)
264+
assert album_info.data_source == "MusicBrainzPseudoRelease"
265+
assert album_info["album_transl"] == "In Bloom"
266+
assert album_info["album_artist_transl"] == "Lilas Ikuta"
267+
assert album_info.tracks[0]["title_transl"] == "In Bloom"
268+
assert album_info.tracks[0]["artist_transl"] == "Lilas Ikuta"
269+
270+
def test_custom_tags_with_import_languages(
271+
self,
272+
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
273+
official_release: JSONDict,
274+
pseudo_release: JSONDict,
275+
):
276+
config["import"]["languages"] = ["en", "jp"]
258277
mbpseudo_plugin._release_getter = (
259278
lambda album_id, includes: pseudo_release
260279
)

0 commit comments

Comments
 (0)