import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
patterns=["*.jpg"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
def on_modified(self, event):
#print event.src_path, event.event_type # print now only for debug
root@ubuntu:/opt/tg/script# more start_monitoring
FOLDER=/mnt/usb/cam1/uploads
python monitor_folder.py $FOLDER
root@ubuntu:/opt/tg/script# more monitor_folder.py
# http://brunorocha.org/python/watching-a-directory-for-file-changes-with-python.html
import os
import sys
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
patterns=["*.jpg"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
def on_modified(self, event):
#print event.src_path, event.event_type # print now only for debug
# /volume1/camera_ftp/cam1/uploads/10.0.0.200_01_20160123205259698_LINE_CROSSING_DETECTION.jpg modified
path = event.src_path
command = "/opt/tg/bin/telegram-cli -k /opt/tg/bin/tg-server.pub -W -e \"send_photo poweruser " + path + "\""
os.system(command)
print command
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
args = sys.argv[1:]
observer = Observer()
observer.schedule(MyHandler(), path=args[0] if args else '.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()