roderickvd

RenameMedia

Dec 24th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. ##############################################################################
  4. ### NZBGET POST-PROCESSING SCRIPT                                          ###
  5.  
  6. # Rename media files for better Sick Beard and CouchPotato Server processing.
  7. #
  8. # By default scans for season and episode numbering for series and ttid for
  9. # movies. If none is found, the largest media file is renamed to NZB name. Set
  10. # ForceRename to rename regardless. This can be used to deal with scene
  11. # exceptions.
  12.  
  13. ##############################################################################
  14. ### OPTIONS                                                                ###
  15.  
  16. # Directory allowed to rename files in.
  17. #AllowedDir=/volume1/download/dst
  18.  
  19. # Category label for TV shows.
  20. #Category.TV=tv
  21.  
  22. # Category label for movies.
  23. #Category.Movies=movies
  24.  
  25. # Force renaming to NZB name (yes, no).
  26. #ForceRename=no
  27.  
  28. ### NZBGET POST-PROCESSING SCRIPT                                          ###
  29. ##############################################################################
  30.  
  31. # Tested on Synology DSM 4.3-3810 Update 3 (BusyBox v1.16.1)
  32.  
  33. # INSTALLATION INSTRUCTIONS:
  34. #
  35. #  1. Place script in ppscripts directory,
  36. #       e.g. /usr/local/ppscripts/RenameMedia
  37. #
  38. #  2. Make script executable:
  39. #       chmod a+x /usr/local/ppscripts/RenameMedia
  40. #
  41. #  3. Restart NZBGet
  42. #
  43. #  4. NZBGet Settings > RenameMedia: set AllowedDir to DestDir (for safety),
  44. #       e.g. /volume1/download/dst
  45. #
  46. #  5. NZBGet Settings > RenameMedia: set Category.TV and Category.Movies labels,
  47. #       e.g. Category.TV=tv
  48. #            Category.Movies=movies
  49. #
  50. #  6. NZBGet Settings > Post-Processing Scripts: move RenameMedia after DeleteSamples.py,
  51. #       e.g. DeleteSamples.py, RenameMedia, nzbToMedia*.py, Email.py, Logger.py
  52. #
  53. #  7. NZBGet Settings > Categories: set RenameMedia as DefScript for tv and movie categories,
  54. #       e.g. RenameMedia, nzbToMedia/nzbToSickBeard.py
  55. #
  56. #  NZBGet will now execute RenameMedia for tv and movie NZBs added from this point onwards.
  57. #  Manually change the PP-Parameters for any tv NZBs that were already queued.
  58.  
  59. # CHANGELOG:
  60. #
  61. #  2014/01/01
  62. #   * [FIX] Extensions with a digit (e.g. mp4) were not handled correctly
  63. #   * [ADD] Support all extensions regardless of length
  64. #   * [CHANGE] Minor refactoring
  65. #
  66. #  2013/12/24
  67. #   * [ADD] Rename movies with missing tt numbers
  68. #   * [ADD] TVCategory and MoviesCategory settings
  69. #   * [ADD] Support all three-lettered file extensions
  70. #   * [ADD] Support force renaming (useful for scene exceptions)
  71. #   * [CHANGE] AllowedDir must now be a full path
  72. #   * [CHANGE] Rename the largest media file, not all media files in random order
  73. #   * [CHANGE] Minor regexp tweaks
  74. #   * [CHANGE] Renamed to RenameMedia
  75. #
  76. #  2013/12/08
  77. #   * [FIX] Typo prevented AllowedDir to work with non-default settings
  78. #   * [ADD] Strip year from NZB name
  79. #   * [CHANGE] AllowedDir error message now includes directories for debugging
  80.  
  81. # Defaults -- users should override them in the NZBGet GUI
  82. ALLOWED_DIR=$NZBPO_ALLOWEDDIR
  83. if [ "$ALLOWED_DIR" == "" ]; then
  84.     ALLOWED_DIR="/volume1/download/dst"
  85.     echo "[WARNING] AllowedDir not configured, using default: $ALLOWED_DIR"
  86. fi
  87. TV_CATEGORY=$NZBPO_CATEGORY_TV
  88. if [ "$TV_CATEGORY" == "" ]; then
  89.     TV_CATEGORY="tv"
  90.     echo "[WARNING] Category.TV not configured, using default: $TV_CATEGORY"
  91. fi
  92. MOVIES_CATEGORY=$NZBPO_CATEGORY_MOVIES
  93. if [ "$MOVIES_CATEGORY" == "" ]; then
  94.     MOVIES_CATEGORY="movies"
  95.     echo "[WARNING] Category.Movie not configured, using default: $MOVIES_CATEGORY"
  96. fi
  97. FORCE_RENAME=$NZBPO_FORCERENAME
  98. if [ "$FORCE_RENAME" == "" ]; then
  99.     FORCE_RENAME="no"
  100.     echo "[WARNING] ForceRename not configured, using default: $FORCE_RENAME"
  101. fi
  102.  
  103. # Sanity checks: require NZB post-processing directory and name from NZBGet
  104. NZB_DIRECTORY=$NZBPP_DIRECTORY
  105. if [ "$NZB_DIRECTORY" == "" ]; then
  106.     echo "[ERROR] Empty directory name, skipping."
  107.     exit 95
  108. fi
  109. NZB_NAME=$NZBPP_NZBNAME
  110. if [ "$NZB_NAME" == "" ]; then
  111.     echo "[ERROR] Empty NZB name, skipping."
  112.     exit 95
  113. fi
  114.  
  115. # Sanity check: disallow post-processing directory to be root or the working directory
  116. if [ "$NZB_DIRECTORY" == "." -o "$NZB_DIRECTORY" == "/" ]; then
  117.     echo "[ERROR] Illegal directory name, skipping."
  118.     exit 95
  119. fi
  120.  
  121. # Sanity check: require post-processing directory to contain AllowedDir
  122. if [ `echo "$NZB_DIRECTORY" | grep "^$ALLOWED_DIR" | wc -l` == "0" ]; then
  123.     echo "[ERROR] Directory $NZB_DIRECTORY not in allowed $ALLOWED_DIR, skipping."
  124.     exit 95
  125. fi
  126.  
  127. # Find the largest file, which we assume to be the media file
  128. # NOTE: the regexps are a bit verbose so they work on DSM's BusyBox
  129. du -a "$NZB_DIRECTORY/" | grep -i -e '\.[a-z0-9][a-z0-9]*$' | sort -g | tail -n 1 | sed 's/^[0-9][0-9]*[[:space:]]*//' | while read LARGEST_FILE
  130. do
  131.     EXTENSION=`echo "$LARGEST_FILE" | tail -c 4`
  132.  
  133.     if [ "$NZBPP_CATEGORY" == "$TV_CATEGORY" ]; then
  134.         if [ "$FORCE_RENAME" == "yes" -o `echo "$LARGEST_FILE" | grep -i -e "S[0-9][0-9]*E[0-9][0-9]*.*\.$EXTENSION$" | wc -l` == "0" ]; then
  135.             # Do not force rename complete season downloads
  136.             if [ "$FORCE_RENAME" == "no" -o `find $NZB_DIRECTORY -regex ".*[sS][0-9][0-9]*[eE][0-9][0-9]*.*\.$EXTENSION$" | wc -l` == "1" ]; then
  137.                 # Strip year from NZB name to ensure Sick Beard will recognize the TV show
  138.                 NEW_NAME=`echo $NZB_NAME | sed 's/([0-9][0-9][0-9][0-9])//g' | sed 's/[[:space:]][[:space:]]/ /g' | sed 's/\.\./\./g'`
  139.                 echo "[INFO] Renaming TV show $LARGEST_FILE to $NEW_NAME.$EXTENSION"
  140.                 mv "$LARGEST_FILE" "$NZB_DIRECTORY/$NEW_NAME.$EXTENSION"
  141.                 RESULT=$?
  142.                 if ! [ "$RESULT" == 0 ]; then
  143.                     echo "[ERROR] Error $RESULT"
  144.                     exit 94
  145.                 fi
  146.             fi
  147.         fi
  148.  
  149.     elif [ "$NZBPP_CATEGORY" == "$MOVIES_CATEGORY" ]; then
  150.         if [ "$FORCE_RENAME" == "yes" -o `echo "$LARGEST_FILE" | grep -i -e "tt[0-9][0-9]*.*\.$EXTENSION$" | wc -l` == "0" ]; then
  151.             NEW_NAME=$NZB_NAME
  152.             echo "[INFO] Renaming movie $LARGEST_FILE to $NEW_NAME.$EXTENSION"
  153.             mv "$LARGEST_FILE" "$NZB_DIRECTORY/$NEW_NAME.$EXTENSION"
  154.             RESULT=$?
  155.             if ! [ "$RESULT" == 0 ]; then
  156.                 echo "[ERROR] Error $RESULT"
  157.                 exit 94
  158.             fi
  159.         fi
  160.  
  161.     fi
  162.  
  163. done
  164.  
  165. exit 93
Advertisement
Add Comment
Please, Sign In to add comment