From 781949fa01da4ce11d3dfe964e9e17f772fdc53a Mon Sep 17 00:00:00 2001 From: Sam Doshi Date: Sat, 16 Feb 2013 17:11:15 +0000 Subject: [PATCH] Add support for artist aliases Use MusicBrainz artist aliases to import files with artist names in an appriorate locale --- beets/autotag/mb.py | 39 +++++++++++++++++++++++++++++++++++++-- beets/config_default.yaml | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/beets/autotag/mb.py b/beets/autotag/mb.py index 896f0cbb5..85108ad08 100644 --- a/beets/autotag/mb.py +++ b/beets/autotag/mb.py @@ -48,6 +48,11 @@ RELEASE_INCLUDES = ['artists', 'media', 'recordings', 'release-groups', 'labels', 'artist-credits'] TRACK_INCLUDES = ['artists'] +# only versions >= 0.3 support artist aliases +if musicbrainzngs.musicbrainz._version >= '0.3': + RELEASE_INCLUDES.append('aliases') + TRACK_INCLUDES.append('aliases') + def configure(): """Set up the python-musicbrainz-ngs module according to settings from the beets configuration. This should be called at startup. @@ -74,12 +79,42 @@ def _flatten_artist_credit(credit): artist_sort_parts.append(el) else: + prefered_locales = config['import']['artist_aliases'].as_str_seq() + chosen_alias = None + if 'alias-list' in el['artist']: + alias_list = el['artist']['alias-list'] + # Get a list of the aliases that have set locales + set_locales = [a for a in alias_list if 'locale' in a] + # Search locales in order + for locale in prefered_locales: + # Does anything match + matches = [a for a in set_locales if a['locale'] == locale] + # Skip to next locale if no matches + if len(matches) == 0: + continue + # Find the aliases that have the primary flag set + primaries = [a for a in matches if 'primary' in a] + # Take the primary if we have it, otherwise take the first + # match with the correct locale + if len(primaries) > 0: + chosen_alias = primaries[0] + else: + chosen_alias = matches[0] + # If we get here we must have found an acceptable alias + # so stop looking + break + # An artist. - cur_artist_name = el['artist']['name'] + if chosen_alias is not None: + cur_artist_name = chosen_alias['alias'] + else: + cur_artist_name = el['artist']['name'] artist_parts.append(cur_artist_name) # Artist sort name. - if 'sort-name' in el['artist']: + if chosen_alias is not None: + artist_sort_parts.append(chosen_alias['sort-name']) + elif 'sort-name' in el['artist']: artist_sort_parts.append(el['artist']['sort-name']) else: artist_sort_parts.append(cur_artist_name) diff --git a/beets/config_default.yaml b/beets/config_default.yaml index a466a63ef..fb2d78c9f 100644 --- a/beets/config_default.yaml +++ b/beets/config_default.yaml @@ -16,6 +16,7 @@ import: quiet: no singletons: no default_action: apply + artist_aliases: [] clutter: ["Thumbs.DB", ".DS_Store"] ignore: [".*", "*~"]