-
Notifications
You must be signed in to change notification settings - Fork 1.9k
lastgenre: Use albumartists field to improve last.fm results #5981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d4d9350
1045d9a
bb30a44
d877362
8732163
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -300,24 +300,20 @@ 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.""" | ||||||||||
| def fetch_album_genre(self, albumartist, albumtitle): | ||||||||||
| """Return genres from Last.fm for the album by albumartist.""" | ||||||||||
| return self._last_lookup( | ||||||||||
| "album", LASTFM.get_album, obj.albumartist, obj.album | ||||||||||
| "album", LASTFM.get_album, albumartist, albumtitle | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| 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, artist): | ||||||||||
| """Return genres from Last.fm for the artist.""" | ||||||||||
| return self._last_lookup("artist", LASTFM.get_artist, artist) | ||||||||||
|
|
||||||||||
| 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): | ||||||||||
| """Return genres from Last.fm for the track by artist.""" | ||||||||||
| return self._last_lookup( | ||||||||||
| "track", LASTFM.get_track, obj.artist, obj.title | ||||||||||
| "track", LASTFM.get_track, trackartist, tracktitle | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Main processing: _get_genre() and helpers. | ||||||||||
|
|
@@ -405,14 +401,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 | ||||||||||
| ): | ||||||||||
|
|
@@ -421,20 +417,36 @@ 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 = [] | ||||||||||
| assert isinstance(obj, Album) # Type narrowing for mypy | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Use runtime type checking instead of assert for production code. Consider replacing the assert with an explicit isinstance check and appropriate error handling, since asserts can be disabled in production environments.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was quite tricky to fix. The original mypy error was: With the assert I'm convincing mypy to consider This seemed to be the most easy fix. I tried to fiddle around with changing types within Line 37 in f79c125
but I'm out of my depth here. Some advice for a better fix or if that's a good enough one already please @semohr ? |
||||||||||
| for item in 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: | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.