forked from P-arag/folder-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (118 loc) · 4.94 KB
/
main.py
File metadata and controls
142 lines (118 loc) · 4.94 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
DIR = "/home/pallav/Downloads/"
class OnMyWatch:
watchDirectory = DIR
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.watchDirectory, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print(" Observer Stopped")
self.observer.join()
def check_if_already_there(file_path, dest_dir):
dest_dir = "/home/pallav/"+dest_dir
fileNum = 1
fileName = file_path.split("/")[-1]
print(dest_dir+"/"+fileName)
if os.path.exists(dest_dir+"/"+fileName):
while True:
if os.path.exists(f"{dest_dir}/{fileName}({fileNum})"):
fileNum += 1
print("Exists")
else:
fileNameArr = fileName.split(".")
fileNameWithoutExten = fileNameArr[0]
fileExten = fileNameArr[1]
return f"{fileNameWithoutExten}{fileNum}.{fileExten}"
else:
return fileName
def commands(file_path, where):
command_copy = "cp " + file_path + " " + "/home/pallav/" + where
command_delete = "rm " + file_path
return [command_copy, command_delete]
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
print("Watchdog received created event - % s." % event.src_path)
if event.src_path.endswith(("jpg", "jpeg", "png", "gif")):
print("Image Detected")
time.sleep(2)
fileName = check_if_already_there(event.src_path, "Pictures")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Pictures")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("docx", "txt", "pdf")):
print("Document Detected")
time.sleep(2)
fileName = check_if_already_there(event.src_path, "Documents")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Documents")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("mp3", "wav")):
print("Audio Detected")
time.sleep(2)
fileName = check_if_already_there(event.src_path, "Music")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Music")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("mp4", "avi", "mpv", "ogg")):
print("Video Detected")
time.sleep(2)
fileName = check_if_already_there(event.src_path, "Videos")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Videos")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("py", "json", "js", "c", "cs", "cpp", "java", "go")):
print("Code Detected")
time.sleep(2)
fileName = check_if_already_there(event.src_path, "Code")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Code")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("zip")):
print("Folders Detected")
time.sleep(100)
fileName = check_if_already_there(event.src_path, "Public")
print(fileName)
os.rename(event.src_path, DIR+fileName)
myCommands = commands(DIR+fileName, "Public")
os.system(myCommands[0])
os.system(myCommands[1])
elif event.src_path.endswith(("tar", "gz", "tar.gz", "deb")):
print("Large Files Detected, Not Doing anything")
else:
print("Unknown Files Detected")
print(fileName)
os.rename(event.src_path, DIR+fileName)
time.sleep(20)
fileName = check_if_already_there(event.src_path, "Etc")
myCommands = commands(DIR+fileName, "Etc")
os.system(myCommands[0])
os.system(myCommands[1])
if __name__ == '__main__':
watch = OnMyWatch()
print(f"Watchdog watching {DIR}")
watch.run()