]> git.treefish.org Git - photosort.git/blob - src/photosort.py
use exif time for subfolder
[photosort.git] / src / photosort.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import datetime
5 import logging
6 import os
7
8 import misc
9
10 parser = argparse.ArgumentParser(description='Process some integers.')
11 parser.add_argument('SOURCE_DIR', type=str, help='source directory')
12 parser.add_argument('DEST_DIR', type=str, help='target directory')
13 parser.add_argument('-c', '--cleanup', action='store_true', dest='cleanup',
14                     default=False, help='clean-up source dir')
15 parser.add_argument('-l', '--log-level', type=str, default='INFO', dest='log_lvl',
16                     choices=['DEBUG', 'INFO', 'WARNING'], help='select log level')
17
18 args = parser.parse_args()
19
20 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
21                     level=logging.getLevelName(args.log_lvl),
22                     datefmt='%m/%d/%Y %H:%M:%S')
23
24 for src_file_name, src_file_path in misc.walk_media_files(args.SOURCE_DIR):
25     logging.info('Processing %s...', src_file_name)
26
27     exif_time = misc.extract_timestamp(src_file_path, use_exif=True)
28
29     dst_dir = os.path.join(args.DEST_DIR,
30                            datetime.datetime.fromtimestamp(exif_time).strftime("%Y/%m"))
31     dst_file_path = os.path.join(dst_dir, src_file_name)
32
33     if not os.path.exists(dst_file_path):
34         alt_dst_dir = misc.find_file(args.DEST_DIR,
35                                      src_file_name,
36                                      os.path.getsize(src_file_path),
37                                      exclude_dir=args.SOURCE_DIR)
38         if alt_dst_dir:
39             dst_dir = alt_dst_dir
40             dst_file_path = os.path.join(dst_dir, src_file_name)
41
42     if not os.path.exists(dst_file_path):
43         if not os.path.exists(dst_dir):
44             os.makedirs(dst_dir)
45         misc.import_file(src_file_path, dst_file_path, move=args.cleanup)
46     else:
47         src_time = misc.extract_timestamp(src_file_path)
48         dst_time = misc.extract_timestamp(dst_file_path)
49         if src_time > dst_time:
50             misc.import_file(src_file_path, dst_file_path, move=args.cleanup)
51
52 if args.cleanup:
53     misc.delete_dir_contents(args.SOURCE_DIR)