formatted filepaths

This commit is contained in:
nathannathant 2021-02-28 21:31:43 -08:00
parent 7987653b32
commit 4a57145be7
5 changed files with 194 additions and 58 deletions

View File

@ -55,6 +55,9 @@ def reset_config(config_file):
spoofer = spoofbuz.Spoofer() spoofer = spoofbuz.Spoofer()
config["DEFAULT"]["app_id"] = str(spoofer.getAppId()) config["DEFAULT"]["app_id"] = str(spoofer.getAppId())
config["DEFAULT"]["secrets"] = ",".join(spoofer.getSecrets().values()) 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: with open(config_file, "w") as configfile:
config.write(configfile) config.write(configfile)
logging.info( logging.info(
@ -98,6 +101,8 @@ def main():
no_cover = config.getboolean("DEFAULT", "no_cover") no_cover = config.getboolean("DEFAULT", "no_cover")
no_database = config.getboolean("DEFAULT", "no_database") no_database = config.getboolean("DEFAULT", "no_database")
app_id = config["DEFAULT"]["app_id"] app_id = config["DEFAULT"]["app_id"]
folder_format = config["DEFAULT"]["folder_format"]
track_format = config["DEFAULT"]["track_format"]
secrets = [ secrets = [
secret for secret in config["DEFAULT"]["secrets"].split(",") if secret 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, cover_og_quality=arguments.og_cover or og_cover,
no_cover=arguments.no_cover or no_cover, no_cover=arguments.no_cover or no_cover,
downloads_db=None if no_database or arguments.no_db else QOBUZ_DB, 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) qobuz.initialize_client(email, password, app_id, secrets)

View File

@ -102,6 +102,21 @@ def add_common_arg(custom_parser, default_folder, default_quality):
custom_parser.add_argument( custom_parser.add_argument(
"--no-db", action="store_true", help="don't call the database" "--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( def qobuz_dl_args(

View File

@ -65,6 +65,9 @@ class QobuzDL:
cover_og_quality=False, cover_og_quality=False,
no_cover=False, no_cover=False,
downloads_db=None, 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.directory = self.create_dir(directory)
self.quality = quality self.quality = quality
@ -78,6 +81,8 @@ class QobuzDL:
self.cover_og_quality = cover_og_quality self.cover_og_quality = cover_og_quality
self.no_cover = no_cover self.no_cover = no_cover
self.downloads_db = create_db(downloads_db) if downloads_db else None 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): def initialize_client(self, email, pwd, app_id, secrets):
self.client = qopy.Client(email, pwd, app_id, secrets) self.client = qopy.Client(email, pwd, app_id, secrets)
@ -140,6 +145,8 @@ class QobuzDL:
self.quality_fallback, self.quality_fallback,
self.cover_og_quality, self.cover_og_quality,
self.no_cover, self.no_cover,
folder_format=self.folder_format,
track_format=self.track_format
) )
handle_download_id(self.downloads_db, item_id, add_id=True) handle_download_id(self.downloads_db, item_id, add_id=True)
except (requests.exceptions.RequestException, NonStreamable) as e: except (requests.exceptions.RequestException, NonStreamable) as e:

View File

@ -10,6 +10,9 @@ from qobuz_dl.color import OFF, GREEN, RED, YELLOW, CYAN
from qobuz_dl.exceptions import NonStreamable from qobuz_dl.exceptions import NonStreamable
QL_DOWNGRADE = "FormatRestrictedByFormatAvailability" QL_DOWNGRADE = "FormatRestrictedByFormatAvailability"
DEFAULT_MP3_FOLDER_FORMAT = '{artist} - {album} [MP3]'
DEFAULT_MP3_TRACK_FORMAT = '{tracknumber}. {tracktitle}'
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -30,16 +33,18 @@ def tqdm_download(url, fname, track_name):
def get_description(u: dict, track_title, multiple=None): 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: if multiple:
downloading_title = f"[Disc {multiple}] {downloading_title}" downloading_title = f"[Disc {multiple}] {downloading_title}"
return 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 quality_met = True
if int(quality) == 5: if int(quality) == 5:
return "MP3", quality_met return ("MP3", quality_met, None, None)
track_dict = item_dict track_dict = item_dict
if not is_track_id: if not is_track_id:
track_dict = item_dict["tracks"]["items"][0] 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") restrictions = new_track_dict.get("restrictions")
if isinstance(restrictions, list): if isinstance(restrictions, list):
if any( if any(
restriction.get("code") == QL_DOWNGRADE for restriction in restrictions restriction.get("code") == QL_DOWNGRADE
for restriction in restrictions
): ):
quality_met = False quality_met = False
if (
new_track_dict["bit_depth"] == 16 return (
and new_track_dict["sampling_rate"] == 44.1 "FLAC",
): quality_met,
return "FLAC", quality_met new_track_dict["bit_depth"],
new_track_dict["sampling_rate"]
)
return ( return (
f'{new_track_dict["bit_depth"]}B-{new_track_dict["sampling_rate"]}Khz', "FLAC",
quality_met, quality_met,
new_track_dict["bit_depth"],
new_track_dict["sampling_rate"],
) )
except (KeyError, requests.exceptions.HTTPError): except (KeyError, requests.exceptions.HTTPError):
return "Unknown", quality_met return "Unknown", quality_met
@ -104,6 +114,7 @@ def download_and_tag(
is_mp3, is_mp3,
embed_art=False, embed_art=False,
multiple=None, multiple=None,
track_format='{tracknumber}. {tracktitle}',
): ):
""" """
Download and tag a file Download and tag a file
@ -112,13 +123,15 @@ def download_and_tag(
:param int tmp_count: Temporal download file number :param int tmp_count: Temporal download file number
:param dict track_url_dict: get_track_url dictionary from Qobuz client :param dict track_url_dict: get_track_url dictionary from Qobuz client
:param dict track_metadata: Track item 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_track
:param bool is_mp3 :param bool is_mp3
:param bool embed_art: Embed cover art into file (FLAC-only) :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 :param multiple: Multiple disc integer
:type multiple: integer or None :type multiple: integer or None
""" """
extension = ".mp3" if is_mp3 else ".flac" extension = ".mp3" if is_mp3 else ".flac"
try: try:
@ -134,26 +147,27 @@ def download_and_tag(
filename = os.path.join(root_dir, f".{tmp_count:02}.tmp") filename = os.path.join(root_dir, f".{tmp_count:02}.tmp")
# Determine the filename # Determine the filename
artist = track_metadata.get("performer", {}).get("name") track_title = track_metadata["title"]
album_artist = track_metadata.get("album", {}).get("artist", {}).get("name") filename_attr = {
new_track_title = track_metadata.get("title") 'artist': track_metadata.get("performer", {}).get("name"),
version = track_metadata.get("version") 'albumartist': track_metadata.get("album", {}).get("artist",
{}).get("name"),
if artist or album_artist: 'bit_depth': track_metadata['maximum_bit_depth'],
new_track_title = ( 'sampling_rate': track_metadata['maximum_sampling_rate'],
f"{artist if artist else album_artist}" f' - {track_metadata["title"]}' 'tracktitle': track_title,
) 'version': track_metadata["version"],
if version: 'tracknumber': f"{track_metadata['track_number']:02}"
new_track_title = f"{new_track_title} ({version})" }
# track_format is a format string
track_file = f'{track_metadata["track_number"]:02}. {new_track_title}' # e.g. '{tracknumber}. {artist} - {tracktitle}'
final_file = os.path.join(root_dir, sanitize_filename(track_file))[:250] + extension 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): 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 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) tqdm_download(url, filename, desc)
tag_function = metadata.tag_mp3 if is_mp3 else metadata.tag_flac tag_function = metadata.tag_mp3 if is_mp3 else metadata.tag_flac
try: try:
@ -181,6 +195,9 @@ def download_id_by_type(
downgrade_quality=True, downgrade_quality=True,
cover_og_quality=False, cover_og_quality=False,
no_cover=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) 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 downgrade: Skip releases not available in set quality
:param bool cover_og_quality: Download cover in its original quality :param bool cover_og_quality: Download cover in its original quality
:param bool no_cover: Don't download cover art :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 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: if album:
meta = client.get_album_meta(item_id) meta = client.get_album_meta(item_id)
@ -212,35 +263,52 @@ def download_id_by_type(
return return
album_title = get_title(meta) 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: if not downgrade_quality and not quality_met:
logger.info( 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 return
logger.info(f"\n{YELLOW}Downloading: {album_title}\nQuality: {album_format}\n") logger.info(f"\n{YELLOW}Downloading: {album_title}\n"
dirT = ( f"Quality: {file_format}\n")
meta["artist"]["name"], album_attr = {
album_title, 'artist': meta["artist"]["name"],
meta["release_date_original"].split("-")[0], 'album': album_title,
album_format, '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) dirn = os.path.join(path, sanitized_title)
os.makedirs(dirn, exist_ok=True) os.makedirs(dirn, exist_ok=True)
if no_cover: if no_cover:
logger.info(f"{OFF}Skipping cover") logger.info(f"{OFF}Skipping cover")
else: 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: if "goodies" in meta:
try: try:
get_extra(meta["goodies"][0]["url"], dirn, "booklet.pdf") get_extra(meta["goodies"][0]["url"], dirn, "booklet.pdf")
except: # noqa except: # noqa
pass 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 is_multiple = True if len([*{*media_numbers}]) > 1 else False
for i in meta["tracks"]["items"]: for i in meta["tracks"]["items"]:
parse = client.get_track_url(i["id"], quality) parse = client.get_track_url(i["id"], quality)
@ -256,6 +324,7 @@ def download_id_by_type(
is_mp3, is_mp3,
embed_art, embed_art,
i["media_number"] if is_multiple else None, i["media_number"] if is_multiple else None,
track_format=track_format,
) )
else: else:
logger.info(f"{OFF}Demo. Skipping") logger.info(f"{OFF}Demo. Skipping")
@ -267,29 +336,43 @@ def download_id_by_type(
meta = client.get_track_meta(item_id) meta = client.get_track_meta(item_id)
track_title = get_title(meta) track_title = get_title(meta)
logger.info(f"\n{YELLOW}Downloading: {track_title}") 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: if not downgrade_quality and not quality_met:
logger.info( 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 return
dirT = ( track_attr = {
meta["album"]["artist"]["name"], 'artist': meta["album"]["artist"]["name"],
track_title, 'tracktitle': track_title,
meta["album"]["release_date_original"].split("-")[0], 'year': meta["album"]["release_date_original"].split("-")[0],
track_format, '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) dirn = os.path.join(path, sanitized_title)
os.makedirs(dirn, exist_ok=True) os.makedirs(dirn, exist_ok=True)
if no_cover: if no_cover:
logger.info(f"{OFF}Skipping cover") logger.info(f"{OFF}Skipping cover")
else: else:
get_extra( 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 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: else:
logger.info(f"{OFF}Demo. Skipping") logger.info(f"{OFF}Demo. Skipping")
logger.info(f"{GREEN}Completed") logger.info(f"{GREEN}Completed")

View File

@ -10,6 +10,9 @@ logger = logging.getLogger(__name__)
# unicode symbols # unicode symbols
COPYRIGHT, PHON_COPYRIGHT = '\u2117', '\u00a9' 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): def get_title(track_dict):
@ -30,6 +33,19 @@ def _format_copyright(s: str) -> str:
return s 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 # Use KeyError catching instead of dict.get to avoid empty tags
def tag_flac(filename, root_dir, final_name, d, album, def tag_flac(filename, root_dir, final_name, d, album,
istrack=True, em_image=False): istrack=True, em_image=False):
@ -72,17 +88,17 @@ def tag_flac(filename, root_dir, final_name, d, album,
pass pass
if istrack: if istrack:
audio["GENRE"] = ", ".join(d["album"]["genres_list"]) # GENRE audio["GENRE"] = _format_genres(d["album"]["genres_list"])
audio["ALBUMARTIST"] = d["album"]["artist"]["name"] # ALBUM ARTIST audio["ALBUMARTIST"] = d["album"]["artist"]["name"]
audio["TRACKTOTAL"] = str(d["album"]["tracks_count"]) # TRACK TOTAL audio["TRACKTOTAL"] = str(d["album"]["tracks_count"])
audio["ALBUM"] = d["album"]["title"] # ALBUM TITLE audio["ALBUM"] = d["album"]["title"]
audio["DATE"] = d["album"]["release_date_original"] audio["DATE"] = d["album"]["release_date_original"]
audio["COPYRIGHT"] = _format_copyright(d["copyright"]) audio["COPYRIGHT"] = _format_copyright(d["copyright"])
else: else:
audio["GENRE"] = ", ".join(album["genres_list"]) # GENRE audio["GENRE"] = _format_genres(album["genres_list"])
audio["ALBUMARTIST"] = album["artist"]["name"] # ALBUM ARTIST audio["ALBUMARTIST"] = album["artist"]["name"]
audio["TRACKTOTAL"] = str(album["tracks_count"]) # TRACK TOTAL audio["TRACKTOTAL"] = str(album["tracks_count"])
audio["ALBUM"] = album["title"] # ALBUM TITLE audio["ALBUM"] = album["title"]
audio["DATE"] = album["release_date_original"] audio["DATE"] = album["release_date_original"]
audio["COPYRIGHT"] = _format_copyright(album["copyright"]) 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 cover_image = multi_emb_image
try: 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 = Picture()
image.type = 3 image.type = 3
image.mime = "image/jpeg" image.mime = "image/jpeg"
@ -161,14 +183,14 @@ def tag_mp3(filename, root_dir, final_name, d, album,
tags['artist'] = album["artist"]["name"] tags['artist'] = album["artist"]["name"]
if istrack: if istrack:
tags["genre"] = ", ".join(d["album"]["genres_list"]) tags["genre"] = _format_genres(d["album"]["genres_list"])
tags["albumartist"] = d["album"]["artist"]["name"] tags["albumartist"] = d["album"]["artist"]["name"]
tags["album"] = d["album"]["title"] tags["album"] = d["album"]["title"]
tags["date"] = d["album"]["release_date_original"] tags["date"] = d["album"]["release_date_original"]
tags["copyright"] = _format_copyright(d["copyright"]) tags["copyright"] = _format_copyright(d["copyright"])
tracktotal = str(d["album"]["tracks_count"]) tracktotal = str(d["album"]["tracks_count"])
else: else:
tags["genre"] = ", ".join(album["genres_list"]) tags["genre"] = _format_genres(album["genres_list"])
tags["albumartist"] = album["artist"]["name"] tags["albumartist"] = album["artist"]["name"]
tags["album"] = album["title"] tags["album"] = album["title"]
tags["date"] = album["release_date_original"] tags["date"] = album["release_date_original"]