Extend jpgSorter.py to allow sorting into years *and* months.

This commit is contained in:
Wouter R 2017-11-25 19:02:08 +01:00
parent 8e4c2a7eb2
commit 73aa74c7f9
2 changed files with 32 additions and 23 deletions

View File

@ -5,7 +5,7 @@ from time import localtime, strftime, strptime, mktime
import shutil import shutil
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
unknownDateFolderName = "Datum unbekannt" unknownDateFolderName = "date-unknown"
def getMinimumCreationTime(exif_data): def getMinimumCreationTime(exif_data):
creationTime = None creationTime = None
@ -51,25 +51,29 @@ def postprocessImage(images, imageDirectory, fileName):
images.append((mktime(creationTime), imagePath)) images.append((mktime(creationTime), imagePath))
image.close() image.close()
# Creates the requested path recursively.
def createPath(newPath):
if not os.path.exists(newPath):
os.makedirs(newPath)
def createNewFolder(destinationRoot, year, eventNumber): # Pass None for month to create 'year/eventNumber' directories instead of 'year/month/eventNumber'.
yearPath = os.path.join(destinationRoot, year) def createNewFolder(destinationRoot, year, month, eventNumber):
if not os.path.exists(yearPath): if month is not None:
os.mkdir(yearPath) newPath = os.path.join(destinationRoot, year, month, str(eventNumber))
eventPath = os.path.join(yearPath, str(eventNumber)) else:
if not os.path.exists(eventPath): newPath = os.path.join(destinationRoot, year, str(eventNumber))
os.mkdir(eventPath)
createPath(newPath)
def createUnknownDateFolder(destinationRoot): def createUnknownDateFolder(destinationRoot):
path = os.path.join(destinationRoot, unknownDateFolderName) path = os.path.join(destinationRoot, unknownDateFolderName)
if not os.path.exists(path): createPath(path)
os.mkdir(path)
def writeImages(images, destinationRoot, splitByMonth=False):
def writeImages(images, destinationRoot):
sortedImages = sorted(images) sortedImages = sorted(images)
previousTime = None previousTime = None
eventNumber = 0 eventNumber = 0
previousDestination = None
today = strftime("%d/%m/%Y") today = strftime("%d/%m/%Y")
for imageTuple in sortedImages: for imageTuple in sortedImages:
@ -77,6 +81,7 @@ def writeImages(images, destinationRoot):
destinationFilePath = "" destinationFilePath = ""
t = localtime(imageTuple[0]) t = localtime(imageTuple[0])
year = strftime("%Y", t) year = strftime("%Y", t)
month = splitByMonth and strftime("%m", t) or None
creationDate = strftime("%d/%m/%Y", t) creationDate = strftime("%d/%m/%Y", t)
fileName = ntpath.basename(imageTuple[1]) fileName = ntpath.basename(imageTuple[1])
@ -87,18 +92,22 @@ def writeImages(images, destinationRoot):
else: else:
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]): if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
previousTime = imageTuple[0]
eventNumber = eventNumber + 1 eventNumber = eventNumber + 1
createNewFolder(destinationRoot, year, eventNumber) createNewFolder(destinationRoot, year, month, eventNumber)
previousTime = imageTuple[0] previousTime = imageTuple[0]
destination = os.path.join(destinationRoot, year, str(eventNumber)) destComponents = [destinationRoot, year, month, str(eventNumber)]
# it may be possible that an event covers 2 years. destComponents = [v for v in destComponents if v is not None]
# in such a case put all the images to the even in the old year destination = os.path.join(*destComponents)
if not (os.path.exists(destination)):
destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber))
# it may be possible that an event covers 2 years.
# in such a case put all the images to the event in the old year
if not (os.path.exists(destination)):
destination = previousDestination
# destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber))
previousDestination = destination
destinationFilePath = os.path.join(destination, fileName) destinationFilePath = os.path.join(destination, fileName)
if not (os.path.exists(destinationFilePath)): if not (os.path.exists(destinationFilePath)):
@ -108,10 +117,10 @@ def writeImages(images, destinationRoot):
os.remove(imageTuple[1]) os.remove(imageTuple[1])
def postprocessImages(imageDirectory): def postprocessImages(imageDirectory, splitByMonth):
images = [] images = []
for root, dirs, files in os.walk(imageDirectory): for root, dirs, files in os.walk(imageDirectory):
for file in files: for file in files:
postprocessImage(images, imageDirectory, file) postprocessImage(images, imageDirectory, file)
writeImages(images, imageDirectory) writeImages(images, imageDirectory, splitByMonth)

View File

@ -91,7 +91,7 @@ for root, dirs, files in os.walk(source, topdown=False):
log(str(fileCounter) + " / " + totalAmountToCopy + " processed.") log(str(fileCounter) + " / " + totalAmountToCopy + " processed.")
log("start special file treatment") log("start special file treatment")
jpgSorter.postprocessImages(os.path.join(destination, "JPG")) jpgSorter.postprocessImages(os.path.join(destination, "JPG"), False)
log("assure max file per folder number") log("assure max file per folder number")
numberOfFilesPerFolderLimiter.limitFilesPerFolder(destination, maxNumberOfFilesPerFolder) numberOfFilesPerFolderLimiter.limitFilesPerFolder(destination, maxNumberOfFilesPerFolder)