Skip to content

Commit

Permalink
Add join_key arg to source plugin get_artist method
Browse files Browse the repository at this point in the history
Add an optional argument to MetadataSourcePlugin.get_artist method that enables
making use of a field containing a keyword supposed to be used to combine
artists together into a single string like "Feat.", "And", "Vs." and so on.
  • Loading branch information
JOJ0 committed Oct 10, 2022
1 parent 407b1fd commit f88d292
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions beets/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,16 @@ def track_for_id(self, track_id=None, track_data=None):
raise NotImplementedError

@staticmethod
def get_artist(artists, id_key='id', name_key='name'):
def get_artist(artists, id_key='id', name_key='name', join_key=None):
"""Returns an artist string (all artists) and an artist_id (the main
artist) for a list of artist object dicts.
For each artist, this function moves articles (such as 'a', 'an',
and 'the') to the front and strips trailing disambiguation numbers. It
returns a tuple containing the comma-separated string of all
normalized artists and the ``id`` of the main/first artist.
Alternatively a keyword can be used to combine artists together into a
single string by passing the join_key argument.
:param artists: Iterable of artist dicts or lists returned by API.
:type artists: list[dict] or list[list]
Expand All @@ -673,11 +675,17 @@ def get_artist(artists, id_key='id', name_key='name'):
to concatenate for the artist string (containing all artists).
Defaults to 'name'.
:type name_key: str or int
:param join_key: Key or index corresponding to a field containing a
keyword to use for combining artists into a single string, for
example "Feat.", "Vs.", "And" or similar. The default is None
which keeps the default behaviour (comma-separated).
:type join_key: str or int
:return: Normalized artist string.
:rtype: str
"""
artist_id = None
artist_names = []
joined = False
for artist in artists:
if not artist_id:
artist_id = artist[id_key]
Expand All @@ -686,8 +694,17 @@ def get_artist(artists, id_key='id', name_key='name'):
name = re.sub(r' \(\d+\)$', '', name)
# Move articles to the front.
name = re.sub(r'^(.*?), (a|an|the)$', r'\2 \1', name, flags=re.I)
# Use a join keyword if requested and available.
if join_key and artist.get(join_key, None):
name += " " + artist[join_key]
joined = True
artist_names.append(name)
artist = ', '.join(artist_names).replace(' ,', ',') or None
# Concat using spaces only if join_key was passed and the join_key was
# ever found. Otherwise join comma-separated.
if join_key and joined is True:
artist = ' '.join(artist_names) or None
else:
artist = ', '.join(artist_names).replace(' ,', ',') or None
return artist, artist_id

def _get_id(self, url_type, id_):
Expand Down

0 comments on commit f88d292

Please sign in to comment.