def clean(config): LOG.setLevel(logging.getLevelName(config.log_level)) try: if config.update and is_plex_running(): raise PlexCleanerException('Should not update database if Plex is running', severity=logging.ERROR) with database.Database(metadata_home=config.plex_home, database_override=config.database_override) as db: if config.database_backup: backup_database(db.filename) library = Library(db) if not len(library): raise PlexCleanerException('Library is empty', severity=logging.WARNING) if library.has_missing_file and config.interrupt: raise PlexCleanerException('Missing media file on the filesystem', severity=logging.WARNING) if config.export: LOG.info("Will consolidate library in: '{0}'".format(config.export)) has_permission([config.export]) space = get_free_fs_space(config.export) if library.effective_size > space: raise PlexCleanerException('Remaining space on the target filesystem is not enough to export the ' "library {0} Bytes > {1} Bytes".format(library.effective_size, space), severity=logging.CRITICAL) else: has_permission(library.library_paths) for movie in library: LOG.info(u"Processing: '{0}'".format(movie.basename)) if movie.matched: new_path = movie.get_correct_absolute_path(override=config.export) create_dir(new_path) jacket = os.path.join(new_path, config.jacket) copy_jacket(movie.get_metadata_jacket(metadata_home=config.plex_home), jacket, config.skip_jacket) # TODO: Copy SRT to library moved = move_media(movie.original_file, movie.get_correct_absolute_file(override=config.export), config.interrupt) if not moved: LOG.info("{0} was not moved to {1}".format(movie.correct_title, new_path)) elif config.update and movie.need_update(override=config.export): update_database(db, movie) else: LOG.info(u"Movie '{0}' was not matched in Plex".format(movie.basename)) except PlexCleanerException: LOG.warning('PlexCleaner did not process media library.') sys.exit(1) except KeyboardInterrupt: LOG.info('bye') sys.exit(0)
def _update_library(self, movie): if int(movie.count) > 1: LOG.warning("Movie {0} has duplicate file. Will not process.".format(movie.original_file)) return False self.library.append(movie) if movie.library_path not in self.library_paths: self.library_paths.append(movie.library_path) if movie.exist and movie.matched: # Movie might be in the database but it might be absent in the filesystem self.effective_size += movie.size if not movie.exist: self.has_missing_file = True LOG.warning("The file {0} is missing from the library".format(movie.original_file))
def clean(config): LOG.setLevel(logging.getLevelName(config.log_level)) try: if config.update and is_plex_running(): raise PlexCleanerException( 'Should not update database if Plex is running', severity=logging.ERROR) with database.Database( metadata_home=config.plex_home, database_override=config.database_override) as db: if config.database_backup: backup_database(db.filename) library = Library(db) if not len(library): raise PlexCleanerException('Library is empty', severity=logging.WARNING) if library.has_missing_file and config.interrupt: raise PlexCleanerException( 'Missing media file on the filesystem', severity=logging.WARNING) if config.export: LOG.info("Will consolidate library in: '{0}'".format( config.export)) has_permission([config.export]) space = get_free_fs_space(config.export) if library.effective_size > space: raise PlexCleanerException( 'Remaining space on the target filesystem is not enough to export the ' "library {0} Bytes > {1} Bytes".format( library.effective_size, space), severity=logging.CRITICAL) else: has_permission(library.library_paths) for movie in library: LOG.info(u"Processing: '{0}'".format(movie.basename)) if movie.matched: new_path = movie.get_correct_absolute_path( override=config.export) create_dir(new_path) jacket = os.path.join(new_path, config.jacket) copy_jacket( movie.get_metadata_jacket( metadata_home=config.plex_home), jacket, config.skip_jacket) # TODO: Copy SRT to library moved = move_media( movie.original_file, movie.get_correct_absolute_file( override=config.export), config.interrupt) if not moved: LOG.info("{0} was not moved to {1}".format( movie.correct_title, new_path)) elif config.update and movie.need_update( override=config.export): update_database(db, movie) else: LOG.info(u"Movie '{0}' was not matched in Plex".format( movie.basename)) except PlexCleanerException: LOG.warning('PlexCleaner did not process media library.') sys.exit(1) except KeyboardInterrupt: LOG.info('bye') sys.exit(0)