View difference between Paste ID: KZ8Q0d0D and i0WpEQiZ
SHOW: | | - or go back to the newest paste.
1
#!/bin/sh
2
3
##############################################################################
4
### NZBGET POST-PROCESSING SCRIPT                                          ###
5
6-
# Rename garbled MKV files to season and episode numbering.
6+
# Rename media files for better Sick Beard and CouchPotato Server processing.
7-
# Useful for QoQ releases.
7+
8
# By default scans for season and episode numbering for series and ttid for
9-
# Directory allowed to rename files in (should be equal to tv category DestDir)
9+
# movies. If none is found, the largest media file is renamed to NZB name. Set
10-
#AllowedDir=/volume1/video/tv
10+
# ForceRename to rename regardless. This can be used to deal with scene
11
# exceptions.
12
13
##############################################################################
14
### OPTIONS                                                                ###
15-
# Tested on Synology DSM 4.3-3810 Update 2 (BusyBox v1.16.1)
15+
16
# Directory allowed to rename files in.
17
#AllowedDir=/volume1/download/dst
18
19
# Category label for TV shows.
20-
#       e.g. /usr/local/ppscripts/RenameMKV.sh
20+
#Category.TV=tv
21
22
# Category label for movies.
23-
#       chmod a+x /usr/local/ppscripts/RenameMKV.sh
23+
#Category.Movies=movies
24
25
# Force renaming to NZB name (yes, no).
26
#ForceRename=no
27-
#  4. NZBGet Settings > RenameMKV: set AllowedDir to tv category DestDir,
27+
28-
#       e.g. /volume1/video/tv
28+
29
##############################################################################
30-
#  5. NZBGet Settings > Post-Processing Scripts: move RenameMKV.sh after DeleteSamples.py,
30+
31-
#       e.g. DeleteSamples.py, RenameMKV.sh, nzbToMedia*.py, Email.py, Logger.py
31+
# Tested on Synology DSM 4.3-3810 Update 3 (BusyBox v1.16.1)
32
33-
#  6. NZBGet Settings > Categories: set RenameMKV.sh as DefScript for tv category,
33+
34-
#       e.g. RenameMKV.sh, nzbToMedia/nzbToSickBeard.py
34+
35
#  1. Place script in ppscripts directory,
36-
#  NZBGet will now execute RenameMKV.sh for tv NZBs added from this point onwards.
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-
#   * [ADD] AllowedDir error message now includes directories for debugging
44+
#       e.g. /volume1/download/dst
45
#
46-
ALLOWED_DIR=$NZBPP_ALLOWEDDIR
46+
#  5. NZBGet Settings > RenameMedia: set Category.TV and Category.Movies labels,
47-
if [ "$ALLOWED_DIR" = "" ]; then
47+
#       e.g. Category.TV=tv
48-
        ALLOWED_DIR="/volume1/video/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-
if [ "$NZBPP_DIRECTORY" == "" ]; then
52+
53-
        echo "[ERROR] Empty directory name, skipping."
53+
#  7. NZBGet Settings > Categories: set RenameMedia as DefScript for tv and movie categories,
54-
        exit 95
54+
#       e.g. RenameMedia, nzbToMedia/nzbToSickBeard.py
55
#
56-
if [ "$NZBPP_NZBNAME" == "" ]; then
56+
#  NZBGet will now execute RenameMedia for tv and movie NZBs added from this point onwards.
57-
        echo "[ERROR] Empty NZB name, skipping."
57+
58-
        exit 95
58+
59
# CHANGELOG:
60
#
61-
echo "[INFO] Fixing MKV filenames in $NZBPP_DIRECTORY for $NZBPP_NZBNAME"
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-
if [ "$NZBPP_DIRECTORY" == "." -o "$NZBPP_DIRECTORY" == "/" ]; then
64+
#   * [CHANGE] Minor refactoring
65-
        echo "[ERROR] Illegal directory name, skipping."
65+
66-
        exit 95
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-
if [ `echo "$NZBPP_DIRECTORY" | grep "$ALLOWED_DIR" | wc -l` == "0" ]; then
70+
#   * [ADD] Support force renaming (useful for scene exceptions)
71-
        echo "[ERROR] Directory $NZBPP_DIRECTORY not in allowed $ALLOWED_DIR, skipping."
71+
#   * [CHANGE] AllowedDir must now be a full path
72-
        exit 95
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-
# Find files which already have season and episode numbering
75+
76-
if [ `find "$NZBPP_DIRECTORY" -maxdepth 1 -type f -regex '.*[sS][0-9][0-9]*[eE][0-9][0-9]*.*\.[mM][kK][vV]$' | wc -l` == "0" ]; then
76+
77-
        # No parsed files found, try to find MKV files now
77+
78-
        NZB_NAME=`echo $NZBPP_NZBNAME | sed 's/([0-9][0-9][0-9][0-9])//g' | sed 's/  / /g' | sed 's/\.\./\./g'`
78+
79-
        echo "[DETAIL] Renaming MKV files to $NZB_NAME.mkv"
79+
#   * [CHANGE] AllowedDir error message now includes directories for debugging
80-
        find "$NZBPP_DIRECTORY" -name *.mkv -exec mv {} "$NZBPP_DIRECTORY/$NZB_NAME.mkv" \;
80+
81-
        RESULT=$?
81+
# Defaults -- users should override them in the NZBGet GUI
82-
        if ! [ "$RESULT" == 0 ]; then
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-
else
86+
87-
        echo "[DETAIL] No MKV files to rename."
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