summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Honz <exislow+github@gmail.com>2025-03-24 07:12:16 +0100
committerRobert Honz <exislow+github@gmail.com>2025-03-24 07:12:16 +0100
commitb25b58a03b4aa7a3fc6403178b0c7483805044d2 (patch)
tree280b31aa04525d015c24dbdd700bf06162a231d2
parent1336bebebb0f17fb43c5259511087f858c7f98d3 (diff)
downloadtidal-dl-ng-b25b58a03b4aa7a3fc6403178b0c7483805044d2.tar.gz
👷 Refactored path file split method.
-rw-r--r--tidal_dl_ng/cli.py2
-rw-r--r--tidal_dl_ng/helper/path.py33
2 files changed, 14 insertions, 21 deletions
diff --git a/tidal_dl_ng/cli.py b/tidal_dl_ng/cli.py
index e2fded9..3e47a1a 100644
--- a/tidal_dl_ng/cli.py
+++ b/tidal_dl_ng/cli.py
@@ -139,7 +139,7 @@ def _download(ctx: typer.Context, urls: list[str], try_login: bool = True) -> bo
file_template=file_template,
download_delay=download_delay,
quality_audio=settings.data.quality_audio,
- quality_video=settings.data.quality_video
+ quality_video=settings.data.quality_video,
)
elif media_type in [MediaType.ALBUM, MediaType.PLAYLIST, MediaType.MIX, MediaType.ARTIST]:
item_ids: [int] = []
diff --git a/tidal_dl_ng/helper/path.py b/tidal_dl_ng/helper/path.py
index 1cbd467..da0e410 100644
--- a/tidal_dl_ng/helper/path.py
+++ b/tidal_dl_ng/helper/path.py
@@ -4,10 +4,7 @@ import pathlib
import posixpath
import re
import sys
-from collections.abc import Generator
from copy import deepcopy
-from pathlib import Path
-from typing import Any
from urllib.parse import unquote, urlsplit
from pathvalidate import sanitize_filename, sanitize_filepath
@@ -210,18 +207,7 @@ def get_format_template(
def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: bool = True) -> pathlib.Path:
sanitized_path_file: pathlib.Path = pathlib.Path(path_file.root)
# Get each directory name separately (first value in tuple; second value is for the file suffix).
- to_sanitize: [(str, str)] = []
- receding_is_first: bool = True
-
- for i in receding_path(path_file):
- if receding_is_first:
- receding_is_first = False
-
- to_sanitize.append((i.stem, i.suffix))
- else:
- to_sanitize.append((i.name, ""))
-
- to_sanitize.reverse()
+ to_sanitize: [[str, str]] = path_split_parts_suffix(path_file)
for name, suffix in to_sanitize:
# Sanitize names: We need first top make sure that none file / directory name has bad chars or is longer than 255 chars.
@@ -256,7 +242,7 @@ def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: b
# Sanitize the whole path. The whole path with filename is not allowed to be longer then the max path length depending on the OS.
try:
- sanitized_path_file: str = sanitize_filepath(
+ sanitized_path_file: pathlib.Path = sanitize_filepath(
sanitized_path_file, replacement_text=" ", validate_after_sanitize=True, platform="auto"
)
except ValidationError as e:
@@ -344,8 +330,15 @@ def url_to_filename(url: str) -> str:
return basename
-def receding_path(p: pathlib.Path) -> Generator[Path | Any, Any, None]:
- while str(p) != p.root:
- yield p
+def path_split_parts_suffix(p: pathlib.Path) -> [[str, str]]:
+ """Splits the path to file in parts and also splits the suffix from the file stem.
- p = p.parent
+ :param p: Path to file which should be split in parts.
+ :type p: pathlib.Path
+ :return: List of tuples (Stem, Suffix) of each path part.
+ """
+ result: [[str, str]] = [[part, ""] for part in p.parts]
+ result[-1][0] = p.stem
+ result[-1][-1] = p.suffix
+
+ return result