python_utilities/mediacopy/file_copy.py

120 lines
4.2 KiB
Python
Raw Permalink 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
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
INGEST_DIR = filedialog.askdirectory()
# INGEST_DIR = pathlib.PureWindowsPath(
# r'C:\Users\ahosking\Nextcloud\Camera Uploads\Alex\2022')
2021-10-18 00:20:26 +00:00
PHOTO_LIBRARY = '//192.168.1.4/photos/photo_library/'
# TODO: Add dircectory traversal
file_list = []
dir_list = {}
def compare_and_move():
# move_list = []
for root, d_names, f_names in os.walk(INGEST_DIR, topdown=False):
for file in f_names:
file_name = os.path.join(root, file)
# print(file_name) # Debugging
file_list.append(file_name)
# print(root, d_names, f_names) # Debugging
# print(file_list) # Debugging
# print(len(file_list)) # Debugging
process_files(file_list)
print(dir_list.keys())
# remove_empty_directories(INGEST_DIR)
def process_files(file_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
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 = os.path.basename(item).split(
'/')[-1] # Critical to the process
print()
print(filename)
print()
if not file_extension.lower() in (".nef", ".jpg", ".mp4", ".mov"):
continue
# print(filename, file_extension)
# print(created_date)
# print()
# print(parsed_date)
year = str(parsed_date.year)
month = str(parsed_date.month).zfill(2)
day = str(parsed_date.day).zfill(2)
destination_path = "%s%s/%s" % (PHOTO_LIBRARY, year, month)
destination = "%s/%s" % (destination_path, filename)
if not destination in dir_list.keys():
dir_list[destination_path] = []
dir_list[destination_path].append(cur_file)
# print(destination)
print("We want to move %s to %s%s/%s." %
(filename, PHOTO_LIBRARY, year, month))
# check if the directory exists
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)))
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month))):
print("We couldn't find your directory, matching year and month")
os.makedirs(os.path.join(PHOTO_LIBRARY, str(year), str(month)))
# I'm not sure that I care about the day anymore #
# if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month), str(day))):
# print("We couldn't find your directory, matching year month and day")
# os.makedirs(os.path.join(PHOTO_LIBRARY,
# str(year), str(month), str(day)))
# We should be copying, creating a source and destination hash
# comparing the files and then deleting the source
print("\nCopying %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))
os.remove(cur_file)
# TODO:
# Find files with no extension and save them + directory to a file
def remove_empty_directories(directory):
for root, d_names, f_names in os.walk(directory, topdown=False):
print(root, d_names, f_names)
if len(d_names) == 0 and len(f_names) == 0:
print("I want to remove:", root)
os.rmdir(root)
if not os.path.isdir(root):
print(root, "was removed.")
if __name__ == "__main__":
compare_and_move()