Chore: Move to functions

This makes it easy to add new functions and features
Added Removal of empty directories
Added root function to control flow and make development easier
This commit is contained in:
Alexander Hosking 2023-08-16 00:21:10 -04:00
parent e1b32be718
commit 3bb1600e62

View File

@ -7,66 +7,101 @@ import pathlib
from PIL import Image, ExifTags from PIL import Image, ExifTags
import pendulum import pendulum
INGEST_DIR = pathlib.PureWindowsPath(r'E:\DCIM\103ND810') INGEST_DIR = pathlib.PureWindowsPath(
r'C:\Users\ahosking\Nextcloud\Camera Uploads\Alex\2022')
PHOTO_LIBRARY = '//192.168.1.4/photos/photo_library/' PHOTO_LIBRARY = '//192.168.1.4/photos/photo_library/'
file_list = os.listdir(INGEST_DIR) # TODO: Add dircectory traversal
# print(file_list) 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 def compare_and_move():
# if no exif data revert to file created time # move_list = []
try: for root, d_names, f_names in os.walk(INGEST_DIR, topdown=False):
created_date = Image.open(cur_file)._getexif()[36867] for file in f_names:
parsed_date = pendulum.parse(created_date) file_name = os.path.join(root, file)
except: print(file_name)
created_date = (time.ctime(os.path.getctime(cur_file))) file_list.append(file_name)
parsed_date = pendulum.parse(created_date, strict=False) print(root, d_names, f_names)
print(file_list)
print(len(file_list))
process_files(file_list)
remove_empty_directories(INGEST_DIR)
filename, file_extension = os.path.splitext(item)
filename = item # Critical to the process
if not file_extension.lower() in (".nef", ".jpg", ".mp4"):
continue
# print(filename, file_extension) def process_files(file_list):
# print(created_date) for item in file_list:
# print() 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
# print(parsed_date) # Check for Exif Data
year = str(parsed_date.year) # if no exif data revert to file created time
month = str(parsed_date.month).zfill(2) try:
day = str(parsed_date.day).zfill(2) created_date = Image.open(cur_file)._getexif()[36867]
destination = "%s%s/%s/%s/%s" % (PHOTO_LIBRARY, year, month, day, filename) parsed_date = pendulum.parse(created_date)
# print(destination) except:
print("We want to move %s to %s%s/%s/%s." % created_date = (time.ctime(os.path.getctime(cur_file)))
(filename, PHOTO_LIBRARY, year, month, day)) parsed_date = pendulum.parse(created_date, strict=False)
# check if the directory exists filename, file_extension = os.path.splitext(item)
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year))): filename = os.path.basename(item).split(
print("We couldn't find your directory.") '/')[-1] # Critical to the process
os.makedirs(os.path.join(PHOTO_LIBRARY, str(year))) print()
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month))): print(filename)
print("We couldn't find your directory, making year and month") print()
os.makedirs(os.path.join(PHOTO_LIBRARY, str(year), str(month))) if not file_extension.lower() in (".nef", ".jpg", ".mp4", ".mov"):
if not os.path.isdir(os.path.join(PHOTO_LIBRARY, str(year), str(month), str(day))): continue
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)))
# We should be copying, creating a source and destination hash # print(filename, file_extension)
# comparing the files and then deleting the source # print(created_date)
print() # 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))
os.remove(cur_file)
# TODO: # print(parsed_date)
# Find files with no extension and save them + directory to a file year = str(parsed_date.year)
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))
# 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)))
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()
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))
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()