94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
"""
|
|
A python program to sort photos by date
|
|
Year Folder
|
|
Named Month folders
|
|
Day of month folders
|
|
"""
|
|
|
|
"""
|
|
Logic:
|
|
sort()
|
|
|
|
def sort():
|
|
For item in list:
|
|
if directory:
|
|
sort(item)
|
|
else:
|
|
try:
|
|
move item to dated folder
|
|
except:
|
|
if dup:
|
|
hash files
|
|
if equal move on
|
|
else
|
|
rename file to be moved by tacking on date shot to filename
|
|
|
|
"""
|
|
|
|
import os
|
|
from os.path import isfile, isdir
|
|
from datetime import datetime
|
|
import time
|
|
import hashlib
|
|
rootlist = os.listdir()
|
|
root = os.getcwd()
|
|
if "Sorted" in rootlist:
|
|
rootlist.remove("Sorted")
|
|
if "Duplicates" in rootlist:
|
|
rootlist.remove("Duplicates")
|
|
if not os.path.isdir("{}/Sorted/".format(root)):
|
|
os.makedirs("{}/Sorted/".format(root))
|
|
if not os.path.isdir("{}/Duplicates/".format(root)):
|
|
os.makedirs("{}/Duplicates/".format(root))
|
|
BUF_SIZE = 65535
|
|
#print(root)
|
|
def sort(item):
|
|
#print(item)
|
|
for x in item:
|
|
#print(x)
|
|
if isfile(x):
|
|
#sort the file
|
|
if x.split('.')[-1].lower() in ["db", "lrprev","lrv", "thm","lrcat", "xmp"]:
|
|
os.remove(x)
|
|
elif ".py" in x:
|
|
pass
|
|
else:
|
|
#print("{} is a file and would be sorted".format(x))
|
|
date = datetime.fromtimestamp(os.path.getmtime(x))
|
|
try:
|
|
os.rename(x, "{}/Sorted/{}/{}/{}/{}".format(root,date.year,date.strftime("%b"),date.day,x))
|
|
except FileNotFoundError:
|
|
os.makedirs("{}/Sorted/{}/{}/{}".format(root,date.year,date.strftime("%b"),date.day))
|
|
os.rename(x, "{}/Sorted/{}/{}/{}/{}".format(root,date.year,date.strftime("%b"),date.day,x))
|
|
except FileExistsError:
|
|
md5_dest = hashlib.md5()
|
|
md5_moving = hashlib.md5()
|
|
with open(x, 'rb') as f:
|
|
while True:
|
|
data = f.read(BUF_SIZE)
|
|
if not data:
|
|
break
|
|
md5_moving.update(data)
|
|
with open("{}/Sorted/{}/{}/{}/{}".format(root,date.year,date.strftime("%b"),date.day,x), 'rb') as f:
|
|
while True:
|
|
data = f.read(BUF_SIZE)
|
|
if not data:
|
|
break
|
|
md5_dest.update(data)
|
|
if md5_dest.hexdigest() == md5_moving.hexdigest():
|
|
#print("Files are the same!")
|
|
os.rename(x, "{}/Duplicates/{}-{}-{}-{}-{}".format(root,date.year,date.strftime("%b"),date.day,time.time(),x))
|
|
else:
|
|
os.chdir(x)
|
|
sort(os.listdir())
|
|
if os.listdir() == []:
|
|
removeable = os.getcwd()
|
|
os.chdir("../")
|
|
os.rmdir(removeable)
|
|
else:
|
|
os.chdir("../")
|
|
start = time.time()
|
|
sort(rootlist)
|
|
end = time.time()
|
|
print("It took {} secs".format(end - start))
|