-
Notifications
You must be signed in to change notification settings - Fork 9
/
FileWatch.py
44 lines (31 loc) · 1.39 KB
/
FileWatch.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
import pyinotify
import os
databaseinfo = os.getenv('dbinfo')
pathtowatch = os.getenv('watchpath')
wm = pyinotify.WatchManager() # Watch Manager
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events
import sqlite3
connection = sqlite3.connect(databaseinfo, timeout=20)
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (id TEXT, filename TEXT, rdstatus TEXT, rdprogressdownload INTEGER, attemptstogetlink INTEGER, rderror TEXT , completed TEXT , Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP )")
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
head, tail = os.path.split(event.pathname)
filename = (tail)
extension = os.path.splitext(filename)[1][1:]
if extension == "torrent":
print("Torrent file detected ",tail)
torrent=(event.pathname)
import subprocess
process = subprocess.Popen(['python', 'RDtorrent.py', torrent])
elif extension == "magnet":
print("Magnet file detected ",tail)
magnetlink = (event.pathname)
import subprocess
process = subprocess.Popen(['python', 'RDmagnet.py', magnetlink])
else:
print("IGNORE Not suitable - " , tail)
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(pathtowatch, mask, rec=True)
notifier.loop()