-
Notifications
You must be signed in to change notification settings - Fork 23
/
blackhole_watcher.py
48 lines (36 loc) · 1.46 KB
/
blackhole_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
import asyncio
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from blackhole import on_created, getPath
class BlackholeHandler(FileSystemEventHandler):
def __init__(self, is_radarr):
super().__init__()
self.is_radarr = is_radarr
self.path_name = getPath(is_radarr, create=True)
def on_created(self, event):
if not event.is_directory and event.src_path.lower().endswith((".torrent", ".magnet")):
asyncio.run(on_created(self.is_radarr))
async def on_run(self):
await on_created(self.is_radarr)
async def main():
print("Watching blackhole")
radarr_handler = BlackholeHandler(is_radarr=True)
sonarr_handler = BlackholeHandler(is_radarr=False)
radarr_observer = Observer()
radarr_observer.schedule(radarr_handler, radarr_handler.path_name)
sonarr_observer = Observer()
sonarr_observer.schedule(sonarr_handler, sonarr_handler.path_name)
try:
radarr_observer.start()
sonarr_observer.start()
await asyncio.gather(
radarr_handler.on_run(),
sonarr_handler.on_run()
)
except KeyboardInterrupt:
radarr_observer.stop()
sonarr_observer.stop()
radarr_observer.join()
sonarr_observer.join()
if __name__ == "__main__":
asyncio.run(main())