Advertisement
Guest User

Untitled

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