From 73aa74c7f9691b45962439544eb417ad0b5102fc Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sat, 25 Nov 2017 19:02:08 +0100 Subject: [PATCH] Extend jpgSorter.py to allow sorting into years *and* months. --- jpgSorter.py | 53 ++++++++++++++++++++++++++++++---------------------- recovery.py | 2 +- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/jpgSorter.py b/jpgSorter.py index 467086f..b18f437 100644 --- a/jpgSorter.py +++ b/jpgSorter.py @@ -5,7 +5,7 @@ from time import localtime, strftime, strptime, mktime import shutil minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds -unknownDateFolderName = "Datum unbekannt" +unknownDateFolderName = "date-unknown" def getMinimumCreationTime(exif_data): creationTime = None @@ -51,25 +51,29 @@ def postprocessImage(images, imageDirectory, fileName): images.append((mktime(creationTime), imagePath)) image.close() +# Creates the requested path recursively. +def createPath(newPath): + if not os.path.exists(newPath): + os.makedirs(newPath) -def createNewFolder(destinationRoot, year, eventNumber): - yearPath = os.path.join(destinationRoot, year) - if not os.path.exists(yearPath): - os.mkdir(yearPath) - eventPath = os.path.join(yearPath, str(eventNumber)) - if not os.path.exists(eventPath): - os.mkdir(eventPath) +# Pass None for month to create 'year/eventNumber' directories instead of 'year/month/eventNumber'. +def createNewFolder(destinationRoot, year, month, eventNumber): + if month is not None: + newPath = os.path.join(destinationRoot, year, month, str(eventNumber)) + else: + newPath = os.path.join(destinationRoot, year, str(eventNumber)) + + createPath(newPath) def createUnknownDateFolder(destinationRoot): path = os.path.join(destinationRoot, unknownDateFolderName) - if not os.path.exists(path): - os.mkdir(path) + createPath(path) - -def writeImages(images, destinationRoot): +def writeImages(images, destinationRoot, splitByMonth=False): sortedImages = sorted(images) previousTime = None eventNumber = 0 + previousDestination = None today = strftime("%d/%m/%Y") for imageTuple in sortedImages: @@ -77,6 +81,7 @@ def writeImages(images, destinationRoot): destinationFilePath = "" t = localtime(imageTuple[0]) year = strftime("%Y", t) + month = splitByMonth and strftime("%m", t) or None creationDate = strftime("%d/%m/%Y", t) fileName = ntpath.basename(imageTuple[1]) @@ -87,18 +92,22 @@ def writeImages(images, destinationRoot): else: if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]): - previousTime = imageTuple[0] eventNumber = eventNumber + 1 - createNewFolder(destinationRoot, year, eventNumber) - + createNewFolder(destinationRoot, year, month, eventNumber) + previousTime = imageTuple[0] - destination = os.path.join(destinationRoot, year, str(eventNumber)) - # it may be possible that an event covers 2 years. - # in such a case put all the images to the even in the old year - if not (os.path.exists(destination)): - destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber)) + destComponents = [destinationRoot, year, month, str(eventNumber)] + destComponents = [v for v in destComponents if v is not None] + destination = os.path.join(*destComponents) + # 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) if not (os.path.exists(destinationFilePath)): @@ -108,10 +117,10 @@ def writeImages(images, destinationRoot): os.remove(imageTuple[1]) -def postprocessImages(imageDirectory): +def postprocessImages(imageDirectory, splitByMonth): images = [] for root, dirs, files in os.walk(imageDirectory): for file in files: postprocessImage(images, imageDirectory, file) - writeImages(images, imageDirectory) + writeImages(images, imageDirectory, splitByMonth) diff --git a/recovery.py b/recovery.py index 5064964..2f2944a 100644 --- a/recovery.py +++ b/recovery.py @@ -91,7 +91,7 @@ for root, dirs, files in os.walk(source, topdown=False): log(str(fileCounter) + " / " + totalAmountToCopy + " processed.") 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") numberOfFilesPerFolderLimiter.limitFilesPerFolder(destination, maxNumberOfFilesPerFolder)