This commit is contained in:
Nico Melone
2021-08-23 19:32:35 -05:00
parent 0b91167ffe
commit ba6ddfe180
2 changed files with 125 additions and 0 deletions

32
remover.py Normal file
View File

@@ -0,0 +1,32 @@
#remove .LRV .lrprev .THM .lrcat
import os
from os.path import isfile, isdir
from datetime import datetime
import time
rootlist = os.listdir()
root = os.getcwd()
def clean(item):
#print(item)
for x in item:
#print(x)
if isfile(x):
#sort the file
if ".db" in x or ".lrprev" in x or ".LRV" in x or ".THM" in x or ".lrcat" in x:
os.remove(x)
elif ".py" in x:
pass
else:
os.chdir(x)
clean(os.listdir())
if os.listdir() == []:
removeable = os.getcwd()
os.chdir("../")
os.rmdir(removeable)
else:
os.chdir("../")
start = time.time()
clean(rootlist)
end = time.time()
print("It took {} secs".format(end - start))

93
sorter.py Normal file
View File

@@ -0,0 +1,93 @@
"""
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))