Skip to content
Open
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
19 changes: 18 additions & 1 deletion zotify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions zotify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 27 additions & 5 deletions zotify/playable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -151,14 +153,33 @@ 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("discnumber", self.disc_number),
MetadataEntry("duration", self.duration),
MetadataEntry("explicit", self.explicit, "[E]" if self.explicit else ""),
MetadataEntry("isrc", self.external_id[0].id),
Expand All @@ -167,6 +188,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, ""
),
Expand Down