Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions beetsplug/lastgenre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import traceback
from functools import singledispatchmethod
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import pylast
import yaml
Expand Down Expand Up @@ -300,24 +300,18 @@ def _last_lookup(self, entity, method, *args):
self._tunelog("last.fm (unfiltered) {} tags: {}", entity, genre)
return genre

def fetch_album_genre(self, obj):
"""Return raw album genres from Last.fm for this Item or Album."""
return self._last_lookup(
"album", LASTFM.get_album, obj.albumartist, obj.album
)
def fetch_album_genre(self, albumartist, album):
"""Return album genres from Last.fm for the album by albumartist."""
return self._last_lookup("album", LASTFM.get_album, albumartist, album)

def fetch_album_artist_genre(self, obj):
"""Return raw album artist genres from Last.fm for this Item or Album."""
return self._last_lookup("artist", LASTFM.get_artist, obj.albumartist)
def fetch_artist_genre(self, artistname):
"""Return artist genres from Last.fm for the artistname."""
return self._last_lookup("artist", LASTFM.get_artist, artistname)

def fetch_artist_genre(self, item):
"""Returns raw track artist genres from Last.fm for this Item."""
return self._last_lookup("artist", LASTFM.get_artist, item.artist)

def fetch_track_genre(self, obj):
"""Returns raw track genres from Last.fm for this Item."""
def fetch_track_genre(self, trackartist, tracktitle):
"""Returns genres from Last.fm for the tracktitle."""
return self._last_lookup(
"track", LASTFM.get_track, obj.artist, obj.title
"track", LASTFM.get_track, trackartist, tracktitle
)

# Main processing: _get_genre() and helpers.
Expand Down Expand Up @@ -405,14 +399,14 @@ def _try_resolve_stage(stage_label: str, keep_genres, new_genres):
# Run through stages: track, album, artist,
# album artist, or most popular track genre.
if isinstance(obj, library.Item) and "track" in self.sources:
if new_genres := self.fetch_track_genre(obj):
if new_genres := self.fetch_track_genre(obj.artist, obj.title):
if result := _try_resolve_stage(
"track", keep_genres, new_genres
):
return result

if "album" in self.sources:
if new_genres := self.fetch_album_genre(obj):
if new_genres := self.fetch_album_genre(obj.albumartist, obj.album):
if result := _try_resolve_stage(
"album", keep_genres, new_genres
):
Expand All @@ -421,20 +415,35 @@ def _try_resolve_stage(stage_label: str, keep_genres, new_genres):
if "artist" in self.sources:
new_genres = []
if isinstance(obj, library.Item):
new_genres = self.fetch_artist_genre(obj)
new_genres = self.fetch_artist_genre(obj.artist)
stage_label = "artist"
elif obj.albumartist != config["va_name"].as_str():
new_genres = self.fetch_album_artist_genre(obj)
new_genres = self.fetch_artist_genre(obj.albumartist)
stage_label = "album artist"
if not new_genres:
self._tunelog(
'No album artist genre found for "{}", '
"trying multi-valued field...",
obj.albumartist,
)
for albumartist in obj.albumartists:
self._tunelog(
'Fetching artist genre for "{}"', albumartist
)
new_genres += self.fetch_artist_genre(albumartist)
if new_genres:
stage_label = "multi-valued album artist"
else:
# For "Various Artists", pick the most popular track genre.
item_genres = []
for item in obj.items():
for item in cast("list[Item]", obj.items()):
item_genre = None
if "track" in self.sources:
item_genre = self.fetch_track_genre(item)
item_genre = self.fetch_track_genre(
item.artist, item.title
)
if not item_genre:
item_genre = self.fetch_artist_genre(item)
item_genre = self.fetch_artist_genre(item.artist)
if item_genre:
item_genres += item_genre
if item_genres:
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Bug fixes:
accepted a list of strings). :bug:`5962`
- Fix a bug introduced in release 2.4.0 where import from any valid
import-log-file always threw a "none of the paths are importable" error.
- :doc:`plugins/lastgenre`: Fix the issue were last.fm does not give a result in
the artist genre stage because multi-artists "concatenation" words (like
"feat." "+", or "&" prevent exact matches. Using the albumartists list field
and fetching a genre for each artist separately massively improves the chance
to get a valid result in that stage.

For plugin developers:

Expand Down
Loading