def __init__(self): self.database = MovieDatabase() self.isWorking = False self.movieConfig = MovieConfig() self.callback = None self.serviceHandler = ServiceCenter.getInstance() #self.full_used_detect = 0 self.enabled = False self.last_update = None
class MovieScanner(): def __init__(self): self.database = MovieDatabase() self.isWorking = False self.movieConfig = MovieConfig() self.callback = None self.serviceHandler = ServiceCenter.getInstance() #self.full_used_detect = 0 self.enabled = False self.last_update = None def setEnabled(self, enabled): self.enabled = enabled print "[AdvancedMovieSelection] Set MovieScanner:", str(enabled) if enabled: recordTimerEvent.appendCallback(self.timerStateChanged) self.addHotplugNotifier() self.reloadMoviesAsync() self.hotplugChanged() else: recordTimerEvent.removeCallback(self.timerStateChanged) self.removeHotplugNotifier() def updateReloadTime(self): self.last_update = datetime.now() def getLastUpdate(self): if self.last_update is not None: return self.last_update.strftime("%d.%m.%Y %H:%M") def isMovieRecorded(self, name): entries = self.database.findMovies(name) for mi in entries: if mi.serviceref.getPath().endswith(".ts"): return 1 if len(entries) > 0: return 2 return 0 def reloadMoviesAsync(self, dir_list=None, delay=0): if self.isWorking: print "[AdvancedMovieSelection] MovieScanner action canceled! reload in progress" return from thread import start_new_thread start_new_thread(self.updateMovieList, (dir_list, delay)) self.isWorking = True def getFullUsed(self): full_used = 0 for p in config.AdvancedMovieSelection.videodirs.value: total, used, free = diskUsage(p) full_used += used return full_used @clockit def updateDirectories(self): config.AdvancedMovieSelection.videodirs.load() new_list = [] for p in config.AdvancedMovieSelection.videodirs.value: getDirectories(new_list, p) return new_list @clockit def updateMovieList(self, dir_list=None, delay=0): print "[AdvancedMovieSelection] Start scanning movies" try: # print dir_list self.isWorking = True if delay > 0: print "waiting", str(delay) import time time.sleep(delay) self.updateReloadTime() if dir_list is None: self.database.clearAll() dir_list = self.updateDirectories() # print "-" * 80 # for p in dir_list: # print p # print "-" * 80 for p in dir_list: self.scanForMovies(p) # self.full_used_detect = self.getFullUsed() if self.callback is not None: self.callback() # from MovieDatabase import dict2xml # xml = dict2xml(self.database) # xml.write("/tmp/movie_list.xml") except: printStackTrace() self.isWorking = False directories, movies = self.database.getFullCount() print "[AdvancedMovieSelection] Finished scanning movies", str(directories), str(movies) def scanForMovies(self, root): # print "[AdvancedMovieSelection] scan folder:", root scan_service = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + root) root_list = self.serviceHandler.list(scan_service) if root_list is None: print "listing of movies failed" return tags = set() l = [] dirs = [] while 1: serviceref = root_list.getNext() if not serviceref.valid(): break dvd = None # print serviceref.getPath() # dvd structure if serviceref.flags & eServiceReference.mustDescent: dvd = detectDVDStructure(serviceref.getPath()) if dvd is not None: if serviceref.getPath()[:-1].endswith(TRASH_NAME): continue serviceref = eServiceReferenceDvd(serviceref, True) bludisc = detectBludiscStructure(serviceref.getPath()) if bludisc is not None: if serviceref.getPath()[:-1].endswith(TRASH_NAME): continue serviceref = eServiceReferenceBludisc(serviceref, True) if not dvd and not bludisc: continue if False: # add folder dir tempDir = serviceref.getPath() parts = tempDir.split(os.sep) dirName = parts[-2] if self.movieConfig.isHidden(dirName): continue serviceref.setName(dirName) dirs.append((serviceref, None, -1, -1)) continue # check hidden files temp = serviceref.getPath() parts = temp.split(os.sep) if self.movieConfig.isHidden(parts[-1]): continue ext = temp.split(".")[-1].lower() if ext in AUDIO_EXCLUDE: continue # check currently moving files if serviceUtil.isServiceMoving(serviceref): continue # check iso and img files extension = serviceref.getPath().split(".")[-1].lower() if extension == "iso" or extension == "img": serviceref = eServiceReferenceDvd(serviceref) info = self.serviceHandler.info(serviceref) # get begin time if dvd is not None: begin = long(os.stat(dvd).st_mtime) else: begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate) # convert space-seperated list of tags into a set this_tags = info.getInfoString(serviceref, iServiceInformation.sTags).split(' ') if this_tags is None or this_tags == ['']: this_tags = [] this_tags = set(this_tags) tags |= this_tags # add to list service_name = info.getName(serviceref) mi = MovieInfo(service_name, serviceref, info, begin) l.append(mi) # we always must add location to database dir_size = getDirSize(root) self.database.addMovieList(root, l, dir_size) self.database.addTags(tags) def findMovies___(self, name): l = [] for mi in self.list: if name == mi[0].name: l.append(mi) return l def findMovie___(self, serviceref): for mi in self.list: if serviceref.getPath() == mi[0].getPath(): return mi[0] def removeMovie(self, file_service_list): self.database.removeMovie(file_service_list) self.updateReloadTime() def timerStateChanged(self, timer): try: from timer import TimerEntry print "timer.event", timer.name print "timer.state", timer.state if timer.state == TimerEntry.StateRunning: print "TimerEntry", timer.name print timer.Filename + ".ts" mi = MovieInfo(timer.name, None, file_name=timer.Filename + ".ts") serviceref = mi.createService() mi.info = self.serviceHandler.info(serviceref) mi.begin = mi.info.getInfo(serviceref, iServiceInformation.sTimeCreate) movie_path = os.path.dirname(mi.serviceref.getPath()) + os.sep self.database.addMovie(movie_path, mi) self.updateReloadTime() print "add:", mi except: printStackTrace() def needFullUpdate(self): videodirs = config.AdvancedMovieSelection.videodirs.value[:] config.AdvancedMovieSelection.videodirs.load() print "checking directories" print videodirs print config.AdvancedMovieSelection.videodirs.value if len(videodirs) < len(config.AdvancedMovieSelection.videodirs.value): print "size changed" return True if len(videodirs) == len(config.AdvancedMovieSelection.videodirs.value) and videodirs != config.AdvancedMovieSelection.videodirs.value: print "path changed" return True return False @clockit def checkAllAvailable(self): # print "*" * 80 if self.isWorking: print "canceled, scan in progress" return if self.needFullUpdate(): print "need update" not_in_db = self.database.getMissingLocations(config.AdvancedMovieSelection.videodirs.value) new_list = [] for p in not_in_db: getDirectories(new_list, p) self.reloadMoviesAsync(new_list) # remove locations from database if path not exists for location in self.database.getDirectoryList(): if not os.path.exists(location): self.updateReloadTime() self.database.removeLocation(location) # print "*" * 80 def updateServiceInfo(self, serviceref): if not serviceref: return if serviceref.flags & eServiceReference.mustDescent: return print "update service info", serviceref.toString() movie_info = self.database.findMoviePath(serviceref) if movie_info is not None: movie_info.info = self.serviceHandler.info(serviceref) movie_info.name = movie_info.info.getName(serviceref) print movie_info def addHotplugNotifier(self): if not self.checkAllAvailable in hotplug.notifier: print "add hotplugNotifier" hotplug.notifier.append(self.checkAllAvailable) def removeHotplugNotifier(self): if self.checkAllAvailable in hotplug.notifier: print "remove hotplugNotifier" hotplug.notifier.remove(self.checkAllAvailable)
class MovieScanner(): def __init__(self): self.database = MovieDatabase() self.isWorking = False self.movieConfig = MovieConfig() self.callback = None self.serviceHandler = ServiceCenter.getInstance() #self.full_used_detect = 0 self.enabled = False self.last_update = None def setEnabled(self, enabled): self.enabled = enabled print "[AdvancedMovieSelection] Set MovieScanner:", str(enabled) if enabled: recordTimerEvent.appendCallback(self.timerStateChanged) self.addHotplugNotifier() self.reloadMoviesAsync() self.hotplugChanged() else: recordTimerEvent.removeCallback(self.timerStateChanged) self.removeHotplugNotifier() def updateReloadTime(self): self.last_update = datetime.now() def getLastUpdate(self): if self.last_update is not None: return self.last_update.strftime("%d.%m.%Y %H:%M") def isMovieRecorded(self, name): entries = self.database.findMovies(name) for mi in entries: if mi.serviceref.getPath().endswith(".ts"): return 1 if len(entries) > 0: return 2 return 0 def reloadMoviesAsync(self, dir_list=None, delay=0): if self.isWorking: print "[AdvancedMovieSelection] MovieScanner action canceled! reload in progress" return from thread import start_new_thread start_new_thread(self.updateMovieList, (dir_list, delay)) self.isWorking = True def getFullUsed(self): full_used = 0 for p in config.AdvancedMovieSelection.videodirs.value: total, used, free = diskUsage(p) full_used += used return full_used @clockit def updateDirectories(self): config.AdvancedMovieSelection.videodirs.load() new_list = [] for p in config.AdvancedMovieSelection.videodirs.value: getDirectories(new_list, p) return new_list @clockit def updateMovieList(self, dir_list=None, delay=0): print "[AdvancedMovieSelection] Start scanning movies" try: # print dir_list self.isWorking = True if delay > 0: print "waiting", str(delay) import time time.sleep(delay) self.updateReloadTime() if dir_list is None: self.database.clearAll() dir_list = self.updateDirectories() # print "-" * 80 # for p in dir_list: # print p # print "-" * 80 for p in dir_list: self.scanForMovies(p) # self.full_used_detect = self.getFullUsed() if self.callback is not None: self.callback() # from MovieDatabase import dict2xml # xml = dict2xml(self.database) # xml.write("/tmp/movie_list.xml") except: printStackTrace() self.isWorking = False directories, movies = self.database.getFullCount() print "[AdvancedMovieSelection] Finished scanning movies", str( directories), str(movies) def scanForMovies(self, root): # print "[AdvancedMovieSelection] scan folder:", root scan_service = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + root) root_list = self.serviceHandler.list(scan_service) if root_list is None: print "listing of movies failed" return tags = set() l = [] dirs = [] while 1: serviceref = root_list.getNext() if not serviceref.valid(): break dvd = None # print serviceref.getPath() # dvd structure if serviceref.flags & eServiceReference.mustDescent: dvd = detectDVDStructure(serviceref.getPath()) if dvd is not None: if serviceref.getPath()[:-1].endswith(TRASH_NAME): continue serviceref = eServiceReferenceDvd(serviceref, True) bludisc = detectBludiscStructure(serviceref.getPath()) if bludisc is not None: if serviceref.getPath()[:-1].endswith(TRASH_NAME): continue serviceref = eServiceReferenceBludisc(serviceref, True) if not dvd and not bludisc: continue if False: # add folder dir tempDir = serviceref.getPath() parts = tempDir.split(os.sep) dirName = parts[-2] if self.movieConfig.isHidden(dirName): continue serviceref.setName(dirName) dirs.append((serviceref, None, -1, -1)) continue # check hidden files temp = serviceref.getPath() parts = temp.split(os.sep) if self.movieConfig.isHidden(parts[-1]): continue ext = temp.split(".")[-1].lower() if ext in AUDIO_EXCLUDE: continue # check currently moving files if serviceUtil.isServiceMoving(serviceref): continue # check iso and img files extension = serviceref.getPath().split(".")[-1].lower() if extension == "iso" or extension == "img": serviceref = eServiceReferenceDvd(serviceref) info = self.serviceHandler.info(serviceref) # get begin time if dvd is not None: begin = long(os.stat(dvd).st_mtime) else: begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate) # convert space-seperated list of tags into a set this_tags = info.getInfoString( serviceref, iServiceInformation.sTags).split(' ') if this_tags is None or this_tags == ['']: this_tags = [] this_tags = set(this_tags) tags |= this_tags # add to list service_name = info.getName(serviceref) mi = MovieInfo(service_name, serviceref, info, begin) l.append(mi) # we always must add location to database dir_size = getDirSize(root) self.database.addMovieList(root, l, dir_size) self.database.addTags(tags) def findMovies___(self, name): l = [] for mi in self.list: if name == mi[0].name: l.append(mi) return l def findMovie___(self, serviceref): for mi in self.list: if serviceref.getPath() == mi[0].getPath(): return mi[0] def removeMovie(self, file_service_list): self.database.removeMovie(file_service_list) self.updateReloadTime() def timerStateChanged(self, timer): try: from timer import TimerEntry print "timer.event", timer.name print "timer.state", timer.state if timer.state == TimerEntry.StateRunning: print "TimerEntry", timer.name print timer.Filename + ".ts" mi = MovieInfo(timer.name, None, file_name=timer.Filename + ".ts") serviceref = mi.createService() mi.info = self.serviceHandler.info(serviceref) mi.begin = mi.info.getInfo(serviceref, iServiceInformation.sTimeCreate) movie_path = os.path.dirname(mi.serviceref.getPath()) + os.sep self.database.addMovie(movie_path, mi) self.updateReloadTime() print "add:", mi except: printStackTrace() def needFullUpdate(self): videodirs = config.AdvancedMovieSelection.videodirs.value[:] config.AdvancedMovieSelection.videodirs.load() print "checking directories" print videodirs print config.AdvancedMovieSelection.videodirs.value if len(videodirs) < len(config.AdvancedMovieSelection.videodirs.value): print "size changed" return True if len(videodirs) == len( config.AdvancedMovieSelection.videodirs.value ) and videodirs != config.AdvancedMovieSelection.videodirs.value: print "path changed" return True return False @clockit def checkAllAvailable(self): # print "*" * 80 if self.isWorking: print "canceled, scan in progress" return if self.needFullUpdate(): print "need update" not_in_db = self.database.getMissingLocations( config.AdvancedMovieSelection.videodirs.value) new_list = [] for p in not_in_db: getDirectories(new_list, p) self.reloadMoviesAsync(new_list) # remove locations from database if path not exists for location in self.database.getDirectoryList(): if not os.path.exists(location): self.updateReloadTime() self.database.removeLocation(location) # print "*" * 80 def updateServiceInfo(self, serviceref): if not serviceref: return if serviceref.flags & eServiceReference.mustDescent: return print "update service info", serviceref.toString() movie_info = self.database.findMoviePath(serviceref) if movie_info is not None: movie_info.info = self.serviceHandler.info(serviceref) movie_info.name = movie_info.info.getName(serviceref) print movie_info def addHotplugNotifier(self): if not self.checkAllAvailable in hotplug.notifier: print "add hotplugNotifier" hotplug.notifier.append(self.checkAllAvailable) def removeHotplugNotifier(self): if self.checkAllAvailable in hotplug.notifier: print "remove hotplugNotifier" hotplug.notifier.remove(self.checkAllAvailable)
def select_movie(database: MovieDatabase, conn: psycopg2.connect): return database.select_movies(conn)
def insert_movie(database: MovieDatabase, movie: Movie, conn: psycopg2.connect): database.upload_movie(movie, conn)