-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_albums.py
50 lines (37 loc) · 1.75 KB
/
create_albums.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
#!/usr/bin/python
import argparse
import glob
import json
import os
import shutil
import uuid
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Copy media files into folders (one folder per album) according to album descriptions provided in <UUID>.json files")
parser.add_argument('--src-dir', type=str,
help='directory containing imported photos, videos and albums metadata files')
parser.add_argument('--out-dir', type=str,
help='destination directory for albums')
args = parser.parse_args()
out_dir = args.out_dir
src_dir = args.src_dir
for album_file in glob.glob(os.path.join(src_dir, '*.json'), recursive=False):
album_dir_name = os.path.basename(os.path.splitext(album_file)[0])
try:
_tmp = uuid.UUID(album_dir_name)
except ValueError:
continue
with open(album_file, encoding='utf-8') as albumdata_file:
albumdata = json.load(albumdata_file)
if not albumdata['files']:
print("[INFO] Album {0} is empty and therefore will not be created".format(albumdata['title']))
continue
album_dir_path = os.path.join(out_dir, album_dir_name)
os.makedirs(album_dir_path, exist_ok=True)
print('[INFO] Creating album {0}'.format(albumdata['title']))
shutil.copy(album_file, album_dir_path)
for file in albumdata['files']:
media_file = os.path.join(src_dir, file)
if not os.path.exists(media_file):
print("[ERROR] Can't place photo {0} into album. File not found".format(media_file))
continue
shutil.copy(media_file, album_dir_path)