From ff24f87926362990e1e86098d3bf1c2d17df6502 Mon Sep 17 00:00:00 2001 From: nathom Date: Thu, 1 Apr 2021 12:54:36 -0700 Subject: [PATCH] Formatting --- streamrip/cli.py | 23 ++++++++++++--------- streamrip/config.py | 8 +++++++- streamrip/constants.py | 2 ++ streamrip/downloader.py | 44 +++++++++++++++++++---------------------- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/streamrip/cli.py b/streamrip/cli.py index 3bee3e9..a02758e 100644 --- a/streamrip/cli.py +++ b/streamrip/cli.py @@ -18,9 +18,14 @@ if not os.path.isdir(CACHE_DIR): @click.group(invoke_without_command=True) -@click.option("-c", "--convert", metavar="CODEC", help='alac, mp3, flac, or ogg') -@click.option("-u", "--urls", metavar="URLS", help='Url from Qobuz, Tidal, or Deezer') -@click.option("-q", "--quality", metavar='INT', help=', '.join(range(5))) +@click.option("-c", "--convert", metavar="CODEC", help="alac, mp3, flac, or ogg") +@click.option("-u", "--urls", metavar="URLS", help="Url from Qobuz, Tidal, or Deezer") +@click.option( + "-q", + "--quality", + metavar="INT", + help="0: < 320kbps, 1: 320 kbps, 2: 16 bit/44.1 kHz, 3: 24 bit/<=96 kHz, 4: 24 bit/<=192 kHz", +) @click.option("-t", "--text", metavar="PATH") @click.option("-nd", "--no-db", is_flag=True) @click.option("--debug", is_flag=True) @@ -50,14 +55,14 @@ def cli(ctx, **kwargs): if kwargs["convert"]: config.session["conversion"]["enabled"] = True config.session["conversion"]["codec"] = kwargs["convert"] - if kwargs['quality'] is not None: - if kwargs['quality'] not in range(5): - click.secho("Invalid quality", fg='red') + if kwargs["quality"] is not None: + if kwargs["quality"] not in range(5): + click.secho("Invalid quality", fg="red") return - config.session['qobuz']['quality'] = kwargs['quality'] - config.session['tidal']['quality'] = kwargs['quality'] - config.session['deezer']['quality'] = kwargs['quality'] + config.session["qobuz"]["quality"] = kwargs["quality"] + config.session["tidal"]["quality"] = kwargs["quality"] + config.session["deezer"]["quality"] = kwargs["quality"] core = MusicDL(config) diff --git a/streamrip/config.py b/streamrip/config.py index 69dfb6b..b914f33 100644 --- a/streamrip/config.py +++ b/streamrip/config.py @@ -5,7 +5,13 @@ from pprint import pformat from ruamel.yaml import YAML -from .constants import (CONFIG_PATH, DOWNLOADS_DIR, FOLDER_FORMAT, TRACK_FORMAT, CONFIG_DIR) +from .constants import ( + CONFIG_DIR, + CONFIG_PATH, + DOWNLOADS_DIR, + FOLDER_FORMAT, + TRACK_FORMAT, +) from .exceptions import InvalidSourceError yaml = YAML() diff --git a/streamrip/constants.py b/streamrip/constants.py index 536a98c..753eeff 100644 --- a/streamrip/constants.py +++ b/streamrip/constants.py @@ -124,8 +124,10 @@ COPYRIGHT = "\u2117" PHON_COPYRIGHT = "\u00a9" FLAC_MAX_BLOCKSIZE = 16777215 # 16.7 MB +# TODO: give these more descriptive names TRACK_KEYS = ("tracknumber", "artist", "albumartist", "composer", "title") ALBUM_KEYS = ("albumartist", "title", "year", "bit_depth", "sampling_rate", "container") +# TODO: rename these to DEFAULT_FOLDER_FORMAT etc FOLDER_FORMAT = ( "{albumartist} - {title} ({year}) [{container}] [{bit_depth}B-{sampling_rate}kHz]" ) diff --git a/streamrip/downloader.py b/streamrip/downloader.py index 0592a73..1536197 100644 --- a/streamrip/downloader.py +++ b/streamrip/downloader.py @@ -93,7 +93,6 @@ class Track: self.__dict__.update(kwargs) # adjustments after blind attribute sets - self.file_format = kwargs.get("track_format", TRACK_FORMAT) self.container = "FLAC" self.sampling_rate = 44100 self.bit_depth = 16 @@ -148,7 +147,7 @@ class Track: def download( self, quality: int = 7, - parent_folder: str = "Downloads", + parent_folder: str = "StreamripDownloads", progress_bar: bool = True, database: MusicDB = None, tag: bool = False, @@ -164,11 +163,14 @@ class Track: :param progress_bar: turn on/off progress bar :type progress_bar: bool """ + # args override attributes self.quality, self.folder = ( quality or self.quality, parent_folder or self.folder, ) - self.folder = sanitize_filepath(parent_folder, platform="auto") + + self.file_format = kwargs.get("track_format", TRACK_FORMAT) + self.folder = sanitize_filepath(self.folder, platform="auto") os.makedirs(self.folder, exist_ok=True) @@ -180,18 +182,18 @@ class Track: f"{self['title']} already logged in database, skipping.", fg="magenta", ) - return + return False # because the track was not downloaded - if os.path.isfile(self.format_final_path()): + if os.path.isfile(self.format_final_path()): # track already exists self._is_downloaded = True self._is_tagged = True click.secho(f"Track already downloaded: {self.final_path}", fg="magenta") return False - if hasattr(self, "cover_url"): + if hasattr(self, "cover_url"): # only for playlists and singles self.download_cover() - dl_info = self.client.get_file_url(self.id, quality) # dict + dl_info = self.client.get_file_url(self.id, quality) temp_file = os.path.join(gettempdir(), f"~{self.id}_{quality}.tmp") logger.debug("Temporary file path: %s", temp_file) @@ -207,11 +209,6 @@ class Track: self.sampling_rate = dl_info.get("sampling_rate") self.bit_depth = dl_info.get("bit_depth") - if os.path.isfile(temp_file): - logger.debug("Temporary file found: %s", temp_file) - self._is_downloaded = True - self._is_tagged = False - click.secho(f"\nDownloading {self!s}", fg="blue") if self.client.source in ("qobuz", "tidal"): @@ -244,7 +241,7 @@ class Track: if tag: self.tag() - if not kwargs.get("keep_cover", True) and hasattr(self, 'cover_path'): + if not kwargs.get("keep_cover", True) and hasattr(self, "cover_path"): os.remove(self.cover_path) return True @@ -252,7 +249,7 @@ class Track: def download_cover(self): """Downloads the cover art, if cover_url is given.""" - assert hasattr(self, "cover_url"), "must pass cover_url parameter" + assert hasattr(self, "cover_url"), "must set cover_url attribute" self.cover_path = os.path.join(self.folder, f"cover{hash(self.id)}.jpg") logger.debug(f"Downloading cover from {self.cover_url}") @@ -444,7 +441,7 @@ class Track: @property def title(self): - if hasattr(self, 'meta'): + if hasattr(self, "meta"): return self.meta.title else: raise Exception("Track must be loaded before accessing title") @@ -658,7 +655,6 @@ class Album(Tracklist): self.bit_depth = None self.container = None - self.folder_format = kwargs.get("album_format", FOLDER_FORMAT) for k, v in kwargs.items(): setattr(self, k, v) @@ -873,24 +869,24 @@ class Album(Tracklist): self.downloaded = True def _get_formatter(self) -> dict: - dict_ = dict() + fmt = dict() for key in ALBUM_KEYS: if hasattr(self, key): - dict_[key] = getattr(self, key) + fmt[key] = getattr(self, key) else: - dict_[key] = None + fmt[key] = None - dict_["sampling_rate"] /= 1000 + fmt["sampling_rate"] /= 1000 # 48.0kHz -> 48kHz, 44.1kHz -> 44.1kHz - if dict_["sampling_rate"] % 1 == 0.0: - dict_["sampling_rate"] = int(dict_["sampling_rate"]) + if fmt["sampling_rate"] % 1 == 0.0: + fmt["sampling_rate"] = int(fmt["sampling_rate"]) - return dict_ + return fmt def _get_formatted_folder(self, parent_folder: str) -> str: if self.bit_depth is not None and self.sampling_rate is not None: self.container = "FLAC" - elif self.client.source == "qobuz": + elif self.client.source in ("qobuz", "deezer"): self.container = "MP3" elif self.client.source == "tidal": self.container = "AAC"