-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiphoneMediaMon.py
113 lines (89 loc) · 2.05 KB
/
iphoneMediaMon.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
from datetime import datetime
import os.path
import sys
from subprocess import call
import signal
import time
import pyinotify
import classifyImg
classifyImg=classifyImg.classifyImg
db=tobedefined
log_file = open("%s/var/log/iphoneMediaMon.log" %db, "w")
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/photo/Iphone"]
allowed_exts = {
"jpg",
"jpeg",
"png",
"tga",
"gif",
"bmp",
"mp3",
"flac",
"aac",
"wma",
"ogg",
"ogv",
"mp4",
"avi",
"m4v",
"mkv",
"mov",
}
wm = pyinotify.WatchManager()
mask = (
pyinotify.IN_CLOSE_WRITE
)
class EventHandler(pyinotify.ProcessEvent):
def __init__(self):
pass
def process_IN_CLOSE_WRITE(self, event):
self.process_create(event)
# def process_IN_CREATE(self, event):
# self.process_create(event)
def process_create(self, event):
if event.maskname == 'IN_CREATE':
return
arg = ''
if event.dir:
pass
else:
self.do_classify_command(event)
def do_classify_command(self, event):
if self.is_allowed_path(event.pathname, event.dir):
log("classify %s %s" % (event.maskname, event.pathname))
time.sleep(1)
if os.path.exists(event.pathname) and os.path.getsize(event.pathname) > 0:
log(classifyImg(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,
exclude_filter=lambda p: '/@' in p
)
try:
notifier.loop(daemonize=True, pid_file='/var/services/homes/afawaz/var/run/iphoneMediaMon.pid')
except pyinotify.NotifierError as err:
log(str(err))