diff --git a/zotify/track.py b/zotify/track.py index 7fedc66..98e8d65 100644 --- a/zotify/track.py +++ b/zotify/track.py @@ -138,27 +138,39 @@ def get_song_info(song_id) -> Tuple[List[str], List[Any], str, str, Any, Any, An def get_song_genres(rawartists: List[str], track_name: str) -> List[str]: - if Zotify.CONFIG.get_save_genres(): - try: - genres = [] - for data in rawartists: - with Loader(PrintChannel.PROGRESS_INFO, "Fetching artist information..."): - (raw, artistInfo) = Zotify.invoke_url(f'{data[HREF]}') - if Zotify.CONFIG.get_all_genres() and len(artistInfo[GENRES]) > 0: - for genre in artistInfo[GENRES]: - genres.append(genre) - elif len(artistInfo[GENRES]) > 0: - genres.append(artistInfo[GENRES][0]) + """Fetch genres for the track from Spotify's artist metadata. - if len(genres) == 0: - Printer.print(PrintChannel.WARNINGS, 'No Genres found for song ' + track_name) - genres.append('') + For each contributing artist, query the artist's Spotify API endpoint + (via its ``href``) and collect the ``genres`` field. These genres are + then passed to ``set_audio_tags`` so they end up in the audio file's + metadata. + """ + try: + genres: List[str] = [] + for data in rawartists: + with Loader(PrintChannel.PROGRESS_INFO, "Fetching artist information..."): + (raw, artistInfo) = Zotify.invoke_url(f"{data[HREF]}") - return genres - except Exception as e: - raise ValueError(f'Failed to parse GENRES response: {str(e)}\n{raw}') - else: - return [''] + artist_genres = artistInfo.get(GENRES, []) + if not isinstance(artist_genres, list): + artist_genres = [] + + if Zotify.CONFIG.get_all_genres(): + genres.extend(artist_genres) + elif artist_genres: + genres.append(artist_genres[0]) + + # De-duplicate while preserving order + seen = set() + genres = [g for g in genres if not (g in seen or seen.add(g))] + + if not genres: + Printer.print(PrintChannel.WARNINGS, 'No Genres found for song ' + track_name) + return [''] + + return genres + except Exception as e: + raise ValueError(f'Failed to parse GENRES response: {str(e)}') def get_song_lyrics(song_id: str, file_save: Optional[PurePath], title: Optional[str] = None, artists: Optional[List[str]] = None,