Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- ##############################################################################
- ### NZBGET POST-PROCESSING SCRIPT ###
- # Flatten all downloaded files into the root download directory.
- #
- # This removes all of the sub-folders created by the unpack process.
- # This should run before other scripts.
- #
- # NOTE: This script requires Python to be installed on your system.
- ##############################################################################
- ### OPTIONS ###
- # Destination Directory.
- #
- # Set the directory where you want all files to be moved to.
- # Use this if you want all downloaded files in a single "root" directory.
- # If left blank, files will all be "flattened" into the individual download's sub-directory.
- #DestinationDirectory=
- # Append Categories (yes, no).
- #
- # If using the Destination Directory above, then this option will append the download category.
- #AppendCategories=no
- ### NZBGET POST-PROCESSING SCRIPT ###
- ##############################################################################
- import os
- import sys
- import shutil
- # NZBGet Exit Codes
- NZBGET_POSTPROCESS_PARCHECK = 92
- NZBGET_POSTPROCESS_SUCCESS = 93
- NZBGET_POSTPROCESS_ERROR = 94
- NZBGET_POSTPROCESS_NONE = 95
- # Only run script if download was successful
- if os.environ.get('NZBPP_TOTALSTATUS') != 'SUCCESS':
- print("[INFO] Skipping script because download was not successful.")
- sys.exit(NZBGET_POSTPROCESS_NONE)
- def removeEmptyFolders(path, removeRoot=True):
- if not os.path.isdir(path):
- return
- # Remove empty subfolders
- for f in os.listdir(path):
- fullpath = os.path.join(path, f)
- if os.path.isdir(fullpath):
- removeEmptyFolders(fullpath)
- # If folder empty, delete it
- if not os.listdir(path) and removeRoot:
- print(f"[INFO] Removing empty folder: {path}")
- os.rmdir(path)
- directory = os.path.normpath(os.environ.get('NZBPP_DIRECTORY'))
- destination = (
- os.path.normpath(os.environ['NZBPO_DESTINATIONDIRECTORY'])
- if os.environ.get('NZBPO_DESTINATIONDIRECTORY', False) and os.path.isdir(os.environ['NZBPO_DESTINATIONDIRECTORY'])
- else directory
- )
- # Add category to destination if specified
- if os.environ.get('NZBPO_APPENDCATEGORIES') == 'yes':
- destination = os.path.join(destination, os.environ.get('NZBPP_CATEGORY'))
- print(f"Flattening directory: {directory}")
- for dirpath, dirnames, filenames in os.walk(directory):
- for fileName in filenames:
- outputFile = os.path.join(dirpath, fileName)
- # Check if it's a file and skip if it's actually a directory
- if not os.path.isfile(outputFile):
- continue # Skip directories
- # Skip if already in the destination directory
- if dirpath == destination:
- continue
- # Check if a file or folder with the same name already exists in the destination
- target = os.path.join(destination, fileName)
- if os.path.exists(target):
- base, extension = os.path.splitext(fileName)
- counter = 1
- # Rename with incremental suffix if a file or folder with the name exists
- while os.path.exists(target):
- target = os.path.join(destination, f"{base}_{counter}{extension}")
- counter += 1
- try:
- shutil.move(outputFile, target)
- print(f"[INFO] Moved {outputFile} to {target}")
- except Exception as e:
- print(f"[ERROR] Could not flatten {outputFile}. Error: {str(e)}")
- removeEmptyFolders(directory) # Cleanup empty directories
- sys.exit(NZBGET_POSTPROCESS_SUCCESS)
Advertisement
Add Comment
Please, Sign In to add comment