forked from ivandokov/phockup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phockup.py
executable file
·263 lines (201 loc) · 7.13 KB
/
phockup.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
import getopt
import hashlib
import os
import shutil
import subprocess
import sys
import re
from datetime import datetime
from subprocess import check_output, CalledProcessError
version = '1.2.1'
def main(argv):
check_dependencies()
if len(argv) != 2:
help_info()
inputdir = argv[0]
outputdir = argv[1]
if not os.path.isdir(inputdir) or not os.path.exists(inputdir):
error('Input directory "%s" does not exist' % inputdir)
if not os.path.exists(outputdir):
print('Output directory "%s" does not exist, creating now' % outputdir)
os.makedirs(outputdir)
ignored_files = ('.DS_Store', 'Thumbs.db')
for root, _, files in os.walk(inputdir):
for filename in files:
try:
if filename in ignored_files:
continue
handle_file(os.path.join(root, filename), outputdir)
except KeyboardInterrupt:
print(' Exiting...')
sys.exit(0)
def check_dependencies():
if shutil.which('exiftool') is None:
print('Exiftool is not installed. Visit http://www.sno.phy.queensu.ca/~phil/exiftool/')
sys.exit(2)
def exif(file):
try:
data = check_output(['exiftool', file]).decode('UTF-8').strip().split("\\n")[0].split("\n")
exif_data = {}
except (CalledProcessError, UnicodeDecodeError):
return None
for row in data:
opt = row.split(":")
exif_data[opt[0].strip()] = ":".join(opt[1:]).strip()
return exif_data
def get_date(file, exif_data):
keys = ['Create Date', 'Date/Time Original']
datestr = None
for key in keys:
if key in exif_data:
datestr = exif_data[key]
break
if datestr:
datestr = datestr.split('.')
date = datestr[0]
if len(datestr) > 1:
subseconds = datestr[1]
else:
subseconds = ''
search = r'(.*)([+-]\d{2}:\d{2})'
if re.search(search, date) is not None:
date = re.sub(search, r'\1', date)
try:
parsed_date_time = datetime.strptime(date, "%Y:%m:%d %H:%M:%S")
except ValueError:
try:
parsed_date_time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
except ValueError:
parsed_date_time = None
return {
'date': parsed_date_time,
'subseconds': subseconds
}
else:
# If missing datetime from exif data check if filename is in datetime format
# E.g.: IMG_20160915_123456.jpg
regex = re.compile('.*[_-](\d{8})[_-]?(\d{6})')
matches = regex.findall(os.path.basename(file))
if matches:
try:
datetimestr = ' '.join(list(matches[0]))
date = datetime.strptime(datetimestr, "%Y%m%d %H%M%S")
except ValueError:
date = None
if date:
return {
'date': date,
'subseconds': ''
}
def get_output_dir(date, outputdir):
if outputdir.endswith(os.path.sep):
outputdir = outputdir[:-1]
try:
path = [
outputdir,
'%04d' % date['date'].year,
'%02d' % date['date'].month,
'%02d' % date['date'].day,
]
except:
path = [
outputdir,
'unknown',
]
fullpath = os.path.sep.join(path)
if not os.path.isdir(fullpath):
os.makedirs(fullpath)
return fullpath
def get_file_name(file, date):
try:
filename = [
'%04d' % date['date'].year,
'%02d' % date['date'].month,
'%02d' % date['date'].day,
'-',
'%02d' % date['date'].hour,
'%02d' % date['date'].minute,
'%02d' % date['date'].second,
]
if date['subseconds']:
filename.append(date['subseconds'])
return ''.join(filename) + os.path.splitext(file)[1]
except:
return os.path.basename(file)
def is_image_or_video(exif_data):
pattern = re.compile('^(image/.+|video/.+|application/vnd.adobe.photoshop)$')
if pattern.match(exif_data['MIME Type']):
return True
return False
def handle_file(source_file, outputdir):
if str.endswith(source_file, '.xmp'):
return None
print(source_file, end="", flush=True)
exif_data = exif(source_file)
if exif_data and is_image_or_video(exif_data):
date = get_date(source_file, exif_data)
output_dir = get_output_dir(date, outputdir)
target_file_name = get_file_name(source_file, date).lower()
target_file_path = os.path.sep.join([output_dir, target_file_name])
else:
output_dir = get_output_dir(False, outputdir)
target_file_name = os.path.basename(source_file)
target_file_path = os.path.sep.join([output_dir, target_file_name])
suffix = 1
target_file = target_file_path
while True:
if os.path.isfile(target_file):
if sha256_checksum(source_file) == sha256_checksum(target_file):
print(' => skipped, duplicated file')
break
else:
shutil.copy2(source_file, target_file)
print(' => %s' % target_file)
handle_file_xmp(source_file, target_file_name, suffix, output_dir)
break
suffix += 1
target_split = os.path.splitext(target_file_path)
target_file = "%s-%d%s" % (target_split[0], suffix, target_split[1])
def handle_file_xmp(source_file, photo_name, suffix, exif_output_dir):
xmp_original_with_ext = source_file + '.xmp'
xmp_original_without_ext = os.path.splitext(source_file)[0] + '.xmp'
suffix = '-%s' % suffix if suffix > 1 else ''
if os.path.isfile(xmp_original_with_ext):
xmp_original = xmp_original_with_ext
xmp_target = '%s%s.xmp' % (photo_name, suffix)
elif os.path.isfile(xmp_original_without_ext):
xmp_original = xmp_original_without_ext
xmp_target = '%s%s.xmp' % (os.path.splitext(photo_name)[0], suffix)
else:
xmp_original = None
xmp_target = None
if xmp_original:
xmp_path = os.path.sep.join([exif_output_dir, xmp_target])
print('%s => %s' % (xmp_original, xmp_path))
shutil.copy2(xmp_original, xmp_path)
def sha256_checksum(filename, block_size=65536):
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def error(message):
print(message)
sys.exit(2)
def help_info():
error("""NAME
phockup - v{version}
SYNOPSIS
phockup INPUTDIR OUTPUTDIR
DESCRIPTION
Phockup is a photos and videos sorting and backup tool written in Python 3.
It organizes the media from your camera in a meaningful hierarchy and with proper file names.
ARGUMENTS
INPUTDIR
Specify the source directory where your photos are located
OUTPUTDIR
Specify the output directory where your photos should be exported
""".format(version=version))
if __name__ == '__main__':
main(sys.argv[1:])