3
# Example postprocessing script for NZBGet
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>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
####################### Usage instructions #######################
26
# o Script will unrar downloaded rar files, join ts-files and rename img-files
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
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.
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.
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.
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,
53
# To use delayed par-check set following options in nzbget configuration file:
56
# LoadPars=one (or) LoadPars=all
58
# o If you want to par-check/repair all files before trying to unpack them,
59
# set option "ParCheck=yes".
61
####################### End of Usage instructions #######################
64
# NZBGet passes following arguments to postprocess-programm as environment
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
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).
86
# Name of script's configuration file
87
SCRIPT_CONFIG_FILE="postprocess-example.conf"
90
POSTPROCESS_PARCHECK_CURRENT=91
91
POSTPROCESS_PARCHECK_ALL=92
92
POSTPROCESS_SUCCESS=93
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
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
111
echo "[INFO] Post-Process: Post-process script successfully started"
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
119
ScriptConfigFile="$ConfigDir/$SCRIPT_CONFIG_FILE"
121
if [ ! -f "$ScriptConfigFile" ]; then
122
echo "[ERROR] Post-Process: Configuration file $ScriptConfigFile not found, exiting"
123
exit $POSTPROCESS_ERROR
126
# Readg configuration file
127
while read line; do eval "$line"; done < $ScriptConfigFile
129
# Check nzbget.conf options
132
if [ "$NZBOP_ALLOWREPROCESS" = "yes" ]; then
133
echo "[ERROR] Post-Process: Please disable option \"AllowReProcess\" in nzbget configuration file"
137
if [ "$NZBOP_LOADPARS" = "none" ]; then
138
echo "[ERROR] Post-Process: Please set option \"LoadPars\" to \"One\" or \"All\" in nzbget configuration file"
142
if [ "$NZBOP_PARREPAIR" = "no" ]; then
143
echo "[ERROR] Post-Process: Please set option \"ParRepair\" to \"Yes\" in nzbget configuration file"
147
if [ "$BadConfig" -eq 1 ]; then
148
echo "[ERROR] Post-Process: Existing because of not compatible nzbget configuration"
149
exit $POSTPROCESS_ERROR
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
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"
163
echo "[WARNING] Post-Process: Par-check failed, exiting"
165
exit $POSTPROCESS_ERROR
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
174
cd "$NZBPP_DIRECTORY"
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
187
# All checks done, now processing the files
189
# Flag indicates that something was unrared
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
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
202
# Make a temporary directory to store the unrarred files
204
if [ -d extracted ]; then
210
echo "[INFO] Post-Process: Unraring"
212
if [ "$NZBPR_Password" != "" ]; then
213
rarpasswordparam="-p$NZBPR_Password"
216
$UnrarCmd x -y -p- "$rarpasswordparam" -o+ "*.rar" ./extracted/
217
if [ "$?" -eq 3 ]; then
218
echo "[ERROR] Post-Process: Unrar failed"
219
if [ "$ExtractedDirExists" -eq 0 ]; then
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
227
exit $POSTPROCESS_ERROR
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
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
242
if (ls *.rar >/dev/null 2>&1); then
243
echo "[INFO] Post-Process: Unraring (second pass)"
244
$UnrarCmd x -y -p- -o+ "*.rar"
246
if [ "$?" -eq 3 ]; then
247
echo "[INFO] Post-Process: Unrar (second pass) failed"
248
exit $POSTPROCESS_ERROR
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
260
# Move everything back to the Download folder
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
280
echo "[INFO] Post-Process: Cleaning up"
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
if [ "$Unrared" -eq 1 ]; then
287
# Delete par2-file only if there were files for unpacking.
288
rm *.[pP][aA][rR]2 >/dev/null 2>&1
291
if [ "$JoinTS" = "yes" ]; then
292
# Join any split .ts files if they are named xxxx.0000.ts xxxx.0001.ts
293
# They will be joined together to a file called xxxx.0001.ts
294
if (ls *.ts >/dev/null 2>&1); then
295
echo "[INFO] Post-Process: Joining ts-files"
296
tsname=`find . -name "*0001.ts" |awk -F/ '{print $NF}'`
297
cat *0???.ts > ./$tsname
300
# Remove all the split .ts files
301
echo "[INFO] Post-Process: Deleting source ts-files"
302
rm *0???.ts >/dev/null 2>&1
305
if [ "$RenameIMG" = "yes" ]; then
306
# Rename img file to iso
307
# It will be renamed to .img.iso so you can see that it has been renamed
308
if (ls *.img >/dev/null 2>&1); then
309
echo "[INFO] Post-Process: Renaming img-files to iso"
310
imgname=`find . -name "*.img" |awk -F/ '{print $NF}'`
311
mv $imgname $imgname.iso
315
# Check if destination directory was set in postprocessing parameters
316
# (for current nzb-file) via web-interface or via command line with
317
# "nzbget -E G O DestDir=/new/path <ID>"
318
if [ "$NZBPR_DestDir" != "" ]; then
320
mv * $NZBPR_DestDir >/dev/null 2>&1
322
rmdir $NZBPP_DIRECTORY
325
# All OK, requesting cleaning up of download queue
326
exit $POSTPROCESS_SUCCESS