-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganize_downloads.py
executable file
·82 lines (64 loc) · 3.28 KB
/
organize_downloads.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
#!/usr/local/bin/python3
"""
Oranize the donwload folder through moving files in the download folder to more
appropriate folders.
"""
from watchdog.observers import Observer
import time
from watchdog.events import FileSystemEventHandler
import os #operating system dependent functionalities
import json
img_formats = [".png", ".jpg", ".gif", ".HEIC"]
video_formats = ["video", ".mp4", ".MOV"]
class HandleDownloads(FileSystemEventHandler):
#observer.schedule runs on_modified()
def on_modified(self, event):
try:
for filename in os.listdir(folder_to_track):
src= folder_to_track + "/" + filename
if ("U10" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TDDD91" + "/" + filename
os.rename(src, new_destination)
if ("TDAB01" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TDAB01" + "/" + filename
os.rename(src, new_destination)
if ("TFYA87" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TFYA87" + "/" + filename
os.rename(src, new_destination)
if ("TDDC17" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TDDC17" + "/" + filename
os.rename(src, new_destination)
if ("TDDD37" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TDDD37" + "/" + filename
os.rename(src, new_destination)
if ("TDDD92" in filename):
new_destination = "/Users/maximilianvorbrodt/Documents/STUDIER/TDDD92" + "/" + filename
os.rename(src, new_destination)
for i in img_formats:
if i in filename and filename != "Downloaded images":
src = folder_to_track + "/" + filename
new_destination = "/Users/maximilianvorbrodt/Downloads/Downloaded images" + "/" +filename
os.rename(src, new_destination)
for i in video_formats:
if i in filename and filename != "Downloaded videos":
src = folder_to_track + "/" + filename
print ("SRC: ", src)
new_destination = "/Users/maximilianvorbrodt/Downloads/Downloaded videos" + "/" +filename
print ("DEST: ", new_destination)
os.rename(src, new_destination)
except:
pass
#where to look for modified files
folder_to_track = "/Users/maximilianvorbrodt/Downloads" #pwd - "print working directory"
event_handler = HandleDownloads()
observer = Observer()
go_recursively = True #a boolean that allow me to catch all the event that occurs even in the sub directories of my current directory
#Schedules watching a path and calls appropriate methods specified in the given event handler in response to file system events
observer.schedule(event_handler, folder_to_track, recursive = go_recursively)
observer.start()
#wait for modifications to occur
try:
while True:
time.sleep(30)
except KeyboardInterrupt:
observer.stop()