Skip to content

Commit 6536937

Browse files
committed
lastgenre: Dedup finding correct artist attr
1 parent d2b0e00 commit 6536937

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

beetsplug/lastgenre/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ def _format_genres(self, tags: list[str]) -> list[str]:
371371
else:
372372
return tags
373373

374+
def _artist_for_filter(self, obj: LibModel) -> str | None:
375+
"""Return the representative artist for genre resolution and filtering."""
376+
return (
377+
getattr(obj, "artist", None)
378+
if isinstance(obj, library.Item)
379+
else getattr(obj, "albumartist", None)
380+
or getattr(obj, "artist", None)
381+
)
382+
374383
def _get_existing_genres(self, obj: LibModel) -> list[str]:
375384
"""Return a list of genres for this Item or Album."""
376385
if isinstance(obj, library.Item):
@@ -443,11 +452,11 @@ def _try_resolve_stage(
443452
# If none are found, we use the fallback (if set).
444453
if self.config["cleanup_existing"]:
445454
keep_genres = [g.lower() for g in genres]
446-
cleanup_artist = getattr(obj, "albumartist", None) or getattr(
447-
obj, "artist", None
448-
)
449455
if result := _try_resolve_stage(
450-
"cleanup", keep_genres, [], artist=cleanup_artist
456+
"cleanup",
457+
keep_genres,
458+
[],
459+
artist=self._artist_for_filter(obj),
451460
):
452461
return result
453462

@@ -545,14 +554,7 @@ def _try_resolve_stage(
545554

546555
# Nothing found, leave original if configured and valid.
547556
if genres and self.config["keep_existing"].get():
548-
if isinstance(obj, library.Item):
549-
# For track items, use track artist (important for compilations).
550-
artist = getattr(obj, "artist", None)
551-
else:
552-
# For albums, prefer albumartist, fall back to artist.
553-
artist = getattr(obj, "albumartist", None) or getattr(
554-
obj, "artist", None
555-
)
557+
artist = self._artist_for_filter(obj)
556558
if valid_genres := self._filter_valid(genres, artist=artist):
557559
return valid_genres, "original fallback"
558560
# If the original genre doesn't match a whitelisted genre, check

0 commit comments

Comments
 (0)