Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Deduplicate file_date_us, file_date_ca calculation (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
qarkai authored Jan 28, 2024
1 parent 3833021 commit b324a69
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions cyberdrop_dl/utils/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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))
Expand Down

0 comments on commit b324a69

Please sign in to comment.