Skip to content

Commit

Permalink
formatted filepaths
Browse files Browse the repository at this point in the history
  • Loading branch information
nathom committed Mar 1, 2021
1 parent 7987653 commit 4a57145
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 58 deletions.
9 changes: 9 additions & 0 deletions qobuz_dl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
]
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions qobuz_dl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions qobuz_dl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def __init__(
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
Expand All @@ -78,6 +81,8 @@ def __init__(
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)
Expand Down Expand Up @@ -140,6 +145,8 @@ def download_from_id(self, item_id, album=True, alt_path=None):
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:
Expand Down
179 changes: 131 additions & 48 deletions qobuz_dl/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
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__)


Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -104,6 +114,7 @@ def download_and_tag(
is_mp3,
embed_art=False,
multiple=None,
track_format='{tracknumber}. {tracktitle}',
):
"""
Download and tag a file
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Loading

0 comments on commit 4a57145

Please sign in to comment.