feat: fallback на Created Date файла, если нет EXIF

This commit is contained in:
admin committed 2026-06-28 16:28:23 +02:00
1 parent 226d861cfc
commit c5d333f3e9
1 file changed
+26 -9
+26 -9
View File
@@ -23,6 +23,15 @@ from pathlib import Path
EXTENSIONS = {'.jpg', '.jpeg', '.png', '.heic', '.tiff', '.tif', '.webp', '.bmp'}
def get_file_created_date(filepath: Path) -> datetime | None:
"""Возвращает дату создания файла из файловой системы (Windows: st_ctime)."""
try:
ts = filepath.stat().st_ctime
return datetime.fromtimestamp(ts)
except Exception:
return None
def get_exif_date(filepath: Path) -> str | None:
"""Извлекает дату съёмки из EXIF (DateTimeOriginal)."""
try:
@@ -87,17 +96,24 @@ def organize(folder: Path) -> None:
no_date = 0
for filepath in files:
dt = None
date_source = None
# 1. EXIF DateTimeOriginal
date_str = get_exif_date(filepath)
if not date_str:
if date_str:
dt = parse_date(date_str)
# 2. Fallback: дата создания файла (Windows: st_ctime)
if dt is None:
dt = get_file_created_date(filepath)
if dt:
date_source = 'дата создания файла'
if dt is None:
skipped += 1
no_date += 1
print(f' [—] {filepath.name} — нет EXIF даты, оставлен на месте')
continue
dt = parse_date(date_str)
if not dt:
skipped += 1
print(f' [?] {filepath.name} — не удалось распарсить дату "{date_str}"')
print(f' [—] {filepath.name} — нет даты (EXIF + CreatedDate), оставлен на месте')
continue
dest_dir = folder / str(dt.year) / f'{dt.month:02d}' / f'{dt.day:02d}'
@@ -118,7 +134,8 @@ def organize(folder: Path) -> None:
shutil.move(str(filepath), str(dest_path))
moved += 1
print(f' [✓] {filepath.name} → {dt.year}/{dt.month:02d}/{dt.day:02d}/')
src = f' ({date_source})' if date_source else ''
print(f' [✓] {filepath.name} → {dt.year}/{dt.month:02d}/{dt.day:02d}/{src}')
print()
print(f'Готово! Перемещено: {moved}, пропущено: {skipped} (без даты: {no_date})')