summaryrefslogtreecommitdiff
path: root/tidal_dl_ng
diff options
context:
space:
mode:
authorRobert Honz <exislow+github@gmail.com>2024-01-17 22:08:39 +0100
committerRobert Honz <exislow+github@gmail.com>2024-01-17 22:08:39 +0100
commit62c75e6c972eed4aadaba3dd03e60d63c31eb995 (patch)
treee71f22c5757a0e81cf157016d8f9de26234054f3 /tidal_dl_ng
parentbdccb8be37fd941cd5c681950a7e25085efbabb0 (diff)
downloadtidal-dl-ng-62c75e6c972eed4aadaba3dd03e60d63c31eb995.tar.gz
✨ File extension stored in an enum. File exists check refactored to explicit extension check.
Diffstat (limited to 'tidal_dl_ng')
-rw-r--r--tidal_dl_ng/config.py9
-rw-r--r--tidal_dl_ng/constants.py16
-rw-r--r--tidal_dl_ng/download.py26
-rw-r--r--tidal_dl_ng/helper/path.py13
-rw-r--r--tidal_dl_ng/metadata.py1
5 files changed, 44 insertions, 21 deletions
diff --git a/tidal_dl_ng/config.py b/tidal_dl_ng/config.py
index 2fed1ce..fcf8291 100644
--- a/tidal_dl_ng/config.py
+++ b/tidal_dl_ng/config.py
@@ -19,9 +19,13 @@ class BaseConfig:
cls_model: object = None
path_base: str = path_base()
- def save(self) -> None:
+ def save(self, config_to_compare: str = None) -> None:
data_json = self.data.to_json()
+ # If old and current config is equal, skip the write operation.
+ if config_to_compare == data_json:
+ return
+
# Try to create the base folder.
os.makedirs(self.path_base, exist_ok=True)
@@ -59,8 +63,7 @@ class BaseConfig:
self.data = self.cls_model()
# Call save in case of we need to update the saved config, due to changes in code.
- # TODO: Compare if config in memory and on disk is different. Otherwise no write operation.
- self.save()
+ self.save(settings_json)
return result
diff --git a/tidal_dl_ng/constants.py b/tidal_dl_ng/constants.py
index 940b2dc..e6dcd1c 100644
--- a/tidal_dl_ng/constants.py
+++ b/tidal_dl_ng/constants.py
@@ -38,6 +38,16 @@ class CoverDimensions(Enum):
class TidalLists(Enum):
- PLAYLISTS = "Playlists"
- FAVORITES = "Favorites"
- MIXES = "Mixes"
+ PLAYLISTS: str = "Playlists"
+ FAVORITES: str = "Favorites"
+ MIXES: str = "Mixes"
+
+
+class AudioExtensions(Enum):
+ FLAC = ".flac"
+ M4A = ".m4a"
+ MP4 = ".mp4"
+
+
+class MediaExtensions(AudioExtensions):
+ TS = ".ts"
diff --git a/tidal_dl_ng/download.py b/tidal_dl_ng/download.py
index 5d219de..2f54138 100644
--- a/tidal_dl_ng/download.py
+++ b/tidal_dl_ng/download.py
@@ -18,7 +18,14 @@ from rich.progress import Progress, TaskID
from tidalapi import Album, Mix, Playlist, Session, Track, UserPlaylist, Video
from tidal_dl_ng.config import Settings
-from tidal_dl_ng.constants import REQUESTS_TIMEOUT_SEC, CoverDimensions, MediaType, SkipExisting, StreamManifestMimeType
+from tidal_dl_ng.constants import (
+ REQUESTS_TIMEOUT_SEC,
+ CoverDimensions,
+ MediaExtensions,
+ MediaType,
+ SkipExisting,
+ StreamManifestMimeType,
+)
from tidal_dl_ng.helper.decryption import decrypt_file, decrypt_security_token
from tidal_dl_ng.helper.exceptions import MediaMissing, MediaUnknown, UnknownManifestFormat
from tidal_dl_ng.helper.path import check_file_exists, format_path_media, path_file_sanitize
@@ -203,7 +210,6 @@ class Download:
# Compute if and how downloads need to be skipped.
if self.skip_existing.value:
extension_ignore = self.skip_existing == SkipExisting.ExtensionIgnore
- # TODO: Check if extension is already in `path_file` or not.
download_skip = check_file_exists(path_file, extension_ignore=extension_ignore)
else:
download_skip = False
@@ -357,9 +363,9 @@ class Download:
return result
def get_file_extension(self, stream_url: str, stream_codec: str) -> str:
- if ".flac" in stream_url:
- result: str = ".flac"
- elif ".mp4" in stream_url:
+ if MediaExtensions.FLAC.value in stream_url:
+ result: str = MediaExtensions.FLAC.value
+ elif MediaExtensions.MP4.value in stream_url:
# TODO: Need to investigate, what the correct extension is.
# if "ac4" in stream_codec or "mha1" in stream_codec:
# result = ".mp4"
@@ -367,16 +373,16 @@ class Download:
# result = ".flac"
# else:
# result = ".m4a"
- result: str = ".mp4"
- elif ".ts" in stream_url:
- result: str = ".ts"
+ result: str = MediaExtensions.MP4.value
+ elif MediaExtensions.TS.value in stream_url:
+ result: str = MediaExtensions.TS.value
else:
- result: str = ".m4a"
+ result: str = MediaExtensions.M4A.value
return result
def _video_convert(self, path_file: str) -> str:
- path_file_out = os.path.splitext(path_file)[0] + ".mp4"
+ path_file_out = os.path.splitext(path_file)[0] + MediaExtensions.MP4.value
result, _ = ffmpeg.input(path_file).output(path_file_out, map=0, c="copy").run()
return path_file_out
diff --git a/tidal_dl_ng/helper/path.py b/tidal_dl_ng/helper/path.py
index fc8602c..a2ce98c 100644
--- a/tidal_dl_ng/helper/path.py
+++ b/tidal_dl_ng/helper/path.py
@@ -9,7 +9,7 @@ from pathvalidate.error import ValidationError
from tidalapi import Album, Mix, Playlist, Track, UserPlaylist, Video
from tidal_dl_ng import __name_display__
-from tidal_dl_ng.constants import MediaType
+from tidal_dl_ng.constants import AudioExtensions, MediaType
def path_home() -> str:
@@ -210,9 +210,14 @@ def path_file_sanitize(path_file: str, adapt: bool = False) -> (bool, str):
def check_file_exists(path_file: str, extension_ignore: bool = False):
if extension_ignore:
- path_file = Path(path_file).stem + ".*"
+ path_file_stem: str = Path(path_file).stem
+ path_files: [str] = []
- # TODO: Check what happens is (no) files .
- result = bool(glob.glob(path_file))
+ for extension in AudioExtensions:
+ path_files.append(path_file_stem + extension.value)
+ else:
+ path_files: [str] = [path_file]
+
+ result = bool(sum([glob.glob(_file) for _file in path_files], []))
return result
diff --git a/tidal_dl_ng/metadata.py b/tidal_dl_ng/metadata.py
index b11a9c1..7f7edd4 100644
--- a/tidal_dl_ng/metadata.py
+++ b/tidal_dl_ng/metadata.py
@@ -25,7 +25,6 @@ class Metadata:
url_cover: str = None
m: mutagen.mp4.MP4 | mutagen.mp4.MP4 | mutagen.flac.FLAC = None
- # TODO: What about videos?
def __init__(
self,
path_file: str,