Guest User

Untitled

a guest
Nov 28th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #
  3. # This file if part of nzbget
  4. #
  5. # Example postprocessing script for NZBGet
  6. #
  7. # Copyright (C) 2008 Peter Roubos <[email protected]>
  8. # Copyright (C) 2008 Otmar Werner
  9. # Copyright (C) 2008-2012 Andrey Prygunkov <[email protected]>
  10. # Copyright (C) 2012 Antoine Bertin <[email protected]>
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  25. #
  26. #
  27.  
  28. #######################    Usage instructions     #######################
  29. # o  Script will unrar downloaded rar files, join ts-files and rename img-files
  30. #    to iso.
  31. #
  32. # o  To use this script with nzbget set the option "PostProcess" in
  33. #    nzbget configuration file to point to this script file. E.g.:
  34. #        PostProcess=/home/user/nzbget/nzbget-postprocess.sh
  35. #
  36. # o  The script needs a configuration file. An example configuration file
  37. #    is provided in file "postprocess-example.conf". Put the configuration file
  38. #    into the directory where nzbget's configuration file (nzbget.conf) or where
  39. #    this script itself is located. Then edit the configuration file in any
  40. #    text editor to adjust the settings.
  41. #
  42. # o  You can also edit the script's configuration via web-interface (requires
  43. #    NZBGetWeb 1.4 or later). Set the options "PostProcessConfigFile" and
  44. #    "PostProcessConfigTemplate" to point to "postprocess-example.conf"
  45. #    (including full path). The both options are under the section
  46. #    "CONFIGURATION OF POSTPROCESSING-SCRIPT" in NZBGetWeb.
  47. #
  48. # o  There are few options, which can be ajdusted for each nzb-file
  49. #    individually. To view/edit them in web-interface click on a spanner icon
  50. #    near the name of nzb-file.
  51. #
  52. # o  The script supports the feature called "delayed par-check".
  53. #    That means it can try to unpack downloaded files without par-checking
  54. #    them fisrt. Only if unpack fails, the script schedules par-check,
  55. #    then unpacks again.
  56. #    To use delayed par-check set following options in nzbget configuration file:
  57. #        ParCheck=no
  58. #        ParRepair=yes
  59. #        LoadPars=one (or) LoadPars=all
  60. #
  61. # o  If you want to par-check/repair all files before trying to unpack them,
  62. #    set option "ParCheck=yes".
  63. #
  64. ####################### End of Usage instructions #######################
  65.  
  66.  
  67. # NZBGet passes following arguments to postprocess-programm as environment
  68. # variables:
  69. #  NZBPP_DIRECTORY    - path to destination dir for downloaded files;
  70. #  NZBPP_NZBFILENAME  - name of processed nzb-file;
  71. #  NZBPP_PARFILENAME  - name of par-file or empty string (if no collections were
  72. #                       found);
  73. #  NZBPP_PARSTATUS    - result of par-check:
  74. #                       0 = not checked: par-check disabled or nzb-file does
  75. #                           not contain any par-files;
  76. #                       1 = checked and failed to repair;
  77. #                       2 = checked and successfully repaired;
  78. #                       3 = checked and can be repaired but repair is disabled;
  79. #  NZBPP_NZBCOMPLETED - state of nzb-job:
  80. #                       0 = there are more collections in this nzb-file queued;
  81. #                       1 = this was the last collection in nzb-file;
  82. #  NZBPP_PARFAILED    - indication of failed par-jobs for current nzb-file:
  83. #                       0 = no failed par-jobs;
  84. #                       1 = current par-job or any of the previous par-jobs for
  85. #                           the same nzb-files failed;
  86. #  NZBPP_CATEGORY     - category assigned to nzb-file (can be empty string).
  87.  
  88.  
  89. # Name of script's configuration file
  90. SCRIPT_CONFIG_FILE="nzbget-postprocess.conf"
  91.  
  92. # Exit codes
  93. POSTPROCESS_PARCHECK_CURRENT=91
  94. POSTPROCESS_PARCHECK_ALL=92
  95. POSTPROCESS_SUCCESS=93
  96. POSTPROCESS_ERROR=94
  97. POSTPROCESS_NONE=95
  98.  
  99. # Check if the script is called from nzbget
  100. if [ "$NZBPP_DIRECTORY" = "" -o "$NZBOP_CONFIGFILE" = "" ]; then
  101.     echo "*** NZBGet post-process script ***"
  102.     echo "This script is supposed to be called from nzbget (0.7.0 or later)."
  103.     exit $POSTPROCESS_ERROR
  104. fi
  105.  
  106. # Check if postprocessing was disabled in postprocessing parameters
  107. # (for current nzb-file) via web-interface or via command line with
  108. # "nzbget -E G O PostProcess=no <ID>"
  109. if [ "$NZBPR_PostProcess" = "no" ]; then
  110.     echo "[WARNING] Post-Process: Postprocessing disabled for this nzb-file, exiting"
  111.     exit $POSTPROCESS_NONE
  112. fi
  113.  
  114. echo "[INFO] Post-Process: Post-process script successfully started"
  115.  
  116. # Determine the location of configuration file (it must be stored in
  117. # the directory with nzbget.conf or in this script's directory).
  118. ConfigDir="${NZBOP_CONFIGFILE%/*}"
  119. ScriptConfigFile="$ConfigDir/$SCRIPT_CONFIG_FILE"
  120. if [ ! -f "$ScriptConfigFile" ]; then
  121.     ConfigDir="${0%/*}"
  122.     ScriptConfigFile="$ConfigDir/$SCRIPT_CONFIG_FILE"
  123. fi
  124. if [ ! -f "$ScriptConfigFile" ]; then
  125.     echo "[ERROR] Post-Process: Configuration file $ScriptConfigFile not found, exiting"
  126.     exit $POSTPROCESS_ERROR
  127. fi
  128.  
  129. # Readg configuration file
  130. while read line; do eval "$line"; done < $ScriptConfigFile
  131.  
  132. # Check nzbget.conf options
  133. BadConfig=0
  134.  
  135. if [ "$NZBOP_ALLOWREPROCESS" = "yes" ]; then
  136.     echo "[ERROR] Post-Process: Please disable option \"AllowReProcess\" in nzbget configuration file"
  137.     BadConfig=1
  138. fi
  139.  
  140. if [ "$NZBOP_LOADPARS" = "none" ]; then
  141.     echo "[ERROR] Post-Process: Please set option \"LoadPars\" to \"One\" or \"All\" in nzbget configuration file"
  142.     BadConfig=1
  143. fi
  144.  
  145. if [ "$NZBOP_PARREPAIR" = "no" ]; then
  146.     echo "[ERROR] Post-Process: Please set option \"ParRepair\" to \"Yes\" in nzbget configuration file"
  147.     BadConfig=1
  148. fi
  149.  
  150. if [ "$BadConfig" -eq 1 ]; then
  151.     echo "[ERROR] Post-Process: Existing because of not compatible nzbget configuration"
  152.     exit $POSTPROCESS_ERROR
  153. fi
  154.  
  155. # Check if all collections in nzb-file were downloaded
  156. if [ ! "$NZBPP_NZBCOMPLETED" -eq 1 ]; then
  157.     echo "[INFO] Post-Process: Not the last collection in nzb-file, exiting"
  158.     exit $POSTPROCESS_SUCCESS
  159. fi
  160.  
  161. # Check par status
  162. if [ "$NZBPP_PARSTATUS" -eq 1 -o "$NZBPP_PARSTATUS" -eq 3 -o "$NZBPP_PARFAILED" -eq 1 ]; then
  163.     if [ "$NZBPP_PARSTATUS" -eq 3 ]; then
  164.         echo "[WARNING] Post-Process: Par-check successful, but Par-repair disabled, exiting"
  165.     else
  166.         echo "[WARNING] Post-Process: Par-check failed, exiting"
  167.     fi
  168.     exit $POSTPROCESS_ERROR
  169. fi
  170.  
  171. # Check if destination directory exists (important for reprocessing of history items)
  172. if [ ! -d "$NZBPP_DIRECTORY" ]; then
  173.     echo "[ERROR] Post-Process: Nothing to post-process: destination directory $NZBPP_DIRECTORY doesn't exist"
  174.     exit $POSTPROCESS_ERROR
  175. fi
  176.  
  177. cd "$NZBPP_DIRECTORY"
  178.  
  179. # If not just repaired and file "_brokenlog.txt" exists, the collection is damaged
  180. # exiting with returning code $POSTPROCESS_PARCHECK_ALL to request par-repair
  181. if [ ! "$NZBPP_PARSTATUS" -eq 2 ]; then
  182.     if [ -f "_brokenlog.txt" ]; then
  183.         if (ls *.[pP][aA][rR]2 >/dev/null 2>&1); then
  184.             echo "[INFO] Post-Process: Brokenlog found, requesting par-repair"
  185.             exit $POSTPROCESS_PARCHECK_ALL
  186.         fi
  187.     fi
  188. fi
  189.  
  190. # All checks done, now processing the files
  191.  
  192. # Flag indicates that something was unrared
  193. Unrared=0
  194.    
  195. # Unrar the files (if any) to the temporary directory, if there are no rar files this will do nothing
  196. if (ls *.rar >/dev/null 2>&1); then
  197.  
  198.     # Check if unrar exists
  199.     $UnrarCmd >/dev/null 2>&1
  200.     if [ "$?" -eq 127 ]; then
  201.         echo "[ERROR] Post-Process: Unrar not found. Set the path to unrar in script's configuration"
  202.         exit $POSTPROCESS_ERROR
  203.     fi
  204.  
  205.     # Make a temporary directory to store the unrarred files
  206.     ExtractedDirExists=0
  207.     if [ -d $ExtractedDir ]; then
  208.         ExtractedDirExists=1
  209.     else
  210.         mkdir $ExtractedDir
  211.     fi
  212.    
  213.     echo "[INFO] Post-Process: Unraring"
  214.     rarpasswordparam=""
  215.     if [ "$NZBPR_Password" != "" ]; then
  216.         rarpasswordparam="-p$NZBPR_Password"
  217.     fi
  218.  
  219.     $UnrarCmd x -y -p- "$rarpasswordparam" -o+ "*.rar"  ./$ExtractedDir/
  220.     if [ "$?" -eq 3 ]; then
  221.         echo "[ERROR] Post-Process: Unrar failed"
  222.         if [ "$ExtractedDirExists" -eq 0 ]; then
  223.             rm -R $ExtractedDir
  224.         fi
  225.         # for delayed par-check/-repair at least one par-file must be already downloaded
  226.         if (ls *.[pP][aA][rR]2 >/dev/null 2>&1); then
  227.             echo "[INFO] Post-Process: Requesting par-repair"
  228.             exit $POSTPROCESS_PARCHECK_ALL
  229.         fi
  230.         exit $POSTPROCESS_ERROR
  231.     fi
  232.     Unrared=1
  233.    
  234.     # Remove the rar files
  235.     if [ "$DeleteRarFiles" = "yes" ]; then
  236.         echo "[INFO] Post-Process: Deleting rar-files"
  237.         rm *.r[0-9][0-9] >/dev/null 2>&1
  238.         rm *.rar >/dev/null 2>&1
  239.         rm *.s[0-9][0-9] >/dev/null 2>&1
  240.     fi
  241.    
  242.     # Go to the temp directory and try to unrar again.  
  243.     # If there are any rars inside the extracted rars then these will no also be unrarred
  244.     cd $ExtractedDir
  245.     if (ls *.rar >/dev/null 2>&1); then
  246.         echo "[INFO] Post-Process: Unraring (second pass)"
  247.         $UnrarCmd x -y -p- -o+ "*.rar"
  248.  
  249.         if [ "$?" -eq 3 ]; then
  250.             echo "[INFO] Post-Process: Unrar (second pass) failed"
  251.             exit $POSTPROCESS_ERROR
  252.         fi
  253.  
  254.         # Delete the Rar files
  255.         if [ "$DeleteRarFiles" = "yes" ]; then
  256.             echo "[INFO] Post-Process: Deleting rar-files (second pass)"
  257.             rm *.r[0-9][0-9] >/dev/null 2>&1
  258.             rm *.rar >/dev/null 2>&1
  259.             rm *.s[0-9][0-9] >/dev/null 2>&1
  260.         fi
  261.     fi
  262.    
  263.     # Move everything back to the Download folder
  264.     mv * ..
  265.     cd ..
  266.     rmdir $ExtractedDir
  267. fi
  268.  
  269. # If download contains only nzb-files move them into nzb-directory
  270. # for further download
  271. # Check if command "wc" exists
  272. wc -l . >/dev/null 2>&1
  273. if [ "$?" -ne 127 ]; then
  274.     AllFilesCount=`ls -1 2>/dev/null | wc -l`
  275.     NZBFilesCount=`ls -1 *.nzb 2>/dev/null | wc -l`
  276.     if [ "$AllFilesCount" -eq "$NZBFilesCount" ]; then
  277.         echo "[INFO] Moving downloaded nzb-files into incoming nzb-directory for further download"
  278.         mv *.nzb $NZBOP_NZBDIR
  279.     fi
  280. fi
  281.  
  282. # Clean up
  283. echo "[INFO] Post-Process: Cleaning up"
  284. chmod -R a+rw .
  285. rm *.nzb >/dev/null 2>&1
  286. rm *.sfv >/dev/null 2>&1
  287. rm *.1 >/dev/null 2>&1
  288. rm _brokenlog.txt >/dev/null 2>&1
  289. if [ "$Unrared" -eq 1 ]; then
  290.     # Delete par2-file only if there were files for unpacking.
  291.     rm *.[pP][aA][rR]2 >/dev/null 2>&1
  292. fi
  293.  
  294. if [ "$JoinTS" = "yes" ]; then
  295.     # Join any split .ts files if they are named xxxx.0000.ts xxxx.0001.ts
  296.     # They will be joined together to a file called xxxx.0001.ts
  297.     if (ls *.ts >/dev/null 2>&1); then
  298.         echo "[INFO] Post-Process: Joining ts-files"
  299.         tsname=`find . -name "*0001.ts" |awk -F/ '{print $NF}'`
  300.         cat *0???.ts > ./$tsname
  301.     fi  
  302.    
  303.     # Remove all the split .ts files
  304.     echo "[INFO] Post-Process: Deleting source ts-files"
  305.     rm *0???.ts >/dev/null 2>&1
  306. fi
  307.  
  308. if [ "$RenameIMG" = "yes" ]; then
  309.     # Rename img file to iso
  310.     # It will be renamed to .img.iso so you can see that it has been renamed
  311.     if (ls *.img >/dev/null 2>&1); then
  312.         echo "[INFO] Post-Process: Renaming img-files to iso"
  313.         imgname=`find . -name "*.img" |awk -F/ '{print $NF}'`
  314.         mv $imgname $imgname.iso
  315.     fi  
  316. fi
  317.  
  318. ############################
  319. ### BEGIN CUSTOMIZATIONS ###
  320. ############################
  321.  
  322. # Move categories to /share/yourdirectory and remove download destination directory
  323. if [ "$NZBPP_CATEGORY" = "Series" ]; then # Change here.
  324.         echo "[INFO] Post-Process: Moving TV shows to /volume1/series" # Change here.
  325.         cp -R "$NZBPP_DIRECTORY" "/volume1/series" >/dev/null 2>&1 # Change here.
  326.         if [ "$?" -ne 0 ]; then
  327.            echo "[ERROR] Post-Process: Moving to /volume1/series failed" # Change here.
  328.            exit $POSTPROCESS_ERROR
  329.         else
  330.            rm -fr *
  331.            cd ..
  332.            rmdir "$NZBPP_DIRECTORY"
  333.            NZBPP_DIRECTORY="/volume1/series"
  334.         fi
  335. fi
  336.  
  337. if [ "$NZBPP_CATEGORY" = "Movies" ]; then # Change here.
  338.         echo "[INFO] Post-Process: Moving Movies to /volume2/downloaded" # Change here.
  339.         cp -R "$NZBPP_DIRECTORY" "/volume2/downloaded" >/dev/null 2>&1 # Change here.
  340.         if [ "$?" -ne 0 ]; then
  341.            echo "[ERROR] Post-Process: Moving to /volume2/downloaded" # Change here.
  342.            exit $POSTPROCESS_ERROR
  343.         else
  344.            rm -fr *
  345.            cd ..
  346.            rmdir "$NZBPP_DIRECTORY"
  347.            NZBPP_DIRECTORY="/volume2/downloaded"
  348.         fi
  349. fi
  350.                                                                                                                                                                                                                                                
  351. ##########################
  352. ### END CUSTOMIZATIONS ###
  353. ##########################
  354.  
  355. if [ "$SickBeard" = "yes" -a "$NZBPP_CATEGORY" = "$SickBeardCategory" -a -e "$SabToSickBeard" ]; then
  356.     # Call SickBeard's postprocessing script
  357.     echo "[INFO] Post-Process: Running SickBeard's postprocessing script"
  358.     $PythonCmd $SabToSickBeard "$NZBPP_DIRECTORY" "$NZBPP_NZBFILENAME" >/dev/null 2>&1
  359. fi
  360.  
  361. if [ "$CouchPotato" = "yes" -a "$NZBPP_CATEGORY" = "$CouchPotatoCategory" -a -e "$SabToCouchPotato" ]; then
  362.     # Call CouchPotato's postprocessing script
  363.     echo "[INFO] Post-Process: Running CouchPotato's postprocessing script"
  364.     $PythonCmd $SabToCouchPotato "$NZBPP_DIRECTORY" "$NZBPP_NZBFILENAME" >/dev/null 2>&1
  365. fi
  366.  
  367. # Check if destination directory was set in postprocessing parameters
  368. # (for current nzb-file) via web-interface or via command line with
  369. # "nzbget -E G O DestDir=/new/path <ID>"
  370. if [ "$NZBPR_DestDir" != "" ]; then
  371.     mkdir $NZBPR_DestDir
  372.     mv * $NZBPR_DestDir >/dev/null 2>&1
  373.     cd ..
  374.     rmdir $NZBPP_DIRECTORY
  375. fi
  376.  
  377. # All OK, requesting cleaning up of download queue
  378. exit $POSTPROCESS_SUCCESS
Advertisement
Add Comment
Please, Sign In to add comment