Skip to content

Commit 86e07aa

Browse files
committed
lastgenre: Fix multi-artist-albums (squashed)
Often last.fm does not have artist genres for delimiter-separated artist names (eg. "Artist One, Artist Two"). This splits out by (the configured) delimiter and gathers each artist's last.fm genre. FIXME: It would be cool if a configured list of artist name separators would be used instead of a hard-coded one.
1 parent 6c21482 commit 86e07aa

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

beetsplug/lastgenre/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import codecs
2626
import os
27+
import re
2728
import traceback
2829
from typing import Union
2930

@@ -290,6 +291,15 @@ def fetch_album_artist_genre(self, obj):
290291
"""Return raw album artist genres from Last.fm for this Item or Album."""
291292
return self._last_lookup("artist", LASTFM.get_artist, obj.albumartist)
292293

294+
def fetch_split_album_artist_genre(self, split_artist):
295+
"""Return the artist genre for any passed artist name.
296+
297+
Used for multi-artist albums where the artist name may not match
298+
the album artist exactly and a split by separator is needed to get a last.fm
299+
result.
300+
"""
301+
return self._last_lookup("artist", LASTFM.get_artist, split_artist)
302+
293303
def fetch_artist_genre(self, item):
294304
"""Returns raw track artist genres from Last.fm for this Item."""
295305
return self._last_lookup("artist", LASTFM.get_artist, item.artist)
@@ -408,6 +418,47 @@ def _try_resolve_stage(stage_label: str, keep_genres, new_genres):
408418
elif obj.albumartist != config["va_name"].as_str():
409419
new_genres = self.fetch_album_artist_genre(obj)
410420
stage_label = "album artist"
421+
if not new_genres:
422+
if self.config["extended_debug"]:
423+
self._log.debug(
424+
'No album artist genre found for "{0.albumartist}"',
425+
obj,
426+
)
427+
separators = [
428+
re.escape(self.config["separator"].get()),
429+
" feat\\. ",
430+
" featuring ",
431+
" & ",
432+
" vs\\. ",
433+
" x ",
434+
" / ",
435+
" + ",
436+
" and ",
437+
" \\| ",
438+
]
439+
if any(
440+
re.sub(r"\\", "", sep) in obj.albumartist
441+
for sep in separators
442+
):
443+
if self.config["extended_debug"]:
444+
self._log.debug(
445+
"Found separators in album artist - splitting..."
446+
)
447+
# Split on all separators using regex
448+
pattern = "|".join(separators)
449+
albumartists = re.split(pattern, obj.albumartist)
450+
for albumartist in albumartists:
451+
albumartist = albumartist.strip()
452+
if self.config["extended_debug"]:
453+
self._log.debug(
454+
'Fetching multi-artist album genre for "{0}"',
455+
albumartist,
456+
)
457+
new_genres += self.fetch_split_album_artist_genre(
458+
albumartist
459+
)
460+
if new_genres:
461+
label = "album artist (split)"
411462
else:
412463
# For "Various Artists", pick the most popular track genre.
413464
item_genres = []

0 commit comments

Comments
 (0)