-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwatch_for_changes.py
75 lines (65 loc) · 2.03 KB
/
watch_for_changes.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
import time
import sys
import subprocess
import boto3
import os
import json
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from multiprocessing import Process, Queue
session = boto3.Session()
credentials = session.get_credentials()
current_credentials = credentials.get_frozen_credentials()
accessKey = current_credentials.access_key
secretKey = current_credentials.secret_key
q = Queue()
with open('config.json') as json_data_file:
data = json.load(json_data_file)
region = data['region']
kinesisVideoStreamName = data['kinesisVideoStreamName']
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.mkv", "*.mp4"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
if event.event_type == 'modified':
# print 'modified: ' + event.src_path
# put into the queue
q.put(event.src_path)
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
def loop_send(q):
try:
while True:
if not q.empty():
# print "upload file: " + q.get()
fileName = q.get()
if os.path.isfile(fileName):
subprocess.Popen(['./putMkvMedia.sh', accessKey, secretKey, region, kinesisVideoStreamName, fileName])
except KeyboardInterrupt:
return
if __name__ == '__main__':
args = sys.argv[1:]
outDirectory = './outputStream/'
if not os.path.exists(outDirectory):
os.makedirs(outDirectory)
observer = Observer()
observer.schedule(MyHandler(), path=args[0] if args else outDirectory)
observer.start()
p1 = Process(target=loop_send, args=(q,))
p1.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
p1.join()
observer.join()