From 7fc81f2703f988b7f1bd3550c0282bf0d6e61de3 Mon Sep 17 00:00:00 2001 From: takanezawa Date: Thu, 6 Feb 2025 20:57:29 +0900 Subject: [PATCH 1/3] get localized artistname and genre, then add them to metadata. --- zotify/__init__.py | 19 ++++++++++++++++++- zotify/playable.py | 27 +++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/zotify/__init__.py b/zotify/__init__.py index 9f95e53f..af5732d6 100644 --- a/zotify/__init__.py +++ b/zotify/__init__.py @@ -30,7 +30,7 @@ from zotify.loader import Loader from zotify.playable import Episode, Track -from zotify.utils import Quality +from zotify.utils import Quality, bytes_to_base62 API_URL = "https://api.sp" + "otify.com/v1/" AUTH_URL = "https://accounts.sp" + "otify.com/" @@ -226,6 +226,7 @@ class ApiClient(LibrespotApiClient): def __init__(self, session: Session): super(ApiClient, self).__init__(session) self.__session = session + self.__localized_artist_names = {} def invoke_url( self, @@ -263,6 +264,22 @@ def invoke_url( except KeyError: return data + def get_localized_artist_name(self, artist_info) -> str: + gid = bytes_to_base62(artist_info.gid) + name = artist_info.name + + if not gid: + return name + if gid in self.__localized_artist_names: + return self.__localized_artist_names[gid] + artist_info = self.invoke_url(f"artists/{gid}") + artist_name = artist_info.get("name") + artist_genres = artist_info.get("genres", []) + if artist_name: + self.__localized_artist_names[gid] = (artist_name, artist_genres) + name = artist_name + return name, artist_genres + def __get_token(self) -> str: return ( self.__session.tokens() diff --git a/zotify/playable.py b/zotify/playable.py index e2b56874..9f08ec1c 100644 --- a/zotify/playable.py +++ b/zotify/playable.py @@ -151,12 +151,30 @@ def __default_metadata(self) -> list[MetadataEntry]: self.track.album = self.__api().get_metadata_4_album( AlbumId.from_hex(bytes_to_hex(self.album.gid)) ) + artist_name, genres_list = self.__api.get_localized_artist_name(self.artist[0]) + artists = [] + for artist in self.artist: + extra_artist_name, extra_genres = self.__api.get_localized_artist_name(artist) + if extra_artist_name: + artists.append(extra_artist_name) + genres_list.extend(extra_genres) + + album_artist_name, extra_genres = self.__api.get_localized_artist_name(self.album.artist[0]) + genres_list = list(set(genres_list + extra_genres)) + + album_artists = [] + for artist in self.album.artist: + extra_artist_name, album_extra_genres = self.__api.get_localized_artist_name(artist) + if extra_artist_name: + album_artists.append(extra_artist_name) + genres_list = list(set(genres_list + album_extra_genres)) + genres = "; ".join(str(item) for item in set(genres_list)) return [ MetadataEntry("album", self.album.name), - MetadataEntry("album_artist", self.album.artist[0].name), - MetadataEntry("album_artists", [a.name for a in self.album.artist]), - MetadataEntry("artist", self.artist[0].name), - MetadataEntry("artists", [a.name for a in self.artist]), + MetadataEntry("album_artist", album_artist_name), + MetadataEntry("album_artists", album_artists), + MetadataEntry("artist", artist_name), + MetadataEntry("artists", artists), MetadataEntry("date", f"{date.year}-{date.month}-{date.day}"), MetadataEntry("disc", self.disc_number), MetadataEntry("duration", self.duration), @@ -167,6 +185,7 @@ def __default_metadata(self) -> list[MetadataEntry]: MetadataEntry("title", self.name), MetadataEntry("track", self.name), MetadataEntry("year", date.year), + MetadataEntry("genre", genres), MetadataEntry( "replaygain_track_gain", self.normalization_data.track_gain_db, "" ), From 6a2e44bc241e65b13b30fb423a774ac0f50b2ac2 Mon Sep 17 00:00:00 2001 From: takanezawa Date: Thu, 6 Feb 2025 21:02:23 +0900 Subject: [PATCH 2/3] fix: check file existence with audio format extension configuration. --- zotify/app.py | 1 + zotify/playable.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/zotify/app.py b/zotify/app.py index d91fda14..d842023f 100644 --- a/zotify/app.py +++ b/zotify/app.py @@ -270,6 +270,7 @@ def download_all(self, collections: list[Collection]) -> None: playable.library, playable.output_template, self.__config.replace_existing, + self.__config.audio_format, ) except FileExistsError: Logger.log( diff --git a/zotify/playable.py b/zotify/playable.py index 9f08ec1c..a7c5d3f6 100644 --- a/zotify/playable.py +++ b/zotify/playable.py @@ -67,6 +67,7 @@ def create_output( library: Path | str = Path("./"), output: str = "{title}", replace: bool = False, + format: AudioFormat = AudioFormat.VORBIS, ) -> Path: """ Creates save directory for the output file @@ -85,7 +86,8 @@ def create_output( "{" + meta.name + "}", fix_filename(meta.string) ) file_path = library.joinpath(output).expanduser() - if file_path.exists() and not replace: + file_path_for_check = file_path.with_name(file_path.name + "." + format.value.ext) + if file_path_for_check.exists() and not replace: raise FileExistsError("File already downloaded") else: file_path.parent.mkdir(parents=True, exist_ok=True) From b67d629eb87497793f1372c30e7aaa4738b72b93 Mon Sep 17 00:00:00 2001 From: takanezawa Date: Thu, 6 Feb 2025 21:13:56 +0900 Subject: [PATCH 3/3] fix: add `discnumber` metadata key for music-tag --- zotify/playable.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zotify/playable.py b/zotify/playable.py index a7c5d3f6..836be855 100644 --- a/zotify/playable.py +++ b/zotify/playable.py @@ -179,6 +179,7 @@ def __default_metadata(self) -> list[MetadataEntry]: MetadataEntry("artists", artists), MetadataEntry("date", f"{date.year}-{date.month}-{date.day}"), MetadataEntry("disc", self.disc_number), + MetadataEntry("discnumber", self.disc_number), MetadataEntry("duration", self.duration), MetadataEntry("explicit", self.explicit, "[E]" if self.explicit else ""), MetadataEntry("isrc", self.external_id[0].id),