-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdirectory_watcher.py
68 lines (51 loc) · 1.75 KB
/
directory_watcher.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
import time, smtplib
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
from email.mime.text import MIMEText
def send_mail(title, body):
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login('[email protected]', 'password')
msg = MIMEText(body)
msg['Subject'] = title
smtp.sendmail('[email protected]', '[email protected]', msg.as_string())
smtp.quit()
class Handler(RegexMatchingEventHandler):
def __init__(self):
super(Handler, self).__init__(ignore_regexes=[
'^[.]{1}.*', '.*/[.]{1}.*', '.*~\$.*', '.*\.tmp.*'], ignore_directories=False)
def on_closed(self, event):
print(event)
def file_type(self, event):
title = '공유 폴더에 파일이'
if event.is_directory:
title = '공유 폴더가 '
result = '%s 변경(삭제/추가) 되었습니다.' % title
return result
def on_modified(self, event):
mail_title = self.file_type(event)
mail_body = event.src_path
send_mail(mail_title, mail_body)
def on_created(self, event):
mail_title = self.file_type(event)
mail_body = event.src_path
send_mail(mail_title, mail_body)
class Watcher:
DIRECTORY_TO_WATCH = 'z:\\'
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(1)
except:
self.observer.stop()
print("Error")
self.observer.join()
if __name__ == '__main__':
w = Watcher()
w.run()