mirror of
https://github.com/nathom/streamrip.git
synced 2025-05-13 06:34:45 -04:00
Formatting
This commit is contained in:
parent
93f0e7bf90
commit
ff24f87926
4 changed files with 43 additions and 34 deletions
|
@ -18,9 +18,14 @@ if not os.path.isdir(CACHE_DIR):
|
||||||
|
|
||||||
|
|
||||||
@click.group(invoke_without_command=True)
|
@click.group(invoke_without_command=True)
|
||||||
@click.option("-c", "--convert", metavar="CODEC", help='alac, mp3, flac, or ogg')
|
@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("-u", "--urls", metavar="URLS", help="Url from Qobuz, Tidal, or Deezer")
|
||||||
@click.option("-q", "--quality", metavar='INT', help=', '.join(range(5)))
|
@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("-t", "--text", metavar="PATH")
|
||||||
@click.option("-nd", "--no-db", is_flag=True)
|
@click.option("-nd", "--no-db", is_flag=True)
|
||||||
@click.option("--debug", is_flag=True)
|
@click.option("--debug", is_flag=True)
|
||||||
|
@ -50,14 +55,14 @@ def cli(ctx, **kwargs):
|
||||||
if kwargs["convert"]:
|
if kwargs["convert"]:
|
||||||
config.session["conversion"]["enabled"] = True
|
config.session["conversion"]["enabled"] = True
|
||||||
config.session["conversion"]["codec"] = kwargs["convert"]
|
config.session["conversion"]["codec"] = kwargs["convert"]
|
||||||
if kwargs['quality'] is not None:
|
if kwargs["quality"] is not None:
|
||||||
if kwargs['quality'] not in range(5):
|
if kwargs["quality"] not in range(5):
|
||||||
click.secho("Invalid quality", fg='red')
|
click.secho("Invalid quality", fg="red")
|
||||||
return
|
return
|
||||||
|
|
||||||
config.session['qobuz']['quality'] = kwargs['quality']
|
config.session["qobuz"]["quality"] = kwargs["quality"]
|
||||||
config.session['tidal']['quality'] = kwargs['quality']
|
config.session["tidal"]["quality"] = kwargs["quality"]
|
||||||
config.session['deezer']['quality'] = kwargs['quality']
|
config.session["deezer"]["quality"] = kwargs["quality"]
|
||||||
|
|
||||||
core = MusicDL(config)
|
core = MusicDL(config)
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,13 @@ from pprint import pformat
|
||||||
|
|
||||||
from ruamel.yaml import YAML
|
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
|
from .exceptions import InvalidSourceError
|
||||||
|
|
||||||
yaml = YAML()
|
yaml = YAML()
|
||||||
|
|
|
@ -124,8 +124,10 @@ COPYRIGHT = "\u2117"
|
||||||
PHON_COPYRIGHT = "\u00a9"
|
PHON_COPYRIGHT = "\u00a9"
|
||||||
FLAC_MAX_BLOCKSIZE = 16777215 # 16.7 MB
|
FLAC_MAX_BLOCKSIZE = 16777215 # 16.7 MB
|
||||||
|
|
||||||
|
# TODO: give these more descriptive names
|
||||||
TRACK_KEYS = ("tracknumber", "artist", "albumartist", "composer", "title")
|
TRACK_KEYS = ("tracknumber", "artist", "albumartist", "composer", "title")
|
||||||
ALBUM_KEYS = ("albumartist", "title", "year", "bit_depth", "sampling_rate", "container")
|
ALBUM_KEYS = ("albumartist", "title", "year", "bit_depth", "sampling_rate", "container")
|
||||||
|
# TODO: rename these to DEFAULT_FOLDER_FORMAT etc
|
||||||
FOLDER_FORMAT = (
|
FOLDER_FORMAT = (
|
||||||
"{albumartist} - {title} ({year}) [{container}] [{bit_depth}B-{sampling_rate}kHz]"
|
"{albumartist} - {title} ({year}) [{container}] [{bit_depth}B-{sampling_rate}kHz]"
|
||||||
)
|
)
|
||||||
|
|
|
@ -93,7 +93,6 @@ class Track:
|
||||||
self.__dict__.update(kwargs)
|
self.__dict__.update(kwargs)
|
||||||
|
|
||||||
# adjustments after blind attribute sets
|
# adjustments after blind attribute sets
|
||||||
self.file_format = kwargs.get("track_format", TRACK_FORMAT)
|
|
||||||
self.container = "FLAC"
|
self.container = "FLAC"
|
||||||
self.sampling_rate = 44100
|
self.sampling_rate = 44100
|
||||||
self.bit_depth = 16
|
self.bit_depth = 16
|
||||||
|
@ -148,7 +147,7 @@ class Track:
|
||||||
def download(
|
def download(
|
||||||
self,
|
self,
|
||||||
quality: int = 7,
|
quality: int = 7,
|
||||||
parent_folder: str = "Downloads",
|
parent_folder: str = "StreamripDownloads",
|
||||||
progress_bar: bool = True,
|
progress_bar: bool = True,
|
||||||
database: MusicDB = None,
|
database: MusicDB = None,
|
||||||
tag: bool = False,
|
tag: bool = False,
|
||||||
|
@ -164,11 +163,14 @@ class Track:
|
||||||
:param progress_bar: turn on/off progress bar
|
:param progress_bar: turn on/off progress bar
|
||||||
:type progress_bar: bool
|
:type progress_bar: bool
|
||||||
"""
|
"""
|
||||||
|
# args override attributes
|
||||||
self.quality, self.folder = (
|
self.quality, self.folder = (
|
||||||
quality or self.quality,
|
quality or self.quality,
|
||||||
parent_folder or self.folder,
|
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)
|
os.makedirs(self.folder, exist_ok=True)
|
||||||
|
|
||||||
|
@ -180,18 +182,18 @@ class Track:
|
||||||
f"{self['title']} already logged in database, skipping.",
|
f"{self['title']} already logged in database, skipping.",
|
||||||
fg="magenta",
|
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_downloaded = True
|
||||||
self._is_tagged = True
|
self._is_tagged = True
|
||||||
click.secho(f"Track already downloaded: {self.final_path}", fg="magenta")
|
click.secho(f"Track already downloaded: {self.final_path}", fg="magenta")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if hasattr(self, "cover_url"):
|
if hasattr(self, "cover_url"): # only for playlists and singles
|
||||||
self.download_cover()
|
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")
|
temp_file = os.path.join(gettempdir(), f"~{self.id}_{quality}.tmp")
|
||||||
logger.debug("Temporary file path: %s", temp_file)
|
logger.debug("Temporary file path: %s", temp_file)
|
||||||
|
@ -207,11 +209,6 @@ class Track:
|
||||||
self.sampling_rate = dl_info.get("sampling_rate")
|
self.sampling_rate = dl_info.get("sampling_rate")
|
||||||
self.bit_depth = dl_info.get("bit_depth")
|
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")
|
click.secho(f"\nDownloading {self!s}", fg="blue")
|
||||||
|
|
||||||
if self.client.source in ("qobuz", "tidal"):
|
if self.client.source in ("qobuz", "tidal"):
|
||||||
|
@ -244,7 +241,7 @@ class Track:
|
||||||
if tag:
|
if tag:
|
||||||
self.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)
|
os.remove(self.cover_path)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -252,7 +249,7 @@ class Track:
|
||||||
def download_cover(self):
|
def download_cover(self):
|
||||||
"""Downloads the cover art, if cover_url is given."""
|
"""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")
|
self.cover_path = os.path.join(self.folder, f"cover{hash(self.id)}.jpg")
|
||||||
logger.debug(f"Downloading cover from {self.cover_url}")
|
logger.debug(f"Downloading cover from {self.cover_url}")
|
||||||
|
@ -444,7 +441,7 @@ class Track:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def title(self):
|
def title(self):
|
||||||
if hasattr(self, 'meta'):
|
if hasattr(self, "meta"):
|
||||||
return self.meta.title
|
return self.meta.title
|
||||||
else:
|
else:
|
||||||
raise Exception("Track must be loaded before accessing title")
|
raise Exception("Track must be loaded before accessing title")
|
||||||
|
@ -658,7 +655,6 @@ class Album(Tracklist):
|
||||||
self.bit_depth = None
|
self.bit_depth = None
|
||||||
self.container = None
|
self.container = None
|
||||||
|
|
||||||
self.folder_format = kwargs.get("album_format", FOLDER_FORMAT)
|
|
||||||
for k, v in kwargs.items():
|
for k, v in kwargs.items():
|
||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
|
@ -873,24 +869,24 @@ class Album(Tracklist):
|
||||||
self.downloaded = True
|
self.downloaded = True
|
||||||
|
|
||||||
def _get_formatter(self) -> dict:
|
def _get_formatter(self) -> dict:
|
||||||
dict_ = dict()
|
fmt = dict()
|
||||||
for key in ALBUM_KEYS:
|
for key in ALBUM_KEYS:
|
||||||
if hasattr(self, key):
|
if hasattr(self, key):
|
||||||
dict_[key] = getattr(self, key)
|
fmt[key] = getattr(self, key)
|
||||||
else:
|
else:
|
||||||
dict_[key] = None
|
fmt[key] = None
|
||||||
|
|
||||||
dict_["sampling_rate"] /= 1000
|
fmt["sampling_rate"] /= 1000
|
||||||
# 48.0kHz -> 48kHz, 44.1kHz -> 44.1kHz
|
# 48.0kHz -> 48kHz, 44.1kHz -> 44.1kHz
|
||||||
if dict_["sampling_rate"] % 1 == 0.0:
|
if fmt["sampling_rate"] % 1 == 0.0:
|
||||||
dict_["sampling_rate"] = int(dict_["sampling_rate"])
|
fmt["sampling_rate"] = int(fmt["sampling_rate"])
|
||||||
|
|
||||||
return dict_
|
return fmt
|
||||||
|
|
||||||
def _get_formatted_folder(self, parent_folder: str) -> str:
|
def _get_formatted_folder(self, parent_folder: str) -> str:
|
||||||
if self.bit_depth is not None and self.sampling_rate is not None:
|
if self.bit_depth is not None and self.sampling_rate is not None:
|
||||||
self.container = "FLAC"
|
self.container = "FLAC"
|
||||||
elif self.client.source == "qobuz":
|
elif self.client.source in ("qobuz", "deezer"):
|
||||||
self.container = "MP3"
|
self.container = "MP3"
|
||||||
elif self.client.source == "tidal":
|
elif self.client.source == "tidal":
|
||||||
self.container = "AAC"
|
self.container = "AAC"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue