From b324a6934cb5c522b8f5ee03d05c423c45d04e01 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Sun, 28 Jan 2024 18:49:46 +0300 Subject: [PATCH] Deduplicate file_date_us, file_date_ca calculation (#795) --- cyberdrop_dl/utils/sorting.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/cyberdrop_dl/utils/sorting.py b/cyberdrop_dl/utils/sorting.py index 503eae564..4c2ca1303 100644 --- a/cyberdrop_dl/utils/sorting.py +++ b/cyberdrop_dl/utils/sorting.py @@ -4,8 +4,8 @@ from pathlib import Path from typing import TYPE_CHECKING -import PIL import filedate +import PIL from PIL import Image from videoprops import get_audio_properties, get_video_properties @@ -15,6 +15,13 @@ from cyberdrop_dl.managers.manager import Manager +def get_file_date_in_us_ca_formats(file: Path) -> tuple(str, str): + file_date = filedate.File(str(file)).get() + file_date_us = file_date['modified'].strftime("%Y-%d-%m") + file_date_ca = file_date['modified'].strftime("%Y-%m-%d") + return file_date_us, file_date_ca + + class Sorter: def __init__(self, manager: 'Manager'): self.download_dir = manager.path_manager.download_dir @@ -111,12 +118,9 @@ async def sort_audio(self, file: Path, base_name: str) -> None: bitrate = "Unknown" sample_rate = "Unknown" - file_date = filedate.File(str(file)).get() - file_date_us = file_date['modified'].strftime("%Y-%d-%m") - file_date_ca = file_date['modified'].strftime("%Y-%m-%d") - parent_name = file.parent.name filename, ext = file.stem, file.suffix + file_date_us, file_date_ca = get_file_date_in_us_ca_formats(file) new_file = Path(self.audio_format.format(sort_dir=self.sorted_downloads, base_dir=base_name, parent_dir=parent_name, filename=filename, ext=ext, length=length, bitrate=bitrate, @@ -136,12 +140,9 @@ async def sort_image(self, file: Path, base_name: str) -> None: except (PIL.UnidentifiedImageError, PIL.Image.DecompressionBombError): resolution = "Unknown" - file_date = filedate.File(str(file)).get() - file_date_us = file_date['modified'].strftime("%Y-%d-%m") - file_date_ca = file_date['modified'].strftime("%Y-%m-%d") - parent_name = file.parent.name filename, ext = file.stem, file.suffix + file_date_us, file_date_ca = get_file_date_in_us_ca_formats(file) new_file = Path(self.image_format.format(sort_dir=self.sorted_downloads, base_dir=base_name, parent_dir=parent_name, filename=filename, ext=ext, resolution=resolution, file_date_us=file_date_us, @@ -168,12 +169,9 @@ async def sort_video(self, file: Path, base_name: str) -> None: frames_per_sec = "Unknown" codec = "Unknown" - file_date = filedate.File(str(file)).get() - file_date_us = file_date['modified'].strftime("%Y-%d-%m") - file_date_ca = file_date['modified'].strftime("%Y-%m-%d") - parent_name = file.parent.name filename, ext = file.stem, file.suffix + file_date_us, file_date_ca = get_file_date_in_us_ca_formats(file) new_file = Path(self.video_format.format(sort_dir=self.sorted_downloads, base_dir=base_name, parent_dir=parent_name, filename=filename, ext=ext, resolution=resolution, fps=frames_per_sec, @@ -187,10 +185,7 @@ async def sort_other(self, file: Path, base_name: str) -> None: parent_name = file.parent.name filename, ext = file.stem, file.suffix - - file_date = filedate.File(str(file)).get() - file_date_us = file_date['modified'].strftime("%Y-%d-%m") - file_date_ca = file_date['modified'].strftime("%Y-%m-%d") + file_date_us, file_date_ca = get_file_date_in_us_ca_formats(file) new_file = Path(self.other_format.format(sort_dir=self.sorted_downloads, base_dir=base_name, parent_dir=parent_name, filename=filename, ext=ext, file_date_us=file_date_us, file_date_ca=file_date_ca))