From 9cec3fccfbe9b0b034bab42c41038f1e63679b9b Mon Sep 17 00:00:00 2001 From: vitiko98 Date: Sat, 12 Dec 2020 17:32:09 -0400 Subject: [PATCH] Fix for #17 Fix for #21 Fix for #20 Feature for #18 --- qobuz_dl/__init__.py | 2 +- qobuz_dl/downloader.py | 40 +++++++++++++++++++++++----------------- qobuz_dl/metadata.py | 30 ++++++++++++++++++++++-------- qobuz_dl/search.py | 7 ++++++- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/qobuz_dl/__init__.py b/qobuz_dl/__init__.py index b08c1c5..c88afc3 100644 --- a/qobuz_dl/__init__.py +++ b/qobuz_dl/__init__.py @@ -1,2 +1,2 @@ -from .qopy import Client from .cli import main +from .qopy import Client diff --git a/qobuz_dl/downloader.py b/qobuz_dl/downloader.py index 8436178..282902b 100644 --- a/qobuz_dl/downloader.py +++ b/qobuz_dl/downloader.py @@ -38,15 +38,23 @@ def getDesc(u, mt, multiple=None): ) -def get_format(album_dict, quality): +def get_format(client, item_dict, quality, is_track_id=False): + if int(quality) == 5: + return "MP3" + track_dict = item_dict + if not is_track_id: + track_dict = [i for i in item_dict["tracks"]["items"]][0] + try: - if int(quality) == 5: - return "MP3" - if album_dict["maximum_bit_depth"] == 16 and int(quality) < 7: + new_track_dict = client.get_track_url(track_dict["id"], quality) + if ( + new_track_dict["bit_depth"] == 16 + and new_track_dict["sampling_rate"] == 44.1 + ): return "FLAC" + return "Hi-Res" except KeyError: return "Unknown" - return "Hi-Res" def get_extra(i, dirn, extra="cover.jpg"): @@ -134,17 +142,16 @@ def download_id_by_type(client, item_id, path, quality, album=False, embed_art=F if album: meta = client.get_album_meta(item_id) - album_title = ( - "{} ({})".format(meta["title"], meta["version"]) - if meta["version"] - else meta["title"] - ) + try: + album_title = "{} ({})".format(meta["title"], meta["version"]) + except KeyError: + album_title = meta["title"] print("\nDownloading: {}\n".format(album_title)) dirT = ( meta["artist"]["name"], album_title, meta["release_date_original"].split("-")[0], - get_format(meta, quality), + get_format(client, meta, quality), ) sanitized_title = sanitize_filename("{} - {} [{}] [{}]".format(*dirT)) dirn = os.path.join(path, sanitized_title) @@ -180,17 +187,16 @@ def download_id_by_type(client, item_id, path, quality, album=False, embed_art=F if "sample" not in parse and parse["sampling_rate"]: meta = client.get_track_meta(item_id) - track_title = ( - "{} ({})".format(meta["title"], meta["version"]) - if meta["version"] - else meta["title"] - ) + try: + track_title = "{} ({})".format(meta["title"], meta["version"]) + except KeyError: + track_title = meta["title"] print("\nDownloading: {}\n".format(track_title)) dirT = ( meta["album"]["artist"]["name"], track_title, meta["album"]["release_date_original"].split("-")[0], - get_format(meta, quality), + get_format(client, meta, quality, True), ) sanitized_title = sanitize_filename("{} - {} [{}] [{}]".format(*dirT)) dirn = os.path.join(path, sanitized_title) diff --git a/qobuz_dl/metadata.py b/qobuz_dl/metadata.py index 7ac3328..f0d4f30 100644 --- a/qobuz_dl/metadata.py +++ b/qobuz_dl/metadata.py @@ -18,10 +18,18 @@ def tag_flac(filename, root_dir, final_name, d, album, istrack=True, em_image=Fa """ audio = FLAC(filename) - audio["TITLE"] = ( - "{} ({})".format(d["title"], d["version"]) if d["version"] else d["title"] - ) # TRACK TITLE + try: + audio["TITLE"] = "{} ({})".format(d["title"], d["version"]) + except KeyError: + audio["TITLE"] = d["title"] + audio["TRACKNUMBER"] = str(d["track_number"]) # TRACK NUMBER + + try: + audio["WORK"] = d["work"] + except KeyError: + pass + try: audio["COMPOSER"] = d["composer"]["name"] # COMPOSER except KeyError: @@ -73,17 +81,23 @@ def tag_mp3(filename, root_dir, final_name, d, album, istrack=True, em_image=Fal :param str root_dir: Root dir used to get the cover art :param str final_name: Final name of the mp3 file (complete path) :param dict d: Track dictionary from Qobuz_client - :param dict album: Album dictionary from Qobuz_client - :param bool istrack + :param bool istrack: Embed cover art into file :param bool em_image: Embed cover art into file """ # TODO: add embedded cover art support for mp3 audio = EasyMP3(filename) - audio["title"] = ( - "{} ({})".format(d["title"], d["version"]) if d["version"] else d["title"] - ) # TRACK TITLE + try: + audio["title"] = "{} ({})".format(d["title"], d["version"]) + except KeyError: + audio["title"] = d["title"] + audio["tracknumber"] = str(d["track_number"]) + + try: + audio["discsubtitle"] = d["work"] + except KeyError: + pass try: audio["composer"] = d["composer"]["name"] except KeyError: diff --git a/qobuz_dl/search.py b/qobuz_dl/search.py index 8b91363..113e51d 100644 --- a/qobuz_dl/search.py +++ b/qobuz_dl/search.py @@ -29,8 +29,13 @@ class Search: self.Total.append("[RELEASE] {} - {} - {} [{}]".format(*items)) self.appendInfo(i, True) except KeyError: + try: + artist_field = i["performer"]["name"] + except KeyError: + print("Download: " + i["title"]) + artist_field = i["composer"]["name"] items = ( - i["performer"]["name"], + artist_field, i["title"], self.seconds(i["duration"]), "HI-RES" if i["hires"] else "Lossless",