forked from carljm/synology-mediamon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediamon.py
132 lines (104 loc) · 3 KB
/
mediamon.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
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
# /root/mediamon.py
from datetime import datetime
import os.path
import sys
from subprocess import call
import signal
import pyinotify
log_file = open("/var/log/mediamon.log", "a")
def log(text):
dt = datetime.utcnow().isoformat()
log_file.write(dt + ' - ' + text + "\n")
log_file.flush()
def signal_handler(signal, frame):
log("Exiting")
sys.exit(0)
log("Starting")
signal.signal(signal.SIGTERM, signal_handler)
watched_paths = ["/volume1/music", "/volume1/photo", "/volume1/video"]
allowed_exts = {
"jpg",
"jpeg",
"png",
"tga",
"gif",
"bmp",
"mp3",
"flac",
"aac",
"wma",
"ogg",
"ogv",
"mp4",
"avi",
"m4v",
}
wm = pyinotify.WatchManager()
mask = (
pyinotify.IN_MODIFY |
pyinotify.IN_CLOSE_WRITE |
pyinotify.IN_DELETE |
pyinotify.IN_CREATE |
pyinotify.IN_MOVED_TO |
pyinotify.IN_MOVED_FROM
)
class EventHandler(pyinotify.ProcessEvent):
def __init__(self):
self.modified_files = set()
def process_IN_CREATE(self, event):
self.process_create(event)
def process_IN_MOVED_TO(self, event):
self.process_create(event)
def process_IN_MOVED_FROM(self, event):
self.process_delete(event)
def process_IN_DELETE(self, event):
self.process_delete(event)
def process_create(self, event):
arg = ''
if event.dir:
arg = "-A"
else:
arg = "-a"
self.do_index_command(event, arg)
def process_delete(self, event):
arg = ''
if event.dir:
arg = "-D"
else:
arg = "-d"
self.do_index_command(event, arg)
def process_IN_MODIFY(self, event):
if self.is_allowed_path(event.pathname, event.dir):
self.modified_files.add(event.pathname)
def process_IN_CLOSE_WRITE(self, event):
# ignore close_write unlesss the file has previously been modified.
if (event.pathname in self.modified_files):
self.do_index_command(event, "-a")
def do_index_command(self, event, index_argument):
if self.is_allowed_path(event.pathname, event.dir):
log("synoindex %s %s" % (index_argument, event.pathname))
call(["synoindex", index_argument, event.pathname])
self.modified_files.discard(event.pathname)
else:
log("%s is not an allowed path" % event.pathname)
def is_allowed_path(self, filename, is_dir):
# Don't check the extension for directories
if not is_dir:
ext = os.path.splitext(filename)[1][1:].lower()
if ext not in allowed_exts:
return False
if filename.find("@eaDir") > 0:
return False
return True
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(
watched_paths,
mask,
rec=True,
auto_add=True,
)
try:
notifier.loop(daemonize=True, pid_file='/var/run/mediamon.pid')
except pyinotify.NotifierError, err:
print >> sys.stderr, err