-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulate_directory.py
40 lines (30 loc) · 1.52 KB
/
manipulate_directory.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
import sys
from pathlib import Path
from datetime import datetime
from manipulate_images import get_gps_location, errors
def save_by_date(image_path, image_data, target):
"""Saves photo according to the date taken."""
date = str(datetime.strptime(image_data['datetime_original'], '%Y:%m:%d %H:%M:%S').date())
year = str(datetime.strptime(image_data['datetime_original'], '%Y:%m:%d %H:%M:%S').year)
year_dir = target / year
if not year_dir.exists():
year_dir.mkdir(parents=True)
target_path = year_dir / (date + '_' + image_path.stem)
if target_path.exists():
errors['same_files'].append(image_path.stem)
else:
image_path.rename(target_path)
def save_by_location(image_path, image_data, target):
"""Saves photo according to the location."""
location = get_gps_location(image_data['gps_latitude'], image_data['gps_latitude_ref'], image_data['gps_longitude'], image_data['gps_longitude_ref'])
date = str(datetime.strptime(image_data['datetime_original'], '%Y:%m:%d %H:%M:%S').date())
year = str(datetime.strptime(image_data['datetime_original'], '%Y:%m:%d %H:%M:%S').year)
location_dir = target / (location['country'] + '-' + location['city'])
year_dir = location_dir / year
if not location_dir.exists() or not year_dir.exists():
year_dir.mkdir(parents=True)
target_path = year_dir / (date + '_' + image_path.stem)
if target_path.exists():
errors['same_files'].append(image_path.stem)
else:
image_path.rename(target_path)