-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmodified.py
96 lines (76 loc) · 3.57 KB
/
unmodified.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import xmltodict
import os
import shutil
from dateutil.parser import parse
from subprocess import call
SOURCE_ROOT = '/Volumes/Pictures/Elein - 2021 01 07/unmodified/'
DESTINATION_ROOT = '/Volumes/Pictures/Pictures/'
PHOTOGRAPHER = 'Elein'
def fixFilename(name):
name = name.replace(' ','\ ')
name = name.replace('(', '\(')
name = name.replace(')', '\)')
return name
def moveAndUpdate(filename, year, month, date, time):
destination = DESTINATION_ROOT + year + '/' + year + ' ' + month + '/' + PHOTOGRAPHER + '/other/'
shutil.copyfile(SOURCE_ROOT + filename, destination + filename)
shutil.move(SOURCE_ROOT + filename, SOURCE_ROOT + 'done/' + filename)
command = 'SetFile -d "' + date + ' ' + time + '" ' + fixFilename(destination + filename)
call(command, shell=True)
command = 'SetFile -m "' + date + ' ' + time + '" ' + fixFilename(destination + filename)
call(command, shell=True)
def processFiles(data):
for file in data:
if 'createDate' in data[file]:
date = data[file]['createDate']['date']
time = data[file]['createDate']['time']
year = data[file]['createDate']['year']
month = data[file]['createDate']['month']
# Create destination directories
if not os.path.exists(DESTINATION_ROOT + year):
os.makedirs(DESTINATION_ROOT + year)
if not os.path.exists(DESTINATION_ROOT + year + '/' + year + ' ' + month):
os.makedirs(DESTINATION_ROOT + year + '/' + year + ' ' + month)
if not os.path.exists(DESTINATION_ROOT + year + '/' + year + ' ' + month + '/' + PHOTOGRAPHER):
os.makedirs(DESTINATION_ROOT + year + '/' + year + ' ' + month + '/' + PHOTOGRAPHER)
if not os.path.exists(DESTINATION_ROOT + year + '/' + year + ' ' + month + '/' + PHOTOGRAPHER + '/other'):
os.makedirs(DESTINATION_ROOT + year + '/' + year + ' ' + month + '/' + PHOTOGRAPHER + '/other')
for filename in data[file]['files']:
print 'updating', filename, date, time
moveAndUpdate(filename, year, month, date, time)
def getCreateDate(data, filename, baseName):
if os.path.exists(SOURCE_ROOT + filename):
f = open(SOURCE_ROOT + filename, 'rb')
else:
print 'error'
return
tags = xmltodict.parse(f.read())
f.close()
parsedDate = parse(tags['x:xmpmeta']['rdf:RDF']['rdf:Description']['photoshop:DateCreated'],ignoretz=False)
date = str(parsedDate.strftime('%m/%d/%Y'))
year = parsedDate.strftime('%Y')
month = parsedDate.strftime('%m')
time = str(parsedDate.time())
data[baseName]['createDate'] = {'date':date, 'time':time, 'year':year, 'month':month}
return data
def main():
dirs = os.listdir(SOURCE_ROOT)
data = {}
for filename in dirs:
baseName, extension = os.path.splitext(filename)
if baseName in data:
if 'files' not in data[baseName]:
data[baseName]['files'] = [filename]
else:
data[baseName]['files'].append(filename)
else:
data[baseName] = {'files':[filename]}
if (extension == '.xmp'):
print 'processing', baseName + extension
data = getCreateDate(data, filename, baseName)
# Create done directory where processed files will be moved to.
if not os.path.exists(SOURCE_ROOT + 'done/'):
os.makedirs(SOURCE_ROOT + 'done/')
processFiles(data)
if __name__ == '__main__':
main()