Skip to content

Commit e2eb909

Browse files
committed
get localized artistname.
get genres in artist info. check file existance with audio format extention. add discnumber to metadata.
1 parent b11ecfa commit e2eb909

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

zotify/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from zotify.loader import Loader
3232
from zotify.playable import Episode, Track
33-
from zotify.utils import Quality
33+
from zotify.utils import Quality, bytes_to_base62
3434

3535
API_URL = "https://api.sp" + "otify.com/v1/"
3636
AUTH_URL = "https://accounts.sp" + "otify.com/"
@@ -226,6 +226,7 @@ class ApiClient(LibrespotApiClient):
226226
def __init__(self, session: Session):
227227
super(ApiClient, self).__init__(session)
228228
self.__session = session
229+
self.__localized_artist_names = {}
229230

230231
def invoke_url(
231232
self,
@@ -263,6 +264,22 @@ def invoke_url(
263264
except KeyError:
264265
return data
265266

267+
def get_localized_artist_name(self, artist_info) -> str:
268+
gid = bytes_to_base62(artist_info.gid)
269+
name = artist_info.name
270+
271+
if not gid:
272+
return name
273+
if gid in self.__localized_artist_names:
274+
return self.__localized_artist_names[gid]
275+
artist_info = self.invoke_url(f"artists/{gid}")
276+
artist_name = artist_info.get("name")
277+
artist_genres = artist_info.get("genres", [])
278+
if artist_name:
279+
self.__localized_artist_names[gid] = (artist_name, artist_genres)
280+
name = artist_name
281+
return name, artist_genres
282+
266283
def __get_token(self) -> str:
267284
return (
268285
self.__session.tokens()

zotify/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def download_all(self, collections: list[Collection]) -> None:
270270
playable.library,
271271
playable.output_template,
272272
self.__config.replace_existing,
273+
self.__config.audio_format,
273274
)
274275
except FileExistsError:
275276
Logger.log(

zotify/playable.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def create_output(
6767
library: Path | str = Path("./"),
6868
output: str = "{title}",
6969
replace: bool = False,
70+
format: AudioFormat = AudioFormat.VORBIS,
7071
) -> Path:
7172
"""
7273
Creates save directory for the output file
@@ -85,7 +86,8 @@ def create_output(
8586
"{" + meta.name + "}", fix_filename(meta.string)
8687
)
8788
file_path = library.joinpath(output).expanduser()
88-
if file_path.exists() and not replace:
89+
file_path_for_check = file_path.with_name(file_path.name + "." + format.value.ext)
90+
if file_path_for_check.exists() and not replace:
8991
raise FileExistsError("File already downloaded")
9092
else:
9193
file_path.parent.mkdir(parents=True, exist_ok=True)
@@ -151,14 +153,33 @@ def __default_metadata(self) -> list[MetadataEntry]:
151153
self.track.album = self.__api().get_metadata_4_album(
152154
AlbumId.from_hex(bytes_to_hex(self.album.gid))
153155
)
156+
artist_name, genres_list = self.__api.get_localized_artist_name(self.artist[0])
157+
artists = []
158+
for artist in self.artist:
159+
extra_artist_name, extra_genres = self.__api.get_localized_artist_name(artist)
160+
if extra_artist_name:
161+
artists.append(extra_artist_name)
162+
genres_list.extend(extra_genres)
163+
164+
album_artist_name, extra_genres = self.__api.get_localized_artist_name(self.album.artist[0])
165+
genres_list = list(set(genres_list + extra_genres))
166+
167+
album_artists = []
168+
for artist in self.album.artist:
169+
extra_artist_name, album_extra_genres = self.__api.get_localized_artist_name(artist)
170+
if extra_artist_name:
171+
album_artists.append(extra_artist_name)
172+
genres_list = list(set(genres_list + album_extra_genres))
173+
genres = "; ".join(str(item) for item in set(genres_list))
154174
return [
155175
MetadataEntry("album", self.album.name),
156-
MetadataEntry("album_artist", self.album.artist[0].name),
157-
MetadataEntry("album_artists", [a.name for a in self.album.artist]),
158-
MetadataEntry("artist", self.artist[0].name),
159-
MetadataEntry("artists", [a.name for a in self.artist]),
176+
MetadataEntry("album_artist", album_artist_name),
177+
MetadataEntry("album_artists", album_artists),
178+
MetadataEntry("artist", artist_name),
179+
MetadataEntry("artists", artists),
160180
MetadataEntry("date", f"{date.year}-{date.month}-{date.day}"),
161181
MetadataEntry("disc", self.disc_number),
182+
MetadataEntry("discnumber", self.disc_number),
162183
MetadataEntry("duration", self.duration),
163184
MetadataEntry("explicit", self.explicit, "[E]" if self.explicit else ""),
164185
MetadataEntry("isrc", self.external_id[0].id),
@@ -167,6 +188,7 @@ def __default_metadata(self) -> list[MetadataEntry]:
167188
MetadataEntry("title", self.name),
168189
MetadataEntry("track", self.name),
169190
MetadataEntry("year", date.year),
191+
MetadataEntry("genre", genres),
170192
MetadataEntry(
171193
"replaygain_track_gain", self.normalization_data.track_gain_db, ""
172194
),

0 commit comments

Comments
 (0)