-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotos-watch.py
executable file
·118 lines (103 loc) · 4.22 KB
/
photos-watch.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
#!/home/bob/.venv/movephoto/bin/python3
import os
import shutil
import calendar
import time
from PIL import UnidentifiedImageError
from PIL import Image
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
default_watch_dir = "/mnt/c/Users/bob/OneDrive/Pictures/Camera Roll"
robert_watch_dir = "/mnt/c/Users/bob/OneDrive/Pictures/Camera Roll"
# meg_watch_dir = "/mnt/media/nextcloud/meg/files/Photos"
default_destination_dir = "/mnt/c/Users/bob/OneDrive/Camera"
directories = [robert_watch_dir]
observers = []
image_extensions = [".jpg", ".jpeg"]
video_extensions = [".mp4", ".mov"]
banned_extensions = [".png"]
class Watcher:
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
#self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
for directory in directories:
self.observer.schedule(event_handler, directory, recursive=True)
observers.append(self.observer)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'modified':
file_ext = os.path.splitext(event.src_path,)[1]
if file_ext.lower() in image_extensions:
print(f"Copied {event.src_path}")
move_single_photo(event.src_path, default_destination_dir)
elif file_ext.lower() in video_extensions:
print(f"Copied {event.src_path}")
move_single_video(event.src_path, default_destination_dir)
elif file_ext.lower() in banned_extensions:
print(f"Removed {event.src_path}")
os.remove(event.src_path)
def move_single_photo(full_path, destination_dir):
file_name = os.path.basename(full_path)
file_ext = os.path.splitext(file_name)[1]
if file_ext.lower() in image_extensions:
try:
image_data = Image.open(full_path)
except UnidentifiedImageError:
print(f'Cant identify {full_path}')
pass
try:
date_taken = image_data._getexif()[36867]
except KeyError:
pass
except TypeError:
pass
if type(date_taken) is not list:
date_taken = date_taken.split(" ")[0]
date_taken = date_taken.split(":")
year_taken = date_taken[0]
month_taken = date_taken[1]
month_name = calendar.month_name[int(month_taken)]
day_taken = date_taken[2]
full_destination_dir = f'{destination_dir}/{year_taken}/{month_taken} - {month_name}/{year_taken}-{month_taken}-{day_taken}'
full_destination = f'{full_destination_dir}/{file_name}'
if not os.path.exists(full_destination_dir):
os.makedirs(full_destination_dir)
if not os.path.exists(full_destination):
shutil.copy(full_path, full_destination)
def move_single_video(full_path, destination_dir):
file_name = os.path.basename(full_path)
file_ext = os.path.splitext(file_name)[1]
if file_ext.lower() in video_extensions:
date_taken = os.stat(full_path).st_mtime
date_taken = time.strftime('%Y-%m-%d', time.localtime(date_taken))
date_taken = date_taken.split("-")
if type(date_taken) is not list:
date_taken = date_taken.split(" ")[0]
date_taken = date_taken.split(":")
year_taken = date_taken[0]
month_taken = date_taken[1]
month_name = calendar.month_name[int(month_taken)]
day_taken = date_taken[2]
full_destination_dir = f'{destination_dir}/{year_taken}/{month_taken} - {month_name}/{year_taken}-{month_taken}-{day_taken}'
full_destination = f'{full_destination_dir}/{file_name}'
if not os.path.exists(full_destination_dir):
os.makedirs(full_destination_dir)
if not os.path.exists(full_destination):
shutil.copy(full_path, full_destination)
if __name__ == '__main__':
w = Watcher()
w.run()