✨ File extension stored in an enum. File exists check refactored to explicit extension check.

This commit is contained in:
Robert Honz
2024-01-17 22:08:39 +01:00
parent bdccb8be37
commit 62c75e6c97
5 changed files with 44 additions and 21 deletions
+6 -3
View File
@@ -19,9 +19,13 @@ class BaseConfig:
cls_model: object = None cls_model: object = None
path_base: str = path_base() path_base: str = path_base()
def save(self) -> None: def save(self, config_to_compare: str = None) -> None:
data_json = self.data.to_json() 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. # Try to create the base folder.
os.makedirs(self.path_base, exist_ok=True) os.makedirs(self.path_base, exist_ok=True)
@@ -59,8 +63,7 @@ class BaseConfig:
self.data = self.cls_model() self.data = self.cls_model()
# Call save in case of we need to update the saved config, due to changes in code. # 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(settings_json)
self.save()
return result return result
+13 -3
View File
@@ -38,6 +38,16 @@ class CoverDimensions(Enum):
class TidalLists(Enum): class TidalLists(Enum):
PLAYLISTS = "Playlists" PLAYLISTS: str = "Playlists"
FAVORITES = "Favorites" FAVORITES: str = "Favorites"
MIXES = "Mixes" MIXES: str = "Mixes"
class AudioExtensions(Enum):
FLAC = ".flac"
M4A = ".m4a"
MP4 = ".mp4"
class MediaExtensions(AudioExtensions):
TS = ".ts"
+16 -10
View File
@@ -18,7 +18,14 @@ from rich.progress import Progress, TaskID
from tidalapi import Album, Mix, Playlist, Session, Track, UserPlaylist, Video from tidalapi import Album, Mix, Playlist, Session, Track, UserPlaylist, Video
from tidal_dl_ng.config import Settings 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.decryption import decrypt_file, decrypt_security_token
from tidal_dl_ng.helper.exceptions import MediaMissing, MediaUnknown, UnknownManifestFormat 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 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. # Compute if and how downloads need to be skipped.
if self.skip_existing.value: if self.skip_existing.value:
extension_ignore = self.skip_existing == SkipExisting.ExtensionIgnore 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) download_skip = check_file_exists(path_file, extension_ignore=extension_ignore)
else: else:
download_skip = False download_skip = False
@@ -357,9 +363,9 @@ class Download:
return result return result
def get_file_extension(self, stream_url: str, stream_codec: str) -> str: def get_file_extension(self, stream_url: str, stream_codec: str) -> str:
if ".flac" in stream_url: if MediaExtensions.FLAC.value in stream_url:
result: str = ".flac" result: str = MediaExtensions.FLAC.value
elif ".mp4" in stream_url: elif MediaExtensions.MP4.value in stream_url:
# TODO: Need to investigate, what the correct extension is. # TODO: Need to investigate, what the correct extension is.
# if "ac4" in stream_codec or "mha1" in stream_codec: # if "ac4" in stream_codec or "mha1" in stream_codec:
# result = ".mp4" # result = ".mp4"
@@ -367,16 +373,16 @@ class Download:
# result = ".flac" # result = ".flac"
# else: # else:
# result = ".m4a" # result = ".m4a"
result: str = ".mp4" result: str = MediaExtensions.MP4.value
elif ".ts" in stream_url: elif MediaExtensions.TS.value in stream_url:
result: str = ".ts" result: str = MediaExtensions.TS.value
else: else:
result: str = ".m4a" result: str = MediaExtensions.M4A.value
return result return result
def _video_convert(self, path_file: str) -> str: 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() result, _ = ffmpeg.input(path_file).output(path_file_out, map=0, c="copy").run()
return path_file_out return path_file_out
+9 -4
View File
@@ -9,7 +9,7 @@ from pathvalidate.error import ValidationError
from tidalapi import Album, Mix, Playlist, Track, UserPlaylist, Video from tidalapi import Album, Mix, Playlist, Track, UserPlaylist, Video
from tidal_dl_ng import __name_display__ 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: 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): def check_file_exists(path_file: str, extension_ignore: bool = False):
if extension_ignore: 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 . for extension in AudioExtensions:
result = bool(glob.glob(path_file)) 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 return result
-1
View File
@@ -25,7 +25,6 @@ class Metadata:
url_cover: str = None url_cover: str = None
m: mutagen.mp4.MP4 | mutagen.mp4.MP4 | mutagen.flac.FLAC = None m: mutagen.mp4.MP4 | mutagen.mp4.MP4 | mutagen.flac.FLAC = None
# TODO: What about videos?
def __init__( def __init__(
self, self,
path_file: str, path_file: str,