-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
97 lines (94 loc) · 3.27 KB
/
monitor.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
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
root='/home/ubuntu/Googledrive/videos' ###################### directory in which videos are present
import subprocess
import os
from plexconvert import *
from time import sleep
a=[os.path.join(path, name) for path, subdirs, files in os.walk(root) for name in files]
if len(a)==0:
a=root
import re
#This step makes PLEX to recognize the movie names correctly
regex="www.+?- "
for i in a: #renaming files (it removes website names from the file name )
if len(re.findall(regex,i.lower()))>0:
index=re.search(regex,i.lower()).span()
print(re.search(regex,i.lower()))
uu=i[:index[0]]+i[index[1]:]
print(uu)
os.system(f'mv "{i}" "{uu}"')
print("Done renaming files...")
def rename_file(i):
index=re.search(regex,i.lower()).span()
print(re.search(regex,i.lower()))
uu=i[:index[0]]+i[index[1]:]
print(uu)
os.system(f'mv "{i}" "{uu}"')
scan_all_library()
#optimize_video()
class MyHandler(FileSystemEventHandler):
def on_any_event(self, event):
#scan(s=False)
#print('on any event')
#rint(event.event_type, event.src_path)
#scan()
#sleep(20)
pass
def on_created(self, event):
print('created')
print("on_created", event.src_path)
if "/home/ubuntu/Googledrive/videos/downloaded videos/Plex Versions/" in event.src_path:
print('skip checking as it is converting video in plex')
return
try:
#sleep(60*10)
rename_file(event.src_path)
#os.system('bash /home/ubuntu/jellyfin_convert.sh')
scan_all_library()
try:
optimize_video()
except Exception as error:
print(error)
scan_all_library()
try:
optimize_video()
except Exception as error:
print(error)
except Exception as error:
print(error)
def on_deleted(self, event):
#print('deleted')
#scan(s=False)
#print("on_deleted", event.src_path)
pass
def on_modified(self, event):
#print('modified')
#scan(s=False)
#print("on_modified", event.src_path)
pass
def on_moved(self, event):
#scan(s=False)
#print('moved')
#print("on_moved", event.src_path)
pass
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s')
#path = sys.argv[1] if len(sys.argv) > 1 else '.'
path='/home/ubuntu/Googledrive/videos/'
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True) #Scheduling monitoring of a path with the observer instance and event handler. There is 'recursive=True' because only with it enabled, watchdog.observers.Observer can monitor sub-directories
observer.start() #for starting the observer thread
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()