diff --git a/Sonarr/delete_files.py b/Sonarr/delete_files.py index d21dc24..b0bbbc9 100644 --- a/Sonarr/delete_files.py +++ b/Sonarr/delete_files.py @@ -14,6 +14,20 @@ BasePath = "" SourcePath = "" SourceFolder = "" + +def checkSeasonFolder(SourceFolder): + count = 0 + for file in glob.glob(SourceFolder + "/*"): + if file.endswith(".mkv"): + logging.info(f"Found mkv file {file} in the folder: {SourceFolder}") + count += 1 + if count > 1: + logging.info("Identified a Season folder") + return True + else: + logging.info("This is not a Season folder") + return False + def sendNotifications(title, message, priority): resp = requests.post('https://gotify.rouggy.com/message?token=AKMj5SsZblmpAJ_', json={ "message": message, @@ -42,12 +56,14 @@ if BasePath not in PATH_LIST: os.remove(SourcePath) logging.debug(f"Removing file {SourcePath}") for file in glob.glob(SourceFolder + "/*"): - os.remove(file) - logging.debug(f"Removing file {file} from {SourceFolder}") - os.rmdir(SourceFolder) - logging.debug(f"Deleting folder {SourceFolder}") + if not file.endswith(".mkv"): + os.remove(file) + logging.debug(f"Removing file {file} from {SourceFolder}") + if not checkSeasonFolder(SourceFolder): + os.rmdir(SourceFolder) + logging.debug(f"Deleting folder {SourceFolder}") except: - pass + logging.warning("Error while trying to delete the files") else: try: os.remove(SourcePath)