-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchertoucher.py
167 lines (148 loc) · 4.72 KB
/
watchertoucher.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python3 -u
# watchertoucher 0.0.2
# https://github.com/pulpul-s/watchertoucher
import watchdog.events
import watchdog.observers
from watchdog.observers.polling import PollingObserver
import time
import os
from datetime import datetime
## files to watch
filetypes = [
"*.mkv",
"*.mp4",
"*.avi",
"*.m4v",
"*.mov",
"*.ts",
"*.vob",
"*.webm",
"*.mp3",
"*.mp2",
"*.flac",
"*.m4a",
]
## watched folder for changes (this should be the parent directory of your actual media library folders)
folder = "/mediaserver/libraries/"
## libraries in watched folder
libraries = ["video", "audio"]
## dummy filename
touchfile = "watchertoucher.toucher"
## delay in seconds to write new touchfiles, use 0 to disable
touchdelay = 10
## files to ignore, don't remove the touchfile from the list
ignored_files = [touchfile]
## Watch the folder recusively True/False, do not change from True
recur = True
## log changes to a file True/False
logging = False
## logfile location/name
logfile = "/var/log/watchertoucher.log"
def logger(etype, src, dest=None):
now = datetime.now()
pvm = now.strftime("%d.%m.%Y %H:%M:%S")
if etype == "new":
logentry = pvm + " File created " + src
elif etype == "del":
logentry = pvm + " File removed " + src
elif etype == "move":
logentry = pvm + " File moved/renamed " + src + " " + "->" + " " + dest
elif etype == "touch":
logentry = src
if logging == True:
log = open(logfile, "a")
log.write(logentry)
log.close()
else:
print(logentry, end="")
lasttouch = [time.time() - touchdelay, ""]
def toucher(src, dest="", etype=""):
# write and delete a dummy file to the root of the library
global lasttouch
for lib in libraries:
if (
(
etype == "move"
and os.path.dirname(dest).startswith(folder + lib)
and lasttouch[0] + touchdelay <= time.time()
)
or (
etype == "move"
and os.path.dirname(dest).startswith(folder + lib)
and lasttouch[1] != lib
)
or (
os.path.dirname(src).startswith(folder + lib)
and lasttouch[0] + touchdelay <= time.time()
)
or (os.path.dirname(src).startswith(folder + lib) and lasttouch[1] != lib)
):
f = open(folder + lib + "/" + touchfile, "w")
f.close()
os.remove(folder + lib + "/" + touchfile)
logger("touch", " - touched " + folder + lib + "/\n")
lasttouch[0] = time.time()
lasttouch[1] = lib
return
elif (
lasttouch[0] + touchdelay > time.time()
and os.path.dirname(src).startswith(folder + lib)
or (
etype == "move"
and lasttouch[0] + touchdelay > time.time()
and os.path.dirname(dest).startswith(folder + lib)
)
):
logger(
"touch",
f" - nothing touched, touched {lib} under {touchdelay} seconds ago\n",
)
return
logger("touch", " - nothing touched\n")
class Handler(watchdog.events.PatternMatchingEventHandler):
def __init__(self):
super().__init__(
patterns=filetypes,
ignore_patterns=ignored_files,
ignore_directories=False,
case_sensitive=False,
)
def on_created(self, event):
try:
logger("new", event.src_path)
toucher(event.src_path)
except Exception as e:
print("Error in on_created:", str(e))
def on_deleted(self, event):
try:
logger("del", event.src_path)
toucher(event.src_path)
except Exception as e:
print("Error in on_deleted:", str(e))
def on_moved(self, event):
try:
logger("move", event.src_path, event.dest_path)
toucher(event.src_path, event.dest_path, "move")
except Exception as e:
print("Error in on_moved:", str(e))
if __name__ == "__main__":
event_handler = Handler()
observer = PollingObserver()
observer.schedule(event_handler, path=folder, recursive=recur)
observer.start()
try:
liblist = ""
for index, lib in enumerate(libraries):
if index == len(libraries) - 1:
liblist += lib
else:
liblist += lib + ", "
print(f"Watchertoucher 0.0.2 - watching {folder} and touching {liblist}")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Exiting...")
pass
finally:
observer.stop()
observer.join()