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
|
#!/usr/bin/env python
## -*- coding: utf-8 -*-
#
# «mythvideo-monitor» - Utility to monitor video storage groups and
# automatically add to mythtv database, scan for
# metadata and set group permissions to mythtv with rw
#
# Copyright (C) 2011, Thomas Mashos, for Mythbuntu
#
# Mythbuntu is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this application; if not, write to the Free Software Foundation, Inc., 51
# Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
##################################################################################
from MythTV import MythDB, Video, MythVideo, MythBE, MythError, MythLog, RecordedProgram, VideoGrabber
from MythTV.database import DBData
import platform
import os
import pyinotify
import sys
import grp
db = MythDB()
mvid = MythVideo()
mythtvgrp=grp.getgrnam("mythtv")
# Load TV Grabber
try:
TVgrab = VideoGrabber('TV', db=mvid)
except:
print 'ERROR: Cannot find MythVideo TV grabber'
sys.exit(-1)
# Load Movie Grabber
try:
Mgrab = VideoGrabber('Movie', db=mvid)
except:
print 'ERROR: Cannot find MythVideo Movie grabber'
sys.exit(-1)
## Get hostname
HOSTNAME=platform.node()
## Remove file from db
def del_video(FILENAME):
try:
vid = MythVideo().searchVideos(exactfile=FILENAME).next()
print ' '+format_name(vid)
vid.delete()
except:
print "Could not delete video from DB"
## Add video and gather metadata to db
def add_video(FILENAME):
vid = Video.fromFilename(FILENAME)
print ' '+format_name(vid),
vid.host=HOSTNAME
print vid.getHash()
if vid.subtitle:
matches = TVgrab.sortedSearch(vid.title, vid.subtitle)
else:
matches = Mgrab.sortedSearch(vid.title)
if len(matches) == 0:
print '... no matches, skipped.'
elif len(matches) > 1:
if matches[0].levenshtein > 0:
print '... multiple matches, skipped.'
vid.create()
for k,v in vid.items():
print '{0:>20}: {1}'.format(k,v)
if len(matches) == 1:
vid.importMetadata(matches[0])
wm = pyinotify.WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO | pyinotify.IN_CLOSE_WRITE
class PTmp(pyinotify.ProcessEvent):
def process_IN_DELETE(self, event):
del_video(event.name)
print "Delete: %s " % event.name
def process_IN_MOVED_FROM(self, event):
del_video(event.name)
print "Moved from: %s " % event.name
def process_IN_MOVED_TO(self, event):
os.chmod(os.path.join(event.path, event.name),0664)
os.chown(os.path.join(event.path, event.name), -1, mythtvgrp.gr_gid)
add_video(event.name)
print "Moved to: %s " % event.name
def process_IN_CLOSE_WRITE(self, event):
os.chmod(os.path.join(event.path, event.name),0664)
os.chown(os.path.join(event.path, event.name), -1, mythtvgrp.gr_gid)
add_video(event.name)
print "Write Closed: %s " % event.name
notifier = pyinotify.Notifier(wm, PTmp())
def generate_watch_list():
## Create directory watch list
WATCH_LIST=[]
## Gather all mythvideo storage group directories for this host
VIDEO_SG_LIST=db.getStorageGroup(groupname='Videos',hostname=HOSTNAME)
## Add local mythvideo SG directories to directory watch list
for ITEM in VIDEO_SG_LIST:
WATCH_LIST.append(ITEM.dirname)
for d in WATCH_LIST:
print "Adding directory to watch list:",d
wm.add_watch(d, mask, rec=True)
def format_name(vid):
# returns a string in the format 'TITLE[ - SEASONxEPISODE][ - SUBTITLE]'
s = vid.title
if vid.season:
s += ' - %dx%02d' % (vid.season, vid.episode)
if vid.subtitle:
s += ' - '+vid.subtitle
return s
## Generate the watch list and start watching
generate_watch_list()
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
|