#!python3
# remove Minolta original duplicates
# https://github.com/hMatoba/Piexif
# http://ce3wiki.theturninggate.net/doku.php?id=file_name_convention
# destination: // /ab---.jpg
from pathlib import Path
import datetime as dt
import piexif
SRC = r'E:\RawByDateArchive'
DEST = r'E:\RawByDateArchive'
MINOLTA = "Minolta Dynax 5"
rbda = Path(SRC)
for yeardir in rbda.iterdir():
if not yeardir.is_dir():
continue
for daydir in yeardir.iterdir():
minoltadir = Path(daydir,MINOLTA)
if not minoltadir.exists():
continue
for raw in minoltadir.iterdir():
fn = raw.name
if not raw.is_file():
print(f'skipped {fn}')
continue
try:
exif_dict = piexif.load(str(raw))
idto = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode("ascii")
rawdt = dt.datetime.strptime(idto,"%Y:%m:%d %H:%M:%S")
except:
# print(f'skipped {fn}, using file date')
mtime = raw.stat().st_mtime
rawdt = dt.datetime.fromtimestamp(mtime)
year = rawdt.year
day = f"{rawdt:%Y%m%d}"
ts = f"{rawdt:%H%M%S}"
destdir = Path(DEST, f"{year}",f"{day}")
if not destdir.exists():
print(f'creating {destdir}')
destdir.mkdir()
stem = f"ab-{day}-{ts}"
destfn = destdir / f"{stem}{raw.suffix}"
print(f"{fn}", "->", destfn)
# check dng or mrw dup in daydir
dups = daydir.glob(f"{stem}.*")
for d in dups:
print(f"removing {d}")
d.unlink()
if destfn.exists():
print(f'skipped {destfn}, already exists')
continue
raw.rename(destfn)