Propmt when input parameter missing, progress output
This commit is contained in:
parent
2b454cb2da
commit
e7c91f843e
124
recovery.py
124
recovery.py
|
@ -1,99 +1,59 @@
|
||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
|
||||||
import sys
|
import sys
|
||||||
|
import jpgSorter
|
||||||
import time
|
import time
|
||||||
import exifread
|
import shutil
|
||||||
|
from time import gmtime, strftime
|
||||||
source = sys.argv[1]
|
|
||||||
destination = sys.argv[2]
|
|
||||||
|
|
||||||
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
|
|
||||||
|
|
||||||
|
|
||||||
def getMinimumCreationTime(exif_data):
|
def getFolderSizeInGb(start_path = '.'):
|
||||||
creationTime = None
|
total_size = 0
|
||||||
dateTime = exif_data.get('DateTime')
|
for dirpath, dirnames, filenames in os.walk(start_path):
|
||||||
dateTimeOriginal = exif_data.get('EXIF DateTimeOriginal')
|
for f in filenames:
|
||||||
dateTimeDigitized = exif_data.get('EXIF DateTimeDigitized')
|
fp = os.path.join(dirpath, f)
|
||||||
|
total_size += os.path.getsize(fp)
|
||||||
if dateTime is not None :
|
return int(total_size / 1024 / 1024 / 1024)
|
||||||
creationTime = dateTime
|
|
||||||
if dateTimeOriginal is not None and (creationTime == None or dateTimeOriginal < creationTime):
|
|
||||||
creationTime = dateTimeOriginal
|
|
||||||
if dateTimeDigitized is not None and (creationTime == None or dateTimeDigitized < creationTime):
|
|
||||||
creationTime = dateTimeDigitized
|
|
||||||
return creationTime
|
|
||||||
|
|
||||||
def postprocessImage(images, sourceDir, imageDirectory, fileName):
|
|
||||||
imagePath = os.path.join(sourceDir, fileName)
|
|
||||||
image = open(imagePath, 'rb')
|
|
||||||
exifTags = exifread.process_file(image)
|
|
||||||
creationTime = getMinimumCreationTime(exifTags)
|
|
||||||
|
|
||||||
# distinct different time types
|
|
||||||
if creationTime is None:
|
|
||||||
creationTime = time.gmtime(os.path.getctime(imagePath))
|
|
||||||
else:
|
|
||||||
creationTime = time.strptime(str(creationTime), "%Y:%m:%d %H:%M:%S")
|
|
||||||
|
|
||||||
print time.asctime(creationTime)
|
|
||||||
|
|
||||||
images.append((time.mktime(creationTime), imagePath))
|
|
||||||
|
|
||||||
|
|
||||||
def createNewFolder(destinationRoot, year, eventNumber):
|
source = None
|
||||||
yearPath = os.path.join(destinationRoot, year)
|
destination = None
|
||||||
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)
|
|
||||||
|
|
||||||
|
if(len(sys.argv) < 3):
|
||||||
|
print("Enter source and destination: python sort.py source/path destination/path")
|
||||||
|
else:
|
||||||
|
source = sys.argv[1]
|
||||||
|
print("Source directory: " + source)
|
||||||
|
destination = sys.argv[2]
|
||||||
|
print("Destination directory: " + destination)
|
||||||
|
|
||||||
def writeImages(images, destinationRoot):
|
while ((source is None) or (not os.path.exists(source))):
|
||||||
sortedImages = sorted(images)
|
source = input('Enter a valid source directory\n')
|
||||||
previousTime = None
|
while ((destination is None) or (not os.path.exists(destination))):
|
||||||
eventNumber = 0
|
destination = input('Enter a valid destination directory\n')
|
||||||
|
|
||||||
for imageTuple in sortedImages:
|
|
||||||
t = time.gmtime(imageTuple[0])
|
|
||||||
year = time.strftime("%Y", t)
|
|
||||||
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
|
|
||||||
previousTime = imageTuple[0]
|
|
||||||
eventNumber = eventNumber + 1
|
|
||||||
createNewFolder(destinationRoot, year, eventNumber)
|
|
||||||
|
|
||||||
previousTime = imageTuple[0]
|
|
||||||
|
|
||||||
destination = os.path.join(destinationRoot, year, str(eventNumber))
|
|
||||||
shutil.copy(imageTuple[1], destination)
|
|
||||||
|
|
||||||
|
totalAmountToCopy = str(getFolderSizeInGb(source))
|
||||||
|
print("Files to copy: " + totalAmountToCopy + " GB.")
|
||||||
|
|
||||||
images = []
|
images = []
|
||||||
while not os.path.exists(source):
|
|
||||||
source = raw_input('Enter a valid source directory\n')
|
|
||||||
while not os.path.exists(destination):
|
|
||||||
destination = raw_input('Enter a valid destination directory\n')
|
|
||||||
|
|
||||||
for root, dirs, files in os.walk(source, topdown=False):
|
for root, dirs, files in os.walk(source, topdown=False):
|
||||||
for file in files:
|
print (strftime("%H:%M:%S", gmtime()) + ": " + str(getFolderSizeInGb(destination)) + " / " + totalAmountToCopy + " GB processed.")
|
||||||
extension = os.path.splitext(file)[1][1:].upper()
|
for file in files:
|
||||||
path = os.path.join(root,file)
|
extension = os.path.splitext(file)[1][1:].upper()
|
||||||
|
path = os.path.join(root,file)
|
||||||
destinationPath = os.path.join(destination, extension)
|
|
||||||
|
destinationPath = os.path.join(destination, extension)
|
||||||
|
|
||||||
if not os.path.exists(destinationPath):
|
if not os.path.exists(destinationPath):
|
||||||
os.mkdir(destinationPath)
|
os.mkdir(destinationPath)
|
||||||
|
|
||||||
if extension == "JPG":
|
if extension == "JPG":
|
||||||
postprocessImage(root, destinationPath, file)
|
jpgSorter.postprocessImage(images, root, destinationPath, file)
|
||||||
else:
|
else:
|
||||||
if os.path.exists(os.path.join(destinationPath,file)):
|
if os.path.exists(os.path.join(destinationPath, file)):
|
||||||
print("WARNING: this file was not copied :" + os.path.join(root,file))
|
shutil.copy(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
|
||||||
shutil.copy(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
|
else:
|
||||||
else:
|
shutil.copy(os.path.join(root,file), destinationPath)
|
||||||
shutil.copy(os.path.join(root,file), destinationPath)
|
|
||||||
|
|
||||||
writeImages(os.path.join(destination, "JPG"))
|
jpgSorter.writeImages(images, os.path.join(destination, "JPG"))
|
||||||
|
|
Loading…
Reference in New Issue