diff --git a/__pycache__/Loader.cpython-310.pyc b/__pycache__/Loader.cpython-310.pyc new file mode 100644 index 0000000..90bd0ff Binary files /dev/null and b/__pycache__/Loader.cpython-310.pyc differ diff --git a/main.py b/main.py index 0b68455..5750e38 100644 --- a/main.py +++ b/main.py @@ -33,40 +33,20 @@ class Loader: logging.debug(f"Found {len(mp4_files)} .mp4 files in {root_folder_name}") yield root_folder_name, mp4_files for dir in self.__find_empty_folders(): - relative_path = os.path.relpath(dir, self.directory) - logging.debug(f"Found no .mp4 files in {relative_path}") - yield relative_path, [] - for dir in self.__find_folders_without_mp4(): - relative_path = os.path.relpath(dir, self.directory) - logging.debug(f"Found no .mp4 files in {relative_path}") - yield relative_path, [] + logging.debug(f"Found no .mp4 files in {dir}") + yield dir, [] def __find_empty_folders(self): - """Finds and returns a list of empty folders in the given directory.""" - empty_folders = [] + """Yield folder names that do not contain any mp4 files in a given directory.""" + for folder in os.listdir(self.directory): + folder_path = os.path.join(self.directory, folder) - for root, dirs, files in os.walk(self.directory): - if not files and not dirs: # If the folder contains no files or subdirectories - empty_folders.append(root) + if os.path.isdir(folder_path): # Ensure it's a directory + has_mp4 = any(file.endswith(".mp4") for file in os.listdir(folder_path)) - return empty_folders - def __find_folders_without_mp4(self): - """ - Finds folders that are either empty or contain no .mp4 files. + if not has_mp4: + yield folder # Yield the folder name if no mp4 files found - :param root_dir: The directory to search within. - :return: List of folders that are empty or have no .mp4 files. - """ - result_folders = [] - - for foldername, subfolders, filenames in os.walk(self.directory): - # Check if the folder is empty or has no MP4 files - folder_name = os.path.relpath(foldername, self.directory) - if not any(file.lower().endswith('.mp4') for file in filenames): - if (folder_name != "."): - result_folders.append(foldername) - - return result_folders def __remove_year(self, input_string: str): cleaned_string = re.sub(r'\(\d{4}\)', '', input_string).strip() logging.debug(f"Removed year from '{input_string}' -> '{cleaned_string}'") @@ -95,11 +75,36 @@ class Loader: season = match.group(1) episode = match.group(2) logging.debug(f"Extracted season {season}, episode {episode} from '{filename}'") - return season, episode + return int(season), int(episode) else: logging.error(f"Failed to find season/episode pattern in '{filename}'") raise MatchNotFoundError("Season and episode pattern not found in the filename.") + def __GetEpisodesAndSeasons(self, mp4_files: []): + episodes_dict = {} + + for file in mp4_files: + season, episode = self.__GetEpisodeAndSeason(file) + + if season in episodes_dict: + episodes_dict[season].append(episode) + else: + episodes_dict[season] = [episode] + + return episodes_dict + + def __GetMissingEpisodesAndSeason(self, key: str, mp4_files: []): + expected_dict = get_season_episode_count(key) # key season , value count of episodes + filedict = self.__GetEpisodesAndSeasons(mp4_files) + + for season, expected_count in expected_dict.items(): + existing_episodes = filedict.get(season, []) + missing_episodes = [ep for ep in range(1, expected_count + 1) if ep not in existing_episodes] + + if missing_episodes: + yield season, missing_episodes + + def LoadMissing(self): logging.info("Starting process to load missing episodes") result = self.__find_mp4_files()