rendered paste body#!/usr/bin/env pythonfrom MythTV import MythDB, Video, MythVideo, MythBE, MythError, MythLog, RecordedProgram, VideoGrabberfrom MythTV.database import DBDataimport platformimport osimport pyinotifyimport sysdb = MythDB()mvid = MythVideo()# Load TV Grabbertry: TVgrab = VideoGrabber('TV', db=mvid)except: print 'ERROR: Cannot find MythVideo TV grabber' sys.exit(-1)# Load Movie Grabbertry: Mgrab = VideoGrabber('Movie', db=mvid)except: print 'ERROR: Cannot find MythVideo Movie grabber' sys.exit(-1)## Get hostnameHOSTNAME=platform.node()## Remove file from dbdef 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 dbdef add_video(FILENAME): vid = Video.fromFilename(FILENAME) for I in vid: print I print ' '+format_name(vid), vid.hostname=HOSTNAME 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() print len(matches) 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_WRITEclass 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) 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) add_video(event.name) print "Write Closed: %s " % event.namenotifier = 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 watchinggenerate_watch_list()while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break