diff --git a/streamrip/bases.py b/streamrip/bases.py index f3d628a..196a594 100644 --- a/streamrip/bases.py +++ b/streamrip/bases.py @@ -374,7 +374,7 @@ class Track: a dict with the keys allowed in formatter strings, and their values in the TrackMetadata object. """ - formatter = self.meta.get_formatter() + formatter = self.meta.get_formatter(max_quality=self.quality) logger.debug("Track meta formatter %s", formatter) filename = clean_format(self.file_format, formatter) self.final_path = os.path.join(self.folder, filename)[ diff --git a/streamrip/metadata.py b/streamrip/metadata.py index 8f48dc5..b1da430 100644 --- a/streamrip/metadata.py +++ b/streamrip/metadata.py @@ -446,7 +446,7 @@ class TrackMetadata: """ self._year = val - def get_formatter(self) -> dict: + def get_formatter(self, max_quality: int) -> dict: """Return a dict that is used to apply values to file format strings. :rtype: dict diff --git a/streamrip/tracklists.py b/streamrip/tracklists.py index 0180c40..61d0332 100644 --- a/streamrip/tracklists.py +++ b/streamrip/tracklists.py @@ -21,6 +21,7 @@ from .utils import ( clean_format, get_container, safe_get, + get_stats_from_quality, tidal_cover_url, tqdm_download, ) @@ -104,10 +105,7 @@ class Album(Tracklist): """ # Generate the folder name self.folder_format = kwargs.get("folder_format", FOLDER_FORMAT) - if not hasattr(self, "quality"): - self.quality = min( - kwargs.get("quality", 3), self.client.max_quality - ) + self.quality = min(kwargs.get("quality", 3), self.client.max_quality) self.folder = self._get_formatted_folder( kwargs.get("parent_folder", "StreamripDownloads"), self.quality @@ -238,20 +236,25 @@ class Album(Tracklist): ) def _get_formatter(self) -> dict: - """Get a formatter that is used for previews in core.py. + """Get a formatter that is used for naming folders and previews. :rtype: dict """ - fmt = dict() - for key in ALBUM_KEYS: - # default to None - fmt[key] = self.get(key) + fmt = {key: self.get(key) for key in ALBUM_KEYS} - if fmt.get("sampling_rate", False): - fmt["sampling_rate"] /= 1000 - # change 48.0kHz -> 48kHz, 44.1kHz -> 44.1kHz - if fmt["sampling_rate"] % 1 == 0.0: - fmt["sampling_rate"] = int(fmt["sampling_rate"]) + max_bd, max_sr = get_stats_from_quality(self.quality) + if max_sr < fmt.get("sampling_rate", 0) or max_bd < fmt.get( + "bit_depth", 0 + ): + fmt["sampling_rate"] = max_sr + fmt["bit_depth"] = max_bd + + if sr := fmt.get("sampling_rate"): + if sr % 1000 == 0: + # truncate the decimal .0 when converting to str + fmt["sampling_rate"] = int(sr / 1000) + else: + fmt["sampling_rate"] = sr / 1000 return fmt diff --git a/streamrip/utils.py b/streamrip/utils.py index 4843343..23abc73 100644 --- a/streamrip/utils.py +++ b/streamrip/utils.py @@ -5,7 +5,7 @@ import logging import os import re from string import Formatter -from typing import Dict, Hashable, Optional, Union +from typing import Dict, Hashable, Optional, Union, Tuple import click import requests @@ -14,7 +14,7 @@ from requests.packages import urllib3 from tqdm import tqdm from .constants import AGENT, TIDAL_COVER_URL -from .exceptions import InvalidSourceError, NonStreamable +from .exceptions import InvalidSourceError, NonStreamable, InvalidQuality urllib3.disable_warnings() logger = logging.getLogger("streamrip") @@ -109,6 +109,21 @@ def get_quality_id(bit_depth: Optional[int], sampling_rate: Optional[int]): return 4 +def get_stats_from_quality( + quality_id: int, +) -> Tuple[Optional[int], Optional[int]]: + if quality_id <= 1: + return (None, None) + elif quality_id == 2: + return (16, 44100) + elif quality_id == 3: + return (24, 96000) + elif quality_id == 4: + return (24, 192000) + else: + raise InvalidQuality(quality_id) + + def tqdm_download( url: str, filepath: str, params: dict = None, desc: str = None ):