#!/usr/bin/env python from MythTV import MythDB, Video, MythVideo, MythBE, MythError, MythLog, RecordedProgram, VideoGrabber from MythTV.database import DBData import platform import os import pyinotify import sys db = MythDB() mvid = MythVideo() # 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) 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_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) 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.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