Guest User

Untitled

a guest
May 24th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. ##############################################################################
  4. ### NZBGET POST-PROCESSING SCRIPT ###
  5.  
  6. # Flatten all downloaded files into the root download directory.
  7. #
  8. # This removes all of the sub-folders created by the unpack process.
  9. # This should run before other scripts.
  10. #
  11. # NOTE: This script requires Python to be installed on your system.
  12. ##############################################################################
  13. ### OPTIONS ###
  14.  
  15. # Destination Directory.
  16. #
  17. # Set the directory where you want all files to be moved to.
  18. # Use this if you want all downloaded files in a single "root" directory.
  19. # If left blank, files will all be "flattened" into the individual download's sub-directory.
  20. #DestinationDirectory=
  21.  
  22. # Append Categories (yes, no).
  23. #
  24. # If using the Destination Directory above, then this option will append the download category.
  25. #AppendCategories=no
  26.  
  27.  
  28. ### NZBGET POST-PROCESSING SCRIPT ###
  29. ##############################################################################
  30.  
  31. import os
  32. import sys
  33. import shutil
  34.  
  35. # NZBGet Exit Codes
  36. NZBGET_POSTPROCESS_PARCHECK = 92
  37. NZBGET_POSTPROCESS_SUCCESS = 93
  38. NZBGET_POSTPROCESS_ERROR = 94
  39. NZBGET_POSTPROCESS_NONE = 95
  40.  
  41. # Only run script if download was successful
  42. if os.environ.get('NZBPP_TOTALSTATUS') != 'SUCCESS':
  43. print("[INFO] Skipping script because download was not successful.")
  44. sys.exit(NZBGET_POSTPROCESS_NONE)
  45.  
  46.  
  47. def removeEmptyFolders(path, removeRoot=True):
  48. if not os.path.isdir(path):
  49. return
  50.  
  51. # Remove empty subfolders
  52. for f in os.listdir(path):
  53. fullpath = os.path.join(path, f)
  54. if os.path.isdir(fullpath):
  55. removeEmptyFolders(fullpath)
  56.  
  57. # If folder empty, delete it
  58. if not os.listdir(path) and removeRoot:
  59. print(f"[INFO] Removing empty folder: {path}")
  60. os.rmdir(path)
  61.  
  62. directory = os.path.normpath(os.environ.get('NZBPP_DIRECTORY'))
  63. destination = (
  64. os.path.normpath(os.environ['NZBPO_DESTINATIONDIRECTORY'])
  65. if os.environ.get('NZBPO_DESTINATIONDIRECTORY', False) and os.path.isdir(os.environ['NZBPO_DESTINATIONDIRECTORY'])
  66. else directory
  67. )
  68.  
  69. # Add category to destination if specified
  70. if os.environ.get('NZBPO_APPENDCATEGORIES') == 'yes':
  71. destination = os.path.join(destination, os.environ.get('NZBPP_CATEGORY'))
  72.  
  73. print(f"Flattening directory: {directory}")
  74.  
  75. for dirpath, dirnames, filenames in os.walk(directory):
  76. for fileName in filenames:
  77. outputFile = os.path.join(dirpath, fileName)
  78.  
  79. # Check if it's a file and skip if it's actually a directory
  80. if not os.path.isfile(outputFile):
  81. continue # Skip directories
  82.  
  83. # Skip if already in the destination directory
  84. if dirpath == destination:
  85. continue
  86.  
  87. # Check if a file or folder with the same name already exists in the destination
  88. target = os.path.join(destination, fileName)
  89. if os.path.exists(target):
  90. base, extension = os.path.splitext(fileName)
  91. counter = 1
  92. # Rename with incremental suffix if a file or folder with the name exists
  93. while os.path.exists(target):
  94. target = os.path.join(destination, f"{base}_{counter}{extension}")
  95. counter += 1
  96. try:
  97. shutil.move(outputFile, target)
  98. print(f"[INFO] Moved {outputFile} to {target}")
  99. except Exception as e:
  100. print(f"[ERROR] Could not flatten {outputFile}. Error: {str(e)}")
  101.  
  102. removeEmptyFolders(directory) # Cleanup empty directories
  103. sys.exit(NZBGET_POSTPROCESS_SUCCESS)
  104.  
Advertisement
Add Comment
Please, Sign In to add comment