python_utilities/mediacopy/file_copy.py

73 lines
2.6 KiB
Python
Raw Normal View History

import os
import time
import sys
2021-10-18 00:20:26 +00:00
from shutil import copy2
import filecmp
import pathlib
from PIL import Image, ExifTags
import pendulum
INGEST_DIR = pathlib.PureWindowsPath(r'E:\DCIM\103ND810')
2021-10-18 00:20:26 +00:00
PHOTO_LIBRARY = '//192.168.1.4/photos/photo_library/'
file_list = os.listdir(INGEST_DIR)
# print(file_list)
move_list = []
for item in file_list:
cur_file = pathlib.Path(os.path.join(INGEST_DIR, item))
# print(cur_file)
# print("last modified: %s" % time.ctime(os.path.getmtime(cur_file)))
# created_date = cur_file.stat().st_ctime
# Check for Exif Data
# if no exif data revert to file created time
2021-10-18 00:20:26 +00:00
try:
created_date = Image.open(cur_file)._getexif()[36867]
parsed_date = pendulum.parse(created_date)
except:
created_date = (time.ctime(os.path.getctime(cur_file)))
parsed_date = pendulum.parse(created_date, strict=False)
filename, file_extension = os.path.splitext(item)
filename = item # Critical to the process
if not file_extension.lower() in (".nef", ".jpg", ".mp4"):
2021-10-18 00:20:26 +00:00
continue
# print(filename, file_extension)
# print(created_date)
# print()
2021-10-18 00:20:26 +00:00
# print(parsed_date)
year = str(parsed_date.year)
2021-10-18 00:20:26 +00:00
month = str(parsed_date.month).zfill(2)
day = str(parsed_date.day).zfill(2)
destination = "%s%s/%s/%s/%s" % (PHOTO_LIBRARY, year, month, day, filename)
# print(destination)
print("We want to move %s to %s%s/%s/%s." %
(filename, PHOTO_LIBRARY, year, month, day))
2021-10-18 00:20:26 +00:00
# check if the directory exists
2021-10-18 00:20:26 +00:00
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year))):
print("We couldn't find your directory.")
os.makedirs(os.path.join(PHOTO_LIBRARY, str(year)))
2021-10-18 00:20:26 +00:00
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month))):
print("We couldn't find your directory, making year and month")
2021-10-18 00:20:26 +00:00
os.makedirs(os.path.join(PHOTO_LIBRARY, str(year), str(month)))
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month), str(day))):
print("We couldn't find your directory, making year month and day")
os.makedirs(os.path.join(PHOTO_LIBRARY,
str(year), str(month), str(day)))
2021-10-18 00:20:26 +00:00
# We should be copying, creating a source and destination hash
# comparing the files and then deleting the source
2021-10-18 00:20:26 +00:00
print()
print("Copying %s to %s" % (cur_file, destination))
copy2(cur_file, destination)
if filecmp.cmp(cur_file, destination) == True:
print("%s and %s are a match. Time to delete %s" %
(cur_file, destination, cur_file))
2021-10-18 00:20:26 +00:00
os.remove(cur_file)
# TODO:
# Find files with no extension and save them + directory to a file