mirror of
https://github.com/Wojtek242/qobuz-dl.git
synced 2024-11-22 02:55:25 +01:00
formatted filepaths
This commit is contained in:
parent
7987653b32
commit
4a57145be7
@ -55,6 +55,9 @@ def reset_config(config_file):
|
||||
spoofer = spoofbuz.Spoofer()
|
||||
config["DEFAULT"]["app_id"] = str(spoofer.getAppId())
|
||||
config["DEFAULT"]["secrets"] = ",".join(spoofer.getSecrets().values())
|
||||
config["DEFAULT"]["folder_format"] = "{artist} - {album} ({year}) "
|
||||
"[{bit_depth}B-{sampling_rate}kHz]"
|
||||
config["DEFAULT"]["track_format"] = "{tracknumber}. {tracktitle}"
|
||||
with open(config_file, "w") as configfile:
|
||||
config.write(configfile)
|
||||
logging.info(
|
||||
@ -98,6 +101,8 @@ def main():
|
||||
no_cover = config.getboolean("DEFAULT", "no_cover")
|
||||
no_database = config.getboolean("DEFAULT", "no_database")
|
||||
app_id = config["DEFAULT"]["app_id"]
|
||||
folder_format = config["DEFAULT"]["folder_format"]
|
||||
track_format = config["DEFAULT"]["track_format"]
|
||||
secrets = [
|
||||
secret for secret in config["DEFAULT"]["secrets"].split(",") if secret
|
||||
]
|
||||
@ -131,6 +136,10 @@ def main():
|
||||
cover_og_quality=arguments.og_cover or og_cover,
|
||||
no_cover=arguments.no_cover or no_cover,
|
||||
downloads_db=None if no_database or arguments.no_db else QOBUZ_DB,
|
||||
folder_format=arguments.folder_format
|
||||
if hasattr(arguments, "folder_format") else folder_format,
|
||||
track_format=arguments.track_format
|
||||
if hasattr(arguments, "track_format") else track_format,
|
||||
)
|
||||
qobuz.initialize_client(email, password, app_id, secrets)
|
||||
|
||||
|
@ -102,6 +102,21 @@ def add_common_arg(custom_parser, default_folder, default_quality):
|
||||
custom_parser.add_argument(
|
||||
"--no-db", action="store_true", help="don't call the database"
|
||||
)
|
||||
custom_parser.add_argument(
|
||||
"-ff",
|
||||
"--folder-format",
|
||||
metavar='PATTERN',
|
||||
help='pattern for formatting folder names, e.g '
|
||||
'"{artist} - {album} ({year})". available keys: artist, '
|
||||
'album, year, sampling_rate, bit_rate, tracktitle. '
|
||||
'cannot contain characters used by the system, which includes /:<>',
|
||||
)
|
||||
custom_parser.add_argument(
|
||||
"-tf",
|
||||
"--track-format",
|
||||
metavar='PATTERN',
|
||||
help='pattern for formatting track names. see `folder-format`.',
|
||||
)
|
||||
|
||||
|
||||
def qobuz_dl_args(
|
||||
|
@ -65,6 +65,9 @@ class QobuzDL:
|
||||
cover_og_quality=False,
|
||||
no_cover=False,
|
||||
downloads_db=None,
|
||||
folder_format='{artist} - {album} ({year}) [{bit_depth}B-'
|
||||
'{sampling_rate}kHz]',
|
||||
track_format='{tracknumber}. {tracktitle}',
|
||||
):
|
||||
self.directory = self.create_dir(directory)
|
||||
self.quality = quality
|
||||
@ -78,6 +81,8 @@ class QobuzDL:
|
||||
self.cover_og_quality = cover_og_quality
|
||||
self.no_cover = no_cover
|
||||
self.downloads_db = create_db(downloads_db) if downloads_db else None
|
||||
self.folder_format = folder_format
|
||||
self.track_format = track_format
|
||||
|
||||
def initialize_client(self, email, pwd, app_id, secrets):
|
||||
self.client = qopy.Client(email, pwd, app_id, secrets)
|
||||
@ -140,6 +145,8 @@ class QobuzDL:
|
||||
self.quality_fallback,
|
||||
self.cover_og_quality,
|
||||
self.no_cover,
|
||||
folder_format=self.folder_format,
|
||||
track_format=self.track_format
|
||||
)
|
||||
handle_download_id(self.downloads_db, item_id, add_id=True)
|
||||
except (requests.exceptions.RequestException, NonStreamable) as e:
|
||||
|
@ -10,6 +10,9 @@ from qobuz_dl.color import OFF, GREEN, RED, YELLOW, CYAN
|
||||
from qobuz_dl.exceptions import NonStreamable
|
||||
|
||||
QL_DOWNGRADE = "FormatRestrictedByFormatAvailability"
|
||||
DEFAULT_MP3_FOLDER_FORMAT = '{artist} - {album} [MP3]'
|
||||
DEFAULT_MP3_TRACK_FORMAT = '{tracknumber}. {tracktitle}'
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@ -30,16 +33,18 @@ def tqdm_download(url, fname, track_name):
|
||||
|
||||
|
||||
def get_description(u: dict, track_title, multiple=None):
|
||||
downloading_title = f'{track_title} [{u["bit_depth"]}/{u["sampling_rate"]}]'
|
||||
downloading_title = f'{track_title} '
|
||||
f'[{u["bit_depth"]}/{u["sampling_rate"]}]'
|
||||
if multiple:
|
||||
downloading_title = f"[Disc {multiple}] {downloading_title}"
|
||||
return downloading_title
|
||||
|
||||
|
||||
def get_format(client, item_dict, quality, is_track_id=False, track_url_dict=None):
|
||||
def get_format(client, item_dict, quality,
|
||||
is_track_id=False, track_url_dict=None) -> tuple:
|
||||
quality_met = True
|
||||
if int(quality) == 5:
|
||||
return "MP3", quality_met
|
||||
return ("MP3", quality_met, None, None)
|
||||
track_dict = item_dict
|
||||
if not is_track_id:
|
||||
track_dict = item_dict["tracks"]["items"][0]
|
||||
@ -53,17 +58,22 @@ def get_format(client, item_dict, quality, is_track_id=False, track_url_dict=Non
|
||||
restrictions = new_track_dict.get("restrictions")
|
||||
if isinstance(restrictions, list):
|
||||
if any(
|
||||
restriction.get("code") == QL_DOWNGRADE for restriction in restrictions
|
||||
restriction.get("code") == QL_DOWNGRADE
|
||||
for restriction in restrictions
|
||||
):
|
||||
quality_met = False
|
||||
if (
|
||||
new_track_dict["bit_depth"] == 16
|
||||
and new_track_dict["sampling_rate"] == 44.1
|
||||
):
|
||||
return "FLAC", quality_met
|
||||
|
||||
return (
|
||||
"FLAC",
|
||||
quality_met,
|
||||
new_track_dict["bit_depth"],
|
||||
new_track_dict["sampling_rate"]
|
||||
)
|
||||
return (
|
||||
f'{new_track_dict["bit_depth"]}B-{new_track_dict["sampling_rate"]}Khz',
|
||||
"FLAC",
|
||||
quality_met,
|
||||
new_track_dict["bit_depth"],
|
||||
new_track_dict["sampling_rate"],
|
||||
)
|
||||
except (KeyError, requests.exceptions.HTTPError):
|
||||
return "Unknown", quality_met
|
||||
@ -104,6 +114,7 @@ def download_and_tag(
|
||||
is_mp3,
|
||||
embed_art=False,
|
||||
multiple=None,
|
||||
track_format='{tracknumber}. {tracktitle}',
|
||||
):
|
||||
"""
|
||||
Download and tag a file
|
||||
@ -112,13 +123,15 @@ def download_and_tag(
|
||||
:param int tmp_count: Temporal download file number
|
||||
:param dict track_url_dict: get_track_url dictionary from Qobuz client
|
||||
:param dict track_metadata: Track item dictionary from Qobuz client
|
||||
:param dict album_or_track_metadata: Album/track dictionary from Qobuz client
|
||||
:param dict album_or_track_metadata: Album/track dict from Qobuz client
|
||||
:param bool is_track
|
||||
:param bool is_mp3
|
||||
:param bool embed_art: Embed cover art into file (FLAC-only)
|
||||
:param track_format python format-string that determines file naming
|
||||
:param multiple: Multiple disc integer
|
||||
:type multiple: integer or None
|
||||
"""
|
||||
|
||||
extension = ".mp3" if is_mp3 else ".flac"
|
||||
|
||||
try:
|
||||
@ -134,26 +147,27 @@ def download_and_tag(
|
||||
filename = os.path.join(root_dir, f".{tmp_count:02}.tmp")
|
||||
|
||||
# Determine the filename
|
||||
artist = track_metadata.get("performer", {}).get("name")
|
||||
album_artist = track_metadata.get("album", {}).get("artist", {}).get("name")
|
||||
new_track_title = track_metadata.get("title")
|
||||
version = track_metadata.get("version")
|
||||
|
||||
if artist or album_artist:
|
||||
new_track_title = (
|
||||
f"{artist if artist else album_artist}" f' - {track_metadata["title"]}'
|
||||
)
|
||||
if version:
|
||||
new_track_title = f"{new_track_title} ({version})"
|
||||
|
||||
track_file = f'{track_metadata["track_number"]:02}. {new_track_title}'
|
||||
final_file = os.path.join(root_dir, sanitize_filename(track_file))[:250] + extension
|
||||
track_title = track_metadata["title"]
|
||||
filename_attr = {
|
||||
'artist': track_metadata.get("performer", {}).get("name"),
|
||||
'albumartist': track_metadata.get("album", {}).get("artist",
|
||||
{}).get("name"),
|
||||
'bit_depth': track_metadata['maximum_bit_depth'],
|
||||
'sampling_rate': track_metadata['maximum_sampling_rate'],
|
||||
'tracktitle': track_title,
|
||||
'version': track_metadata["version"],
|
||||
'tracknumber': f"{track_metadata['track_number']:02}"
|
||||
}
|
||||
# track_format is a format string
|
||||
# e.g. '{tracknumber}. {artist} - {tracktitle}'
|
||||
formatted_path = sanitize_filename(track_format.format(**filename_attr))
|
||||
final_file = os.path.join(root_dir, formatted_path)[:250] + extension
|
||||
|
||||
if os.path.isfile(final_file):
|
||||
logger.info(f"{OFF}{new_track_title} was already downloaded")
|
||||
logger.info(f"{OFF}{track_title} was already downloaded")
|
||||
return
|
||||
|
||||
desc = get_description(track_url_dict, new_track_title, multiple)
|
||||
desc = get_description(track_url_dict, track_title, multiple)
|
||||
tqdm_download(url, filename, desc)
|
||||
tag_function = metadata.tag_mp3 if is_mp3 else metadata.tag_flac
|
||||
try:
|
||||
@ -181,6 +195,9 @@ def download_id_by_type(
|
||||
downgrade_quality=True,
|
||||
cover_og_quality=False,
|
||||
no_cover=False,
|
||||
folder_format='{artist} - {album} ({year}) '
|
||||
'[{bit_depth}B-{sampling_rate}kHz]',
|
||||
track_format='{tracknumber}. {tracktitle}',
|
||||
):
|
||||
"""
|
||||
Download and get metadata by ID and type (album or track)
|
||||
@ -195,9 +212,43 @@ def download_id_by_type(
|
||||
:param bool downgrade: Skip releases not available in set quality
|
||||
:param bool cover_og_quality: Download cover in its original quality
|
||||
:param bool no_cover: Don't download cover art
|
||||
:param str folder_format: format string that determines folder naming
|
||||
:param str track_format: format string that determines track naming
|
||||
"""
|
||||
count = 0
|
||||
|
||||
def _clean_format_gen(s: str) -> str:
|
||||
'''General clean for format strings. Avoids user errors.
|
||||
'''
|
||||
if s.endswith('.mp3'):
|
||||
s = s[:-4]
|
||||
elif s.endswith('.flac'):
|
||||
s = s[:-5]
|
||||
s = s.strip()
|
||||
return s
|
||||
|
||||
def _not_mp3_valid(s: str) -> bool:
|
||||
return 'bit_depth' in s or 'sample_rate' in s
|
||||
|
||||
def clean_format_str(folder: str, track: str, bit_depth) -> tuple:
|
||||
'''Cleans up the format strings to avoid errors
|
||||
with MP3 files.
|
||||
'''
|
||||
print(f'in clean_format_str: {folder=}, {track=}, {bit_depth=}')
|
||||
folder = _clean_format_gen(folder)
|
||||
track = _clean_format_gen(track)
|
||||
if bit_depth is None: # if is an mp3, there is no bit depth
|
||||
if _not_mp3_valid(folder):
|
||||
logger.error(f'{RED}invalid format string for MP3: "{folder}"'
|
||||
f'\ndefaulting to "{DEFAULT_MP3_FOLDER_FORMAT}"')
|
||||
folder = DEFAULT_MP3_FOLDER_FORMAT
|
||||
if _not_mp3_valid(track):
|
||||
logger.error(f'{RED}invalid format string for MP3: "{track}"'
|
||||
f'\ndefaulting to "{DEFAULT_MP3_TRACK_FORMAT}"')
|
||||
track = DEFAULT_MP3_TRACK_FORMAT
|
||||
|
||||
return folder, track
|
||||
|
||||
if album:
|
||||
meta = client.get_album_meta(item_id)
|
||||
|
||||
@ -212,35 +263,52 @@ def download_id_by_type(
|
||||
return
|
||||
|
||||
album_title = get_title(meta)
|
||||
album_format, quality_met = get_format(client, meta, quality)
|
||||
|
||||
format_info = get_format(client, meta, quality)
|
||||
file_format, quality_met, bit_depth, sampling_rate = format_info
|
||||
|
||||
if not downgrade_quality and not quality_met:
|
||||
logger.info(
|
||||
f"{OFF}Skipping {album_title} as doesn't met quality requirement"
|
||||
f"{OFF}Skipping {album_title} as it doesn't "
|
||||
"meet quality requirement"
|
||||
)
|
||||
return
|
||||
|
||||
logger.info(f"\n{YELLOW}Downloading: {album_title}\nQuality: {album_format}\n")
|
||||
dirT = (
|
||||
meta["artist"]["name"],
|
||||
album_title,
|
||||
meta["release_date_original"].split("-")[0],
|
||||
album_format,
|
||||
logger.info(f"\n{YELLOW}Downloading: {album_title}\n"
|
||||
f"Quality: {file_format}\n")
|
||||
album_attr = {
|
||||
'artist': meta["artist"]["name"],
|
||||
'album': album_title,
|
||||
'year': meta["release_date_original"].split("-")[0],
|
||||
'format': file_format,
|
||||
'bit_depth': bit_depth,
|
||||
'sampling_rate': sampling_rate
|
||||
}
|
||||
# TODO: if the quality is MP3, remove `bit_depth`
|
||||
# and `sampling_rate` tags smartly from format
|
||||
# instead of defaulting to a pre-chosen format
|
||||
folder_format, track_format = clean_format_str(folder_format,
|
||||
track_format,
|
||||
bit_depth)
|
||||
sanitized_title = sanitize_filename(
|
||||
folder_format.format(**album_attr)
|
||||
)
|
||||
sanitized_title = sanitize_filename("{} - {} ({}) [{}]".format(*dirT))
|
||||
dirn = os.path.join(path, sanitized_title)
|
||||
os.makedirs(dirn, exist_ok=True)
|
||||
|
||||
if no_cover:
|
||||
logger.info(f"{OFF}Skipping cover")
|
||||
else:
|
||||
get_extra(meta["image"]["large"], dirn, og_quality=cover_og_quality)
|
||||
get_extra(meta["image"]["large"], dirn,
|
||||
og_quality=cover_og_quality)
|
||||
|
||||
if "goodies" in meta:
|
||||
try:
|
||||
get_extra(meta["goodies"][0]["url"], dirn, "booklet.pdf")
|
||||
except: # noqa
|
||||
pass
|
||||
media_numbers = [track["media_number"] for track in meta["tracks"]["items"]]
|
||||
media_numbers = [track["media_number"] for track in
|
||||
meta["tracks"]["items"]]
|
||||
is_multiple = True if len([*{*media_numbers}]) > 1 else False
|
||||
for i in meta["tracks"]["items"]:
|
||||
parse = client.get_track_url(i["id"], quality)
|
||||
@ -256,6 +324,7 @@ def download_id_by_type(
|
||||
is_mp3,
|
||||
embed_art,
|
||||
i["media_number"] if is_multiple else None,
|
||||
track_format=track_format,
|
||||
)
|
||||
else:
|
||||
logger.info(f"{OFF}Demo. Skipping")
|
||||
@ -267,29 +336,43 @@ def download_id_by_type(
|
||||
meta = client.get_track_meta(item_id)
|
||||
track_title = get_title(meta)
|
||||
logger.info(f"\n{YELLOW}Downloading: {track_title}")
|
||||
track_format, quality_met = get_format(client, meta, quality, True, parse)
|
||||
format_info = get_format(client, meta, quality,
|
||||
is_track_id=True, track_url_dict=parse)
|
||||
file_format, quality_met, bit_depth, sampling_rate = format_info
|
||||
|
||||
folder_format, track_format = clean_format_str(folder_format,
|
||||
track_format,
|
||||
bit_depth)
|
||||
|
||||
if not downgrade_quality and not quality_met:
|
||||
logger.info(
|
||||
f"{OFF}Skipping {track_title} as doesn't met quality requirement"
|
||||
f"{OFF}Skipping {track_title} as it doesn't "
|
||||
"meet quality requirement"
|
||||
)
|
||||
return
|
||||
dirT = (
|
||||
meta["album"]["artist"]["name"],
|
||||
track_title,
|
||||
meta["album"]["release_date_original"].split("-")[0],
|
||||
track_format,
|
||||
track_attr = {
|
||||
'artist': meta["album"]["artist"]["name"],
|
||||
'tracktitle': track_title,
|
||||
'year': meta["album"]["release_date_original"].split("-")[0],
|
||||
'bit_depth': bit_depth,
|
||||
'sampling_rate': sampling_rate
|
||||
}
|
||||
sanitized_title = sanitize_filename(
|
||||
folder_format.format(**track_attr)
|
||||
)
|
||||
sanitized_title = sanitize_filename("{} - {} [{}] [{}]".format(*dirT))
|
||||
dirn = os.path.join(path, sanitized_title)
|
||||
os.makedirs(dirn, exist_ok=True)
|
||||
if no_cover:
|
||||
logger.info(f"{OFF}Skipping cover")
|
||||
else:
|
||||
get_extra(
|
||||
meta["album"]["image"]["large"], dirn, og_quality=cover_og_quality
|
||||
meta["album"]["image"]["large"], dirn,
|
||||
og_quality=cover_og_quality
|
||||
)
|
||||
is_mp3 = True if int(quality) == 5 else False
|
||||
download_and_tag(dirn, count, parse, meta, meta, True, is_mp3, embed_art)
|
||||
download_and_tag(dirn, count, parse, meta,
|
||||
meta, True, is_mp3, embed_art,
|
||||
track_format=track_format)
|
||||
else:
|
||||
logger.info(f"{OFF}Demo. Skipping")
|
||||
logger.info(f"{GREEN}Completed")
|
||||
|
@ -10,6 +10,9 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
# unicode symbols
|
||||
COPYRIGHT, PHON_COPYRIGHT = '\u2117', '\u00a9'
|
||||
# if a metadata block exceeds this, mutagen will raise error
|
||||
# and the file won't be tagged
|
||||
FLAC_MAX_BLOCKSIZE = 16777215
|
||||
|
||||
|
||||
def get_title(track_dict):
|
||||
@ -30,6 +33,19 @@ def _format_copyright(s: str) -> str:
|
||||
return s
|
||||
|
||||
|
||||
def _format_genres(genres: list) -> str:
|
||||
'''Fixes the weirdly formatted genre lists returned by the API.
|
||||
>>> g = ['Pop/Rock', 'Pop/Rock→Rock', 'Pop/Rock→Rock→Alternatif et Indé']
|
||||
>>> _format_genres(g)
|
||||
'Pop/Rock, Rock, Alternatif et Indé'
|
||||
'''
|
||||
|
||||
if genres == []:
|
||||
return ''
|
||||
else:
|
||||
return ', '.join(genres[-1].split('\u2192'))
|
||||
|
||||
|
||||
# Use KeyError catching instead of dict.get to avoid empty tags
|
||||
def tag_flac(filename, root_dir, final_name, d, album,
|
||||
istrack=True, em_image=False):
|
||||
@ -72,17 +88,17 @@ def tag_flac(filename, root_dir, final_name, d, album,
|
||||
pass
|
||||
|
||||
if istrack:
|
||||
audio["GENRE"] = ", ".join(d["album"]["genres_list"]) # GENRE
|
||||
audio["ALBUMARTIST"] = d["album"]["artist"]["name"] # ALBUM ARTIST
|
||||
audio["TRACKTOTAL"] = str(d["album"]["tracks_count"]) # TRACK TOTAL
|
||||
audio["ALBUM"] = d["album"]["title"] # ALBUM TITLE
|
||||
audio["GENRE"] = _format_genres(d["album"]["genres_list"])
|
||||
audio["ALBUMARTIST"] = d["album"]["artist"]["name"]
|
||||
audio["TRACKTOTAL"] = str(d["album"]["tracks_count"])
|
||||
audio["ALBUM"] = d["album"]["title"]
|
||||
audio["DATE"] = d["album"]["release_date_original"]
|
||||
audio["COPYRIGHT"] = _format_copyright(d["copyright"])
|
||||
else:
|
||||
audio["GENRE"] = ", ".join(album["genres_list"]) # GENRE
|
||||
audio["ALBUMARTIST"] = album["artist"]["name"] # ALBUM ARTIST
|
||||
audio["TRACKTOTAL"] = str(album["tracks_count"]) # TRACK TOTAL
|
||||
audio["ALBUM"] = album["title"] # ALBUM TITLE
|
||||
audio["GENRE"] = _format_genres(album["genres_list"])
|
||||
audio["ALBUMARTIST"] = album["artist"]["name"]
|
||||
audio["TRACKTOTAL"] = str(album["tracks_count"])
|
||||
audio["ALBUM"] = album["title"]
|
||||
audio["DATE"] = album["release_date_original"]
|
||||
audio["COPYRIGHT"] = _format_copyright(album["copyright"])
|
||||
|
||||
@ -97,6 +113,12 @@ def tag_flac(filename, root_dir, final_name, d, album,
|
||||
cover_image = multi_emb_image
|
||||
|
||||
try:
|
||||
# rest of the metadata still gets embedded
|
||||
# when the image size is too big
|
||||
if os.path.getsize(cover_image) > FLAC_MAX_BLOCKSIZE:
|
||||
raise Exception("downloaded cover size too large to embed. "
|
||||
"turn off `og_cover` to avoid error")
|
||||
|
||||
image = Picture()
|
||||
image.type = 3
|
||||
image.mime = "image/jpeg"
|
||||
@ -161,14 +183,14 @@ def tag_mp3(filename, root_dir, final_name, d, album,
|
||||
tags['artist'] = album["artist"]["name"]
|
||||
|
||||
if istrack:
|
||||
tags["genre"] = ", ".join(d["album"]["genres_list"])
|
||||
tags["genre"] = _format_genres(d["album"]["genres_list"])
|
||||
tags["albumartist"] = d["album"]["artist"]["name"]
|
||||
tags["album"] = d["album"]["title"]
|
||||
tags["date"] = d["album"]["release_date_original"]
|
||||
tags["copyright"] = _format_copyright(d["copyright"])
|
||||
tracktotal = str(d["album"]["tracks_count"])
|
||||
else:
|
||||
tags["genre"] = ", ".join(album["genres_list"])
|
||||
tags["genre"] = _format_genres(album["genres_list"])
|
||||
tags["albumartist"] = album["artist"]["name"]
|
||||
tags["album"] = album["title"]
|
||||
tags["date"] = album["release_date_original"]
|
||||
|
Loading…
Reference in New Issue
Block a user