forked from ivandokov/phockup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phockup.py
executable file
·295 lines (253 loc) · 7.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
import argparse
import logging
import logging.handlers
import os
import re
import sys
from src.date import Date
from src.dependency import check_dependencies
from src.phockup import Phockup
__version__ = '1.7.0'
PROGRAM_DESCRIPTION = """\
Media sorting tool to organize photos and videos from your camera in folders by year, \
month and day.
The software will collect all files from the input directory and copy them to the output
directory without changing the files content. It will only rename the files and place
them in the proper directory for year, month and day.
"""
DEFAULT_DIR_FORMAT = ['%Y', '%m', '%d']
logger = logging.getLogger('phockup')
def parse_args(args=sys.argv[1:]):
parser = argparse.ArgumentParser(
description=PROGRAM_DESCRIPTION,
formatter_class=argparse.RawTextHelpFormatter)
parser.version = f"v{__version__}"
parser.add_argument(
'-v',
'--version',
action='version',
)
parser.add_argument(
'-d',
'--date',
action='store',
type=Date().parse,
help="""\
Specify date format for OUTPUTDIR directories.
You can choose different year format (e.g. 17 instead of 2017) or decide to skip the
day directories and have all photos sorted in year/month.
Supported formats:
YYYY - 2016, 2017 ...
YY - 16, 17 ...
MM - 07, 08, 09 ...
M - July, August, September ...
m - Jul, Aug, Sept ...
DD - 27, 28, 29 ... (day of month)
DDD - 123, 158, 365 ... (day of year)
Example:
YYYY/MM/DD -> 2011/07/17
YYYY/M/DD -> 2011/July/17
YYYY/m/DD -> 2011/Jul/17
YY/m-DD -> 11/Jul-17
""",
)
exclusive_group_link_move = parser.add_mutually_exclusive_group()
exclusive_group_link_move.add_argument(
'-m',
'--move',
action='store_true',
help="""\
Instead of copying the process will move all files from the INPUTDIR to the OUTPUTDIR.
This is useful when working with a big collection of files and the remaining free space
is not enough to make a copy of the INPUTDIR.
""",
)
exclusive_group_link_move.add_argument(
'-l',
'--link',
action='store_true',
help="""\
Instead of copying the process will make hard links to all files in INPUTDIR and place
them in the OUTPUTDIR.
This is useful when working with working structure and want to create YYYY/MM/DD
structure to point to same files.
""",
)
parser.add_argument(
'-o',
'--original-names',
action='store_true',
help="""\
Organize the files in selected format or using the default year/month/day format but
keep original filenames.
""",
)
parser.add_argument(
'-t',
'--timestamp',
action='store_true',
help="""\
Use the timestamp of the file (last modified date) if there is no EXIF date information.
If the user supplies a regex, it will be used if it finds a match in the filename.
This option is intended as "last resort" since the file modified date may not be
accurate, nevertheless it can be useful if no other date information can be obtained.
""",
)
parser.add_argument(
'-y',
'--dry-run',
action='store_true',
help="""\
Does a trial run with no permanent changes to the filesystem.
So it will not move any files, just shows which changes would be done.
""",
)
parser.add_argument(
'--maxdepth',
type=int,
default=-1,
choices=range(0, 255),
metavar='1-255',
help="""\
Descend at most 'maxdepth' levels (a non-negative integer) of directories
""",
)
parser.add_argument(
'-r',
'--regex',
action='store',
type=re.compile,
help="""\
Specify date format for date extraction from filenames if there is no EXIF date
information.
Example:
{regex}
can be used to extract the date from file names like the following
IMG_27.01.2015-19.20.00.jpg.
""",
)
parser.add_argument(
'-f',
'--date-field',
action='store',
help="""\
Use a custom date extracted from the exif field specified.
To set multiple fields to try in order until finding a valid date, use spaces to
separate fields inside a string.
Example:
DateTimeOriginal
"DateTimeOriginal CreateDate FileModifyDate"
These fields are checked by default when this argument is not set:
"SubSecCreateDate SubSecDateTimeOriginal CreateDate DateTimeOriginal"
To get all date fields available for a file, do:
exiftool -time:all -mimetype -j <file>
""",
)
exclusive_group_debug_silent = parser.add_mutually_exclusive_group()
exclusive_group_debug_silent.add_argument(
'--debug',
action='store_true',
default=False,
help="""\
Enable debugging.
""",
)
exclusive_group_debug_silent.add_argument(
'--quiet',
action='store_true',
default=False,
help="""\
Run without output.
""",
)
exclusive_group_debug_silent.add_argument(
'--progress',
action='store_true',
default=False,
help="""\
Run with progressbar output.
""",
)
parser.add_argument(
'--log',
action='store',
help="""\
Specify the output directory where your log file should be exported.
This flag can be used in conjunction with the flag `--quiet` or `--progress`.
""",
)
parser.add_argument(
'input_dir',
metavar='INPUTDIR',
help="""\
Specify the source directory where your photos are located.
""",
)
parser.add_argument(
'output_dir',
metavar='OUTPUTDIR',
help="""\
Specify the output directory where your photos should be exported.
""",
)
parser.add_argument(
'--file-type',
type=str,
choices=['image', 'video'],
metavar='image|video',
help="""\
By default, Phockup addresses both image and video files.
If you want to restrict your command to either images or
videos only, use `--file-type=[image|video]`.
""",
)
return parser.parse_args(args)
def setup_logging(options):
"""Configure logging."""
root = logging.getLogger('')
root.setLevel(logging.WARNING)
formatter = logging.Formatter(
'[%(asctime)s] - [%(levelname)s] - %(message)s', '%Y-%m-%d %H:%M:%S')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
root.addHandler(ch)
if not options.quiet ^ options.progress:
logger.setLevel(options.debug and logging.DEBUG or logging.INFO)
else:
logger.setLevel(logging.WARNING)
if options.log:
logfile = os.path.expanduser(options.log)
fh = logging.FileHandler(logfile)
fh.setFormatter(formatter)
logger.addHandler(fh)
def main(options):
check_dependencies()
return Phockup(
options.input_dir,
options.output_dir,
dir_format=options.date,
move=options.move,
link=options.link,
date_regex=options.regex,
original_filenames=options.original_names,
timestamp=options.timestamp,
date_field=options.date_field,
dry_run=options.dry_run,
quiet=options.quiet,
progress=options.progress,
max_depth=options.maxdepth,
file_type=options.file_type,
)
if __name__ == '__main__':
try:
options = parse_args()
setup_logging(options)
main(options)
except Exception as e:
logger.warning(e)
sys.exit(1)
except KeyboardInterrupt:
logger.error("Exiting phockup...")
sys.exit(1)
sys.exit(0)